''' In this problem, you'll create a program that guesses a secret number! The program works as follows: you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search, the computer will guess the user's secret number! ''' high = 100 low = 0 uinput = '' preguess = 0 guess = 0 print("Please think of a number between 0 and 100!") #while loop while True: guess = int((high+low)/2) # if guess == preguess: break print("Is your secret number " + str(guess) + "?") uinput = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.") if uinput == "h" or uinput == "l" or uinput == "c": if uinput == "h": high = guess if uinput == "l": low = guess if uinput == "c": break else: print("Sorry, I did not understand your input.") continue preguess = guess print("Game over. Your secret number was: "+str(guess)) #default to y = 5 def foo(x, y = 5): def bar(x): return x + 1 return bar(y * 2) foo(3) ''' Write a function recurPower(base, exp) which computes by recursively calling itself to solve a smaller version of the same problem, and then multiplying the result by base to solve the initial problem. ''' def recurPower(base, exp): ''' base: int or float. exp: int >= 0 returns: int or float, base^exp ''' if exp == 0: return 1 elif exp == 1: return base else: return base * recurPower(base, exp - 1) #get the common divisor using recursion def gcdRecur(a, b):#stub ''' a, b: positive integers returns: a positive integer, the greatest common divisor of a & b. ''' if b == 0: return a else: return gcdRecur(b, a % b) ''' We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you're looking for (the "test character"). If they are the same, we are done - we've found the character we're looking for! If they're not the same, check if the test character is "smaller" than the middle character. If so, we need only consider the lower half of the string; otherwise, we only consider the upper half of the string. (Note that you can compare characters using Python's < function.) Implement the function isIn(char, aStr) which implements the above idea recursively to test if char is in aStr. char will be a single character and aStr will be a string that is in alphabetical order. The function should return a boolean value. ''' def isIn(char, aStr):#stub ''' char: a single character aStr: an alphabetized string returns: True if char is in aStr; False otherwise ''' if len(aStr) <= 1: return char == aStr elif aStr[int(len(aStr)/2)] > char: return isIn(char, aStr[0:int(len(aStr)/2)]) else: return isIn(char, aStr[int(len(aStr)/2):len(aStr)]) #At the end of 12 months, print out the remaining balance. def muns(balance, annualInterestRate, monthlyPaymentRate): monthlyInterestRate = annualInterestRate/12 unpaidbal = balance minpay = balance * monthlyPaymentRate for month in range(1, 13, 1): minpay = unpaidbal * monthlyPaymentRate unpaidbal -= minpay unpaidbal += (monthlyInterestRate * unpaidbal) unpaidbal = round(unpaidbal,2) return unpaidbal print("Remaining balance: "+ str(muns(balance, annualInterestRate, monthlyPaymentRate))) ''' Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. The monthly payment must be a multiple of $10 and is the same for all months. ''' def lowpay(balance, annualInterestRate): monthlyPayment = 0 monthlyInterestRate = annualInterestRate/12 unpaidbal = balance -10 while unpaidbal > 0: unpaidbal = balance monthlyPayment += 10 for month in range(1, 13, 1): unpaidbal -= monthlyPayment unpaidbal += (monthlyInterestRate * unpaidbal) unpaidbal = round(unpaidbal,2) return monthlyPayment print("Lowest Payment: "+str(lowpay(balance, annualInterestRate)))