HW_W11 (1)

docx

School

San Francisco State University *

*We aren’t endorsed by this school

Course

3000

Subject

Computer Science

Date

Nov 24, 2024

Type

docx

Pages

9

Uploaded by sparrow01

Report
COMP101: Page 1 of 9 Tolulope Adegunju Homework - WEEK 11 [45 points] This is a review of the material from the textbook (Chapter 8 [8.7-8.10]) and lectures from class. You will not be given credit for answers that are copies or near verbatim transcripts – please use your own words and document sources where appropriate using APA guidelines. Chapter 8 (More About Iteration [8.7-8.10]) Week 11 Learning Outcomes Explain how algorithms are mechanical processes for solving a category of problems. Define an algorithm. Learn how to produce output tables Use nested loops for image processing. Short Answer Questions (10 points) . Complete the following questions in one or more paragraphs using APA format as required: 1. What are the differences between definite iteration and indefinite iteration in a while loop? Provide an example and explanation for each. (3 points) An indefinite iteration means that the number of times the while loop will be executed, it is not specified by the code but will be executed if and only if the condition is true. On the other hand, a definite iteration means that the number of times a given code is executed which is specified in the code. 2. Provide an algorithm to show the steps you would take to calculate and display the gross pay for an hourly paid employee (including overtime). You will need to know a couple of things before getting started. The base hours per week are 40. The overtime multiplier is 1.5 (time and a half for all hours over the base). You will need to prompt the user for the hours worked and the hourly pay rate. Then you will calculate and display the gross pay with overtime (if applicable). (3 points) Hours worked = Hourly rate = Regular hours =40 overtime hours = hours - 40
COMP101: Page 2 of 9 Gross pay = (Regular hours * rate) + (overtime hours * 1.5) 3. What are nested loops? Give an example of an algorithm that would need a nested loop and explain how it works. (2 points) With a nested loop, this means that a loop commands are inside the commands of another loop. There is an outer loop and an inner loop in the code. 4. What is a sentinel value? What happens if the sentinel is generated as part of the input stream? How does this affect your choice of sentinel? (2 points) A sentinel value can be said to be a value used while terminating a loop. For you to terminate a loop, you need to create a sentinel value which will not be part of the loop, this will tell the python program when the loop needs to be terminated. Programming Problems (30 points) . All Python programming problems/code snippets for this class are to be written/tested using one of two possible repl.it 1 Python 3 websites. You will work on your code in one of these websites and then copy your final code from the repl.it application and paste it into this Word document 2 for final grading. There are two possible links to be used: one link is for the Python 3 programming problems/code snippets that do not require the Turtle drawing module, and one is for the Python 3 programming problems/code snippets that do require the Python 3 Turtle module. The link you will use for each problem is shown below 3 in square brackets right after the points. Do NOT share or save your code on this website. Also, no screenshots of code will be accepted; you must copy and paste your code into this Word document before submitting. 1. (4 points) [ https://repl.it/languages/python3 ] Write a Python program snippet that accepts a number (prompt the user to enter start number (Enter a number:), multiples itself by 2 until the number is larger than 10,000; prints the number of multiplication operations. Program n= 0 num = int(input("Enter a number:")) while num < 100: num = num * 2 n+= 1 1 The repl.it website is an online tool for running code online in a number of programming languages, including Pyt hon 3. 2 Please make sure your code is in the proper format with proper indenting, etc. after copying it in this document for grading. 3 If a programming problem below requires the Turtle module, then you will need to open the repl.it website that su pports it. Otherwise, you will need to open the repl.it website that does not require this module.
COMP101: Page 3 of 9 print (num) print (n) Output 2. (4 points) [ https://repl.it/languages/python3 ] Create a Python program snippet where every number in a list called numbs (see list below) multiplies itself by all the other numbers in the list one by one (including itself); print the sum of all the multiplications. numbs = [ 2, 5, 8, 4, 3, 6, 7, 9] Program numbs = [ 3, 5, 6, 4, 8, 5, 7, 9] total = 0 for i in numbs: for j in numbs: total = total + i * j print (total) Output
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
COMP101: Page 4 of 9 3. (7 points) [ https://repl.it/languages/python3 ] Write a function called diff(list) that returns the difference between the largest and smallest values in the list. For example, diff([10, 3, 5, 6]) → 7 diff([7, 2, 10, 9]) → 8 diff([2, 10, 7, 1]) → 9 Hint: to get the largest ( or smallest) number, you can compare the first number, list[0], with all the other numbers one by one, and assign the value of other number to it if the other number is larger (or smaller). Program def diff(list): largNum = list[0] smalNum = list[0] for i in list: if i > largNum: largNum = i elif i < smalNum: smalNum = i return largNum - smalNum print (diff([1, 3, 8, 6, 5, 12])) Output 4. (8 points) [ https://repl.it/languages/python_turtle ] Modify the second walking turtle program in the textbook in section 8.4 so that rather than a 90 degree left or right turn, the angle of the turn is determined randomly at each step (0 to 360). In each forward move, change the step from 50 to a number between 75 and 125 (both included), and set the color to red, blue, or green randomly. Also, the turtle turns 180 degrees to the left when it hits the wall. So, if the turtle hits the wall then turn left 180 degrees otherwise turn left at a random angle between 0 and 360 degrees. The program will quit when the turtle hits the
COMP101: Page 5 of 9 wall the third time. You will need to use the turtle module and the random module for this exercise. The code below can be removed or changed as needed. import turtle t = turtle.Turtle() t.forward(75) Program import random import turtle ctr = 0 def isInScreen(w,t): leftBound = - w.window_width() / 2 rightBound = w.window_width() / 2 topBound = w.window_height() / 2 bottomBound = -w.window_height() / 2 turtleX = t.xcor() turtleY = t.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: t.left(180) ctr = ctr + 1 if turtleY > topBound or turtleY < bottomBound:
COMP101: Page 6 of 9 t.left(180) ctr = ctr +1 return stillIn # or return ctr t = turtle.Turtle() wn = turtle.Screen() t.shape('turtle') while isInScreen(wn,t) < 3: coin = random.randrange(0, 2) if coin == 0: t.left(random.randrange(0, 361)) else: t.right(random.randrange(0, 361)) roll = random.randrange(0, 3) if roll == 0: t.color("red") elif roll == 1: t.color("blue") else: t.color("green") t.forward(random.randrange(75, 126)) wn.exitonclick() 5. (7 points) [ https://repl.it/languages/python3 ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
COMP101: Page 7 of 9 Write a program that simulates rolling dice. You are playing against the computer. The computer will roll the dice by randomly generating a number between 1 and 6 inclusive. You will then be asked if you want to roll the dice ( Do you want to roll the dice? ). If you respond with Y or y, the dice is rolled by randomly generating a number between 1 and 6. Print the number generated (I rolled: number ). If your roll matches the initial computer roll, then display I rolled: number then I WIN! on a new line and quit the program. If the roll does not match, you will be asked to roll again ( Do you want to roll the dice? ). If you respond with N or n, the game ends. Responding with something other than Y or y or N or n simply re-prompts the user for another roll. This program will be graded manually. There are no pre-defined test cases. Program import random computer = random.choice ([1,2,3,4,5,6]) user = input("Do you want to roll the dice?") while user == "Y" or user == "y": user = random.choice([1,2,3,4,5,6]) print("I rolled:", user) if user == computer: print ("I WIN!") else: while user not in ["Y", "y", "N", "n"]: user = input("Do you want to roll the dice?") Output
COMP101: Page 8 of 9 Class/Meet Session Reflection (5 points) In two to three paragraphs of prose (i.e. sentences, not bullet lists) using APA style citations as needed, summarize and interact with the content that was covered for this week using the learning outcomes listed above for reference. In your summary, you should highlight the major topics, theories, practices, and knowledge that were covered. Your summary should also interact with the material through personal observations, reflections, and applications to the field of study. In particular, highlight what surprised, confused, enlightened, or otherwise engaged you. In other words, you should think and write critically not just about what was presented but also what you have learned through the session. Feel free to ask questions in this as well since it will be returned to you with answers. (5 points) With this week’s assignment, I was able to learn more about the topics we’ve learnt throughout this semester. I enjoyed working with turtles. This is because they make Python fun since you can see the results of your work. Turtles also make Python fun since I was able to see how my program reacted by changing size, speed and even color.
COMP101: Page 9 of 9 However, these assignments were tough. I spent many days working on them. I also did a lot of research to clear some errors that I encountered. I will say that this is a serious course and it needs extensive research and a lot of practice. I got new challenges every week which grew harder.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help