CS10_PS4-3

pdf

School

Santa Clara University *

*We aren’t endorsed by this school

Course

1

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

4

Uploaded by PrivateJellyfish21712

Report
Santa Clara University Fall 2023 Name: | {z } Write your name here. Csci 10–Problem Set 3 Problem 1. The grading scheme of our CS 10 class is given below. Write code to convert a number grade to a letter grade accordingly. Problem 2. Write a class PizzaOrder to take the order’s name, size, and the number of toppings. (a) Suppose the prices are as follows: Large—$23, Medium—$19, and Small—$15, and Topping—$1 each. Write a member function to take the size of the order, the number of the pizzas, and the number of toppings and return the price and the proposed tips corresponding to 22%, 20%, and 18%. (b) Write a member function printOrder() in details. Problem 3. Write a program to prompt the user for which language (give your own choices here) they would like to say “Hello” in. Then, print out, say, “Hola” if they choose Spanish . Improve your program with a while loop to repeatedly prompt the user until they give the right language option. Problem 4. In Statistics, the measures of center include mean, median, and mode. They can be defined as follows for the data set of values x 1 , x 2 , . . . , x n . (a) Mean: ¯ x = x 1 + x 2 + . . . + x n n . Write a user-defined function to return the mean of a given data set. (b) Median is the middle value when your data values are ordered from smallest to greatest . Write a user-defined function to return the median of a given data set. (You might use the sort() method here or use your own sort function from HW 12.) (c) Mode is the value that appears the most often in your data set. Write a user-defined function to return the mode of a given data set. (You might use the count() method here or use your own count function.) (d) Create a class Stat to manage your data sets by including the above three functions as the member functions of your Stat class. Write another member function printStat() to nicely display the mean, median, and mode of a given data set. Problem 5. Assume the days of the week are numbered 0,1,2,3,4,5,6 from Sunday to Saturday. Write a function which is given the day number, and it returns the day name (a string). Problem 6. You go on a wonderful holiday (perhaps to jail, if you don’t like happy exercises) leaving on day number 3 (a Wednesday). You return home after 137 sleeps. Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the name of day of the week you will return on. Problem 7. Write a function days in month which takes the name of a month, and returns the number of days in the month. Problem 8. Write a function to secs that converts hours, minutes and seconds to a total number of seconds.
Problem 9. Write a function that is the “inverses” of to secs . For example, if we have 9010 seconds, the function returns 2 hours, 30 minutes, and 10 seconds. Problem 10. The slope, m , of the line passing through the points ( x 0 , y 0 ) and ( x 1 , y 1 ) is given as m = y 1 - y 0 x 1 - x 0 . (a) Write a function slope( x 0 , y 0 , x 1 , y 1 ) that returns the slope of the line through the points ( x 0 , y 0 ) and ( x 1 , y 1 ). (b) Then use a call to slope in a new function named intercept( x 0 , y 0 , x 1 , y 1 ) that returns the y-intercept of the line through the points ( x 0 , y 0 ) and ( x 1 , y 1 ). Problem 11. Write a user-defined function multiplication table(n) to return a multiplication table for a given positive integer n . For example, multiplication table(6) should look like Problem 12. Write a user-defined function remove vowels(msg) to remove all the vowels from a given string. Problem 13. Write a user-defined function my find(msg,char) to find and return the index of the character, char , in the given string, and return - 1 if char does not occur in the string. Problem 14. Write a function that mirrors its argument. For example, mirror(‘‘good’’) returns ‘‘gooddoog’’ . Problem 15. Write a function that removes all occurrences of a given letter from a string. For example, remove letter(‘‘a’’, ‘‘banana’’) returns ‘‘bnn’’ . Problem 16. Write a function that recognizes palindromes. For example, is palindrome( ‘‘abba’’) returns True , but is palindrome( ‘‘abab’’) returns False . Problem 17. Write a function that counts how many times a substring occurs in a string. For example, count substring( ‘‘is’’, ‘‘Mississippi’’) returns 2 and count substring( ‘‘aaa’’, ‘‘aaaaaa’’) returns 4. Problem 18. Write a function that removes the first occurrence of a string from another string. For example, remove substring( ‘‘iss’’, ‘‘Mississippi’’) returns ‘‘Missippi’’ . Problem 19. Write a function called cumsum that takes a list of numbers and returns the cumulative sum by appending the sum as the new last element of the original list. For example, =[1,2,3] , cumsum(t) returns [1 , 2 , 3 , 6] Problem 20. Write a function called middle that takes a list and returns a new list that containsall but the first and last elements. Problem 21. Write a function called is sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. For example: Problem 22. (a) Create a class named Point to represent a point P ( x, y ) in the coordinate attributes. (We have done that in class.)
(b) Create another class named Circle with attributes center and radius, where center is a Point object and radius is a number. (c) Write a function named point in circle that takes a Circle and a Point and returns True if the Point lies in or on the boundary of the circle. (d) Instantiate a Circle object that represents a circle with its center at (150, 100) and radius 75. Call your point in circle to test if Q (200 , 130) lies in that circle or not. Problem 23. Let a 1 = 5 and a 2 = 3. For n 3, we define a n = a 2 n - 1 + 3 a n - 2 . Write a recursive function to return a n for n 1. Write a recursive function to find a n for n 1. Problem 24. I took the idea of the following problem from Charles’ Discrete Math class. Thank you, Charles! Define the Tribonacci sequence by T 1 = T 2 = T 3 and T n = T n - 1 + T n - 2 + T n - 3 for n 4. (a) Write a recursive function to return T n for n 1. (b) Write another user-defined function that takes an integer n 1 and returns True if T n < 2 n . (Hint: reuse the above function for T n ). Problem 25. For each conversion, be sure to show your work. (a) Convert 1110101 2 (binary) into decimal. (b) Convert 101 10 (decimal) into binary. Problem 26. Powerball is an American lottery game offered by 45 states, the District of Columbia, Puerto Rico and the U.S. Virgin Islands. Since October 7, 2015, the game has used a 5/69 (white balls) + 1/26 (Powerballs) matrix from which winning numbers are chosen, resulting in odds of 1 in 292,201,338 of winning a jackpot per play. Write code to simulate a Powerball drawing by randomly generating five numbers from 1 to 69 and one Powerball number from 1 to 26. Export your drawing nicely. Continue to develop your code to prompt the users (you can ask your friends) to pick numbers to play and return how many numbers have been matched.
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
Problem 27. Write code to prompt the user to enter one of the animals you see below. Then, print out the cor- responding verse. For example, ‘‘Ladybug’’ returns ‘‘Give a Hug, Ladybug!’’ . Please make sure your program prompt the user again until they enter an animial in the list if they enter something else. Restriction: do not use multiple if statements for each animal—at this stage, you can use the data structures that we have learned and think about how to manage your data effectively. Thank you all for taking part in CSCI 10, Fall 2023. I hope you found it useful, and that you enjoyed it. Special thanks to those who asked questions and who laughed at the jokes. Happy Holidays to everyone!