CSCI1200 Homework 2 — Pseudo-functions solution

$35.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Objectives:

– Practice formulating problems as pseudocode – Identify the type, and trace the value of a variable as it is manipulated within a code snippet – Understand code execution ordering constraints – Understand using functions in python — both with and without parameters and return variables involved

Turn In: – hw2_LASTNAME_written.pdf (corresponding to part 1) – hw2_LASTNAME_code.ipynb (corresponding to part 2) You should turn these files in on canvas under the “Homework 2 – Pseudo-functions” link.

Make sure to double check everything on the following checklist: ❏ Your files are named hw2_LASTNAME_written.pdf and hw2_LASTNAME_code.ipynb ❏ You’ve fully submitted the file by uploading to Canvas and pressing “Submit Assignment” after you upload it! ❏ You’ve submitted the correct files (download what you submitted, re-open them, and verify!) ❏ hw2_LASTNAME_written.pdf is one single pdf that includes your name

❏ hw2_LASTNAME_code.ipynb is commented as described below ❏ hw2_LASTNAME_code.ipynb produces no errors and runs all cells when you click “restart kernel” then run all the cells from top to bottom Part 1: Written responses (hw2_LASTNAME_written.pdf)

Answer the questions below using the word processor of your choice (e.g., Microsoft Word, or Google Docs), save the file as a pdf, and submit the file on canvas. Include your name at the top of the file that you submit.

Question 1 (15 points): Pseudocode

You are not expected to code the answers to these problems in Python. Your pseudocode should be written as flowcharts. Review the lecture 4 slides for a refresher on pseudocode.

Describe how you would solve the following problems using pseudocode in flowchart format: A) (7 points) Write the pseudocode for a program to help a student register for a class, from selecting a course to successfully registering for a course.

In this scenario, the student may or may not have already registered for other classes and classes may or may not have space in them. To successfully register for a class, the student must not have time conflicts and there must be at least one open space in the class. You may assume that you have access to the student’s current schedule and a database with information on all classes, including the times that they occur at and how many seats are available in each.

The first few steps are provided to help you get started.

B) (8 points) Write the pseudocode for a program to play the game 20 questions (the animal, vegetable, mineral version). The computer will pick the object, and the player will ask up to 20 yes/no questions to try to guess what the object is. Your program has access to a knowledge bank that knows the answer to any yes or no question.

You should not attempt to write the specific questions that the user would ask (remember, your pseudocode should work for any game of 20 questions). If you have never played this game before, come to office hours, we would be happy to demonstrate it! A few steps are provided to help you get started.

Part 2: Writing Code (hw2_code.ipynb)

For this part, download hw2_starter.ipynb, rename this file to hw2_LASTNAME_code.ipynb, and complete the questions as described below. For full credit, you must also include a comment at the top of your notebook as follows.

Remember that any line that begins with a # is a comment and is just there for informational purposes. # Name: # Section: # Homework: # Description: To earn full credit, your notebook must run without errors, include a comment with your name at the top of your file, and contain completed exercises. 

2 Question 2 (15 points):

We’ve learned that programs are always executed top to bottom. Feel free to use print() statements to verify your answers! A.

Take the following code snippet and trace the value of the variable my_var as it executes as shown on the right side (we’ve done the first one for you as an example of what it should look like). (4 points) my_var = 0.0 my_var = 1 my_var = “great white shark” my_var = 7.4 my_var = 0 my_var = my_var + 1 my_var = my_var + 1 my_var = my_var * 6 my_var = my_var / 1 value, type 0.0, float B.

Run the following code. What kind of error is produced? (1 point) print(x) x = 7 This happens because, as we’ve learned, you need to declare variables before you can use them. For parts C, D, E and F, take the code snippets and rearrange the lines so that they produce no errors when you run them.

Do not modify the lines, just change their order! Note that we have provided cells but no code in your starter notebook. This is to help you build the muscle memory of typing out code, so be careful to type it exactly as we have given it in this document!

C. (1 point) print(x) x = 7 D. (2 points) num1 = 0 num2 = 1 print(num3) num3 = num1 + num2 + num3 num3 = num2 + num2 E. (2 points) name = “harriet” print(name + profession * mult) 

3 profession = “spy” mult = 3 F. (2 points) a = 2 text_2 = ” baa” print(c) text_1 = “baa black sheep” c = text_1 + text_2 * b print(b) b = 2 * a G. Why does the following code not work? Fix it. When you fix it, this code should successfully get the user’s age and print out a statement reading “I am [user’s age here]”.

For example, if the user is 7, it would print “I am 7”. (Hint: this is not an ordering error.) (2 points) input(“How old are you? “) print(“I am ” + age)

H. Why does the following code not work? Note: you are not required to fix this code. (Hint: this is not an ordering error, nor is it the same as the previous question.) (1 points) age = 112 ten_years_from_now = age + 10 print(“I will be ” + ten_years_from_now)

I. What are the three reasons that the following code does not work? Note: you are not required to fix this code. (Hint: there are no ordering errors here.) (2 points) input(“How old are you? “) ten_years_from_now = age + 10 print(“I will be ” + ten_years_from_now)

Question 3 (30 points): Using functions

For this section, you do not need to define your own functions. A) (6 points) Give an example usage of each of the following built-in functions: len(), abs(), round(), and chr().

Valid examples must do all of the following: a) Run b) Use the function with appropriate arguments c) Save any return values in a variable d) Print out the return value Consult the python documentation for information about how functions work. Some of these functions may return types that we haven’t worked with yet—that’s okay, the goal of this exercise is exploration! 

4 You should have 2 – 3 lines of code and 1 comment for each example. B) (8 points) Write a python program that tells the user about your favorite movie. Tell them about the movie using the print() function, then ask the user what their name is and what their favorite movie is by using the input() function.

After the user answers these questions, greet them and make a comment about their favorite movie. You should produce output similar to the examples below (but not the same as!). Make sure to replace “[Your message here]” with a message about the movie that the user has entered. The only requirement for this message is that it must incorporate the name of the movie that the user has entered.

Example Output (bold and underlined are user input) My name is David and my favorite movie is Jurassic Park. What is your name? Christine What is your favorite movie? Apollo 13 Hello, Christine. [Your message here] My name is Cat and my favorite movie is The Imitation Game. What is your name? Sidd What is your favorite movie? 2001: A Space Odyssey Hello, Sidd. [Your message here] My name is Aiden and my favorite movie is Good Will Hunting.

What is your name? Aditi What is your favorite movie? Now You See Me Hello, Aditi. [Your message here] C) (8 points) Write a program that prompts the user for their height in inches, converts that amount to centimeters, and reports back the user’s height in both centimeters and meters. Use the round() function to round your answers to two decimal places. (Note: to convert inches (“in”) to centimeters (“cm”), multiply the number of inches by 2.54.

To convert centimeters to meters (“m”), divide the number of centimeters by 100). Example output (user input bold underlined): What is your height (in)? 60 That is 152.4 cm and 1.52 m! What is your height (in)? 76 That is 193.04 cm and 1.93 m! D) (8 points) Write a program that calculates the maximum occupancy of a room, given its length and width as reported by the user.

We will use the following formula to calculate maximum occupancy: number_of_people = area_of_the_room / 20.

5 Make sure that your reported occupancy does not include any partial people! (Hint: think about python’s different kinds of divide operators and casting to help you out!)

Example output (user input bold underlined): What is the room length (ft)? 10 What is the room width (ft)? 20 Occupancy: 10 people What is the room length (ft)? 13 What is the room width (ft)? 214 Occupancy: 139 people