Homework7

pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Mathematics

Date

Feb 20, 2024

Type

pdf

Pages

3

Uploaded by MinisterBravery13388

Report
07 HW - Functions Problem 7.1 Make a function with two input integer parameters. The function will compare the two parameters and return the number that is larger. ('Larger Number', 999) Problem 7.2 Write a function named collatz() that has one parameter named number . If number is even, then collatz() should return number // 2 . If number is odd, then collatz() should return 3 * number + 1 . Call the function three times with arguments 300, 30 and 19 as follows: print ( collatz ( 300 )) print ( collatz ( 30 )) print ( collatz ( 19 )) 150 15 58 Problem 7.3 Now write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. You may have to restart the kernel, if the program hangs up (see last lecture notes). In [21]: def Larger_number ( x , y ): x = int ( input ( "Enter your first nummber" )) y = int ( input ( "Enter your second number" )) if ( x > y ): return ( "Larger Number" , x ) return ( "Larger Number" , y ) print ( Larger_number ( x , y )) In [24]: def collatz ( number ): if ( number % 2 ) == 0 : return ( number // 2 ) return (( 3 * number ) + 1 ) print ( collatz ( 300 )) print ( collatz ( 30 )) print ( collatz ( 19 )) In [37]: def collatz ( number ): if number % 2 == 0 : print ( number // 2 ) return number // 2
136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.” Problem 7.4 Write a function day_name that converts an integer number 0 to 6 into the name of a day. Assume day 0 is “Sunday”. Return None if the arguments to the function are not valid. Saturday Problem 7.5 Write the inverse function day_num which is given a day name, and returns its number. Once again, if this function is given an invalid argument, it should return None . elif number % 2 == 1 : result = 3 * number + 1 print ( result ) return result n = input ( "Type a number: " ) while n != 1 : n = collatz ( int ( n )) In [11]: day = int ( input ()) def day_name ( day ): days = [ "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" ] if day in range ( 7 ): return days [ day ] else : return "None" print ( day_name ( day )) In [32]: num = str ( input ( "Enter the day: " )) def day_num ( num ): Days = [ "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" ] if num not in Days : return None Day_no = Days . index ( num )
2 Problem 7.6 Using the two functions above, write a function day_add() that helps answer questions like ‘“Today is Wednesday. I leave for a vacation in 19 days time. What day will that be?”’ So the function must take a day name and a delta argument — the number of days to add— and should return the resulting day name: day_add("Monday", 4) should return "Friday" day_add("Tuesday", 0) should return "Tuesday" day_add("Tuesday", 14) should return "Tuesday" day_add("Sunday", 100) should return "Tuesday" Friday Tuesday Tuesday Tuesday Optional Ungraded Problem Can your day_add() function already work with negative deltas? For example, -1 would be yesterday, or -7 would be a week ago. If your function already works, explain why. If it does not work, make it work. Hint : Play with some cases of using the modulus function %. Specifically, explore what happens to x % 7 when x is negative. Submission Submit an ipynb file and a pdf file through Canvas return Day_no print ( day_num ( num )) In [33]: import math def day_add ( day , num ): lis = [ "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" ] if num == 7 or num % 7 ==0 or num==-7 or num==0: return day else : ind = lis . index ( day ) if num > 0 and num < 7 : ind += num return lis [ ind ] elif num > 7 : w = num // 7 mod = num - ( w * 7 ) return lis [ ind + mod ] print ( day_add ( "Monday" , 4 )) print ( day_add ( "Tuesday" , 0 )) print ( day_add ( "Tuesday" , 14 )) print ( day_add ( "Sunday" , 100 ))
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