[CS134.U03.EX] 🏋️ Storing data in lists – Exercise

CS 134: Programming Fundamentals, Summer 2022

Assignment turn-in link: CANVAS TURN IN

📝 Assignment

Exercises are meant to be solo effort, please try to complete it on your own. You can ask the instructor or a tutor questions if you get really stuck, but all the information you need to complete the assignment is in here, or in the related reading/lectures.

If you get stuck, please post questions in the Unit 3 'Help!' discussion board .

🔧 Setup

FIRST, from your "My Repls" page on Repl.it create a New folder for this weeks' assignments name it Unit03. Within this folder you will create separate Repl projects for each program. When you turn in the assignments, you will copy the URL to your folder as the submission.

🎁 Submitting your work

First, make sure all your programs are inside your Unit03 folder. If they're not you can click on the three dots on the right side of the program name and click "Move".

PLEASE GIVE YOUR PROGRAMS THE FOLLOWING NAMES:

  • Unit 03 Program 1
  • Unit 03 Program 2
  • Unit 03 Program 3
  • Unit 03 Program 4

Clean code also means the ability to find the code itself, so if your project is unkempt and hard to navigate you will lose points.

To submit your work, navigate to the folder for your exercises on replit. Copy the URL to that directory (e.g., https://replit.com/@rsingh13?path=folder/Unit03) and paste it in here in Canvas.

Once you've turned in the assignment, make sure to also post your program in the code review assignment,

💻 Programs

Quick jump: Program 1: Planets | Program 2: Products | Program 3: Random numbers | Program 4: Course list

Program 1: Planets

For this program we use a list to display all the planets. The list starts off with 9 planets, but you will need to remove Pluto and then display the updated list.

$ The planets are...
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto

Oops, actually, it's...
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Running the program

1. Start off by going to the starter code here: https://replit.com/@rsingh13/Program-1-Planets#main.py

2. Click on the FORK button to make a copy under your own account.

3. Click the RUN button to see the propgram so far:

The planets are...
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto

Oops, actually, it's...

Modifying the program

Modify the program by...

  1. Remove "Pluto" from the list with either the pop function or the del command (Reference the textbook's chapter 8, "Deleting elements" part for reference.)
  2. Use either type of for loop to display the updated list of all the planets. (In the textbook this is under "Traversing a list".)

Once it's done the program output should look like this:

$ The planets are...
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto

Oops, actually, it's...
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Program 2: Products

⭐⭐

In this program we will store two parallel lists that contain related data: product names and product prices. After creating the lists, the products should be displayed to the screen, including the index of the pair of elements.

$ STATIONERY STORE

Products:
0 	 Fine tip pens 	 $12.99
1 	 Letter set 	 $14.95
2 	 Marker pack 	 $2.47 

Writing the program

  1. Create a new Python project in replit.
  2. Create a list called product_names and a separate list called product_prices. Fill the lists with the following data:

    IndexProduct NameProduct Price
    0Fine tip pens$12.99
    1Letter set$14.95
    2Marker pack$2.47

    Tips:
    • When creating your lists, you won't be specifying the index #. Just remember that the first item in each list will be at position 0, so "Fine tip pens" is the first item to go in product_names and 12.99 is the first item to go in product_prices.
    • Remember that you're creating two separate lists; don't put all the data in one list.
  3. Next, display the program name "STATIONERY STORE", and then a header "Products:"
  4. Use a for i loop to iterate over all the indices of product_names. (Reference the textbook's chapter 8, "Traversing a list" part for reference.)

    Since product_names and product_prices are related, they both have the same range. Within the for loop, print the index (i), the product name at this index (product_names[i]), and the product price at this index (product_prices[i]).

    Remember that you can format currency like this: "${:,.2f}".format( VARIABLENAME ), just replace "VARIABLENAME" with the appropriate variable.

Once done the program output should look like this:

$ STATIONERY STORE

Products:
0 	 Fine tip pens 	 $12.99
1 	 Letter set 	 $14.95
2 	 Marker pack 	 $2.47 

Program 3: Random numbers

⭐⭐

For this program the user will select two numbers from a list by entering their index number. Then the program will add those two numbers together and display the result.

NUMBER LIST

RANDOM NUMBER LIST:
	 Index 	 Number
	 0 	 8
	 1 	 16
	 2 	 30
	 3 	 63

We are going to add two numbers together!
Enter the INDEX of the first number: 2
Enter the INDEX of the second number: 3

	 RESULT:  30 + 63 = 93

Starter code

Go to https://replit.com/@rsingh13/Program-3-Random-numbers#main.py and create a FORK of the program. Currently, it will generate a list of random integers and display the contents of the list to the user.

NUMBER LIST

RANDOM NUMBER LIST:
     Index   Number
     0       90
     1       61
     2       23
     3       59

We are going to add two numbers together!

Modifying the program

Add the following to the program:

  1. # 1. Ask user to enter indices for two of these numbers
    You will need to get the user's input, but it also has to be an int, so use int( input( "..." ) ) to ask the user for the first index and store it in a variable (e.g., "index1").
    Do the same for a second index.
  2. # 2. Calculate the sum of the two numbers together
    You can get the index of each of the two numbers from the numbers list using your index1 and index2 variables. Add these two values together and store the result in a new third variable (e.g., "sum_result").
  3. # 3. Display the result
    Finally, display the result to the screen like the example output. ("RESULT: 19 + 9 = 28")

Once done, your program will look like this:

NUMBER LIST

RANDOM NUMBER LIST:
	 Index 	 Number
	 0 	 58
	 1 	 19
	 2 	 9
	 3 	 18

We are going to add two numbers together!
Enter the INDEX of the first number: 1 
Enter the INDEX of the second number: 2

	 RESULT:  19 + 9 = 28

Program 4: Course list

⭐⭐⭐

This program stores a list of the classes available in the CS department. The user can enter the index of one of these courses in order to view the course name and course description.

COURSE CATALOG
Select a course by its number code:
	 0 	 CS134
	 1 	 CS200
	 2 	 CS201
	 3 	 CS202
	 4 	 CS205
	 5 	 CS210
	 6 	 CS211
	 7 	 CS235
	 8 	 CS236
	 9 	 CS250
	 10 	 CS252
	 11 	 CS255

Which class do you want to see more information about?

Starter code

Start by going to https://replit.com/@rsingh13/Program-4-Course-list#main.py and creating a FORK of the project. The starter code contains three lists: course_codes, course_names, and course_descriptions. The data is all from the JCCC course catalog.

After the three lists are created you will find a comment that says # - Program code -, and you will need to add code beneath each of the three steps outlined in the comments.

Modifying the program

Do the following beneath the corresponding comment in the code:

  1. # 1. Traverse the list of course codes, displaying each index and course code
    Use the appropriate for loop to display each course code AND the index.
  2. # 2. Ask the user which class they want to see, get the input as an int.
    Ask the user to enter an index value and store it in a variable as an integer.
  3. # 3. Display information from course_codes, course_names, and course_description
    # all using the index entered by the user.
    Using the index entered by the user, display the course_codes element at that index, the course_names element at that index, and the course_description element at that index.

Once complete, you can run the program and enter any valid index to view more information about a given course:

COURSE CATALOG
Select a course by its number code:
	 0 	 CS134
	 1 	 CS200
	 2 	 CS201
	 3 	 CS202
	 4 	 CS205
	 5 	 CS210
	 6 	 CS211
	 7 	 CS235
	 8 	 CS236
	 9 	 CS250
	 10 	 CS252
	 11 	 CS255

Which class do you want to see more information about? 1

-----------------------------------------------------------------------------
CS200

Concepts of Programming Algorithms Using C++

This course emphasizes problem solving using a high-level programming language 
and the software development process. Algorithm design and development, programming 
style, documentation, testing and debugging will be presented. Standard algorithms 
and data structures will be introduced. Data abstraction and an introduction to 
object-oriented programming will be studied and used to implement algorithms. 
3 hrs. lecture, 2 hrs. lab by arrangement/wk.