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.)
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