''' Reverse the digits of a non-negative 2 digit int parameters: number (int) - 2 digit int Returns: newNumber (int) reverses number ''' onesDigit = number % 10 tensDigit = number // 10 newNumber = onesDigit * 10 + tensDigit return newNumber assertEqual(reverseDigits(37), 73) ## if staments if 5 < 8: print("truuuuuu") age = int(input('how old are you? ')) if age < 21: print('im sorry you cannot be served') else: print('what would u like to drink') def can_vote(age): if age >= 18: decision = 'yes'
# Searching # linear search from cisc106_34 import assertEqual def linear_search(int_list, target): # serach list for target value for i in range(len(int_list)): if int_list[i] == target: return i # target not found, return None return None assertEqual(linear_search(['a', 'b', 'c', 'd', 'e', 'f'], 'c'), 2) assertEqual(linear_search([0, 1, 2, 3, 4, 6, 7, 8, 9], 7), 6) assertEqual(linear_search(['a', 'b', 'c', 'd', 'e', 'f'], 'z'), None) # binary search def binary_search(int_list, target): # initialize low, mid, and high low = 0 high = len(int_list) - 1 mid = (low + high) // 2 # loop until low and high cross, or until target is found while low <= high: if int_list[mid] == target: return mid # return index if target is found
from cisc106_34 import assertEqual def convert_F_to_C(fahrenheit): ''' This function converts temperature from °F to °C. Paameters: fahrenheit (number) - temperature in °F Returns: celsius (float)- temperature converted to °C ''' # Convert from °F to °C celsius = ((fahrenheit - 32.0) * 5.0 / 9.0) return celsius assertEqual(convert_F_to_C(212), 100) assertEqual(convert_F_to_C(32), 0) assertEqual(convert_F_to_C(-40), -40) assertEqual(convert_F_to_C(176), 80)
''' Takes a string and returns that string encoded in md5 hash. Parameters: my_string (string) - string to be converted Returns: hex_string (bytes) - converted version of string ''' import hashlib #transforms string into bytes hash_object = hashlib.md5(my_string.encode()) #encodes the string hex_string = hash_object.hexdigest() return (hex_string) assertEqual(compute_md5_hash('hi'), '49f68a5c8493ec2c0bf489821c21fc3b') assertEqual(compute_md5_hash('drew'), 'b2dd08a69dcdac5a20a7b90b5c4b759f') assertEqual(compute_md5_hash('bye'), 'bfa99df33b137bc8fb5f5407d7e58da8') #Problem 2 print() print('Problem 2') def convert_to_base64(my_string): ''' Takes a string and converts it to b64 encoding. Parameters: my_string (string) - string to be converted Returns: encoded_string (bytes) - transformed version of string
# Continuing from last time from cisc106_34 import assertEqual def can_vote(age): if age < 18: decision = 'no' else: decision = 'yes' return decision # Note that the fact that these assertEqual() tests succeed indicates that it # is possible to compare strings. assertEqual(can_vote(17), 'no') assertEqual(can_vote(21), 'yes') assertEqual(can_vote(18), 'yes') # Strings are compared letter by letter just like comparing words to put them # in alphabetical order. Each character has a numerical value associated with # which is used to do the comparison. The values are assigned so that you get # the expected result - a < b < c < ... < z, and similarly A < B < C < ... < Z. # However, note that all uppercase letters are less than every lower case # letter - Z < a. Also, every digit is lower than every letter - 9 < A. name_1 = 'Mohan' name_2 = 'Morgan' # Change to Martin and output order changes. Note that # using martin rather than martin results in Mohan # being printed first because M < m. if name_1 < name_2:
#method 1 ##import math ##print(math.sqrt(49)) ##print(math.acos(0.49)) ##print(math.log2(49)) #log base 2 ##print(math.log10(49)) #log base 10 ##print(math.log(49)) #natural log ###method 2 ##from math import sqrt, log ##print(sqrt(49)) ##print(log(49)) #natural log ## ###method 3 ##from math import * ##print(sqrt(49)) ##print(log(49)) from cisc106_34 import assertEqual def convertFtoC(fahrenheit): c = (fahrenheit - 32.) *5.0/9.0 return c print(convertFtoC(32)) assertEqual(convertFtoC(-40), -40.0)
print('-40 °F is', convert_F_to_C(-40), '°C') print('32 °F is', convert_F_to_C(32), '°C') print('212 °F is', convert_F_to_C(212), '°C') # Testing using assertEqual() from cisc106_34 import assertEqual def convert_F_to_C(fahrenheit): celsius = (fahrenheit - 32.0) * 5.0 / 9.0 return celsius assertEqual(convert_F_to_C(-40), -40) assertEqual(convert_F_to_C(32), 0) assertEqual(convert_F_to_C(212), 100) assertEqual(convert_F_to_C(41), 6) # example of a failure # What about rounding? assertEqual(convert_F_to_C(75), 23.88888888888889) # Use round() function print(2 / 3) print(round(2 / 3, 2)) print(round(2 / 3, 4)) print(round(2 / 3, 12)) # Using round() with assertEqual() assertEqual(round(convert_F_to_C(75), 2), 23.89)
elif (int(credit_score) >= (590) and int(credit_score) < (700)) and ((float(account_balance)) < (.25 * float(loan_amount))): decision = "No" elif (float(current_salary)) < ( (1 / 3) * (float(loan_amount) - float(account_balance))): decision = "No" elif (str(last_name) == "Doe") and (float(non_cash_assets) < 750000): decision = "No" elif (float(account_balance)) > (float(loan_amount)): decision = "Yes" else: decision = "Maybe" return (decision) assertEqual(mortgage_approval(100, 250, 5, 500, 600, "Hi"), 'No') #Testing account_balance < loan_amount * .15 when True assertEqual(mortgage_approval(5, 250, 500, 500, 580, "Hi"), 'No') #Testing credit_score < (590) when True assertEqual(mortgage_approval(4000, 250, 500, 500, 600, "Hi"), 'No') #Testing (credit_score >= (590) and credit_score < (700)) ##and (account_balance < (.25 * loan_amount)) when True assertEqual(mortgage_approval(4000, 250, 500, 500, 600, "Hi"), 'No') #Testing current_salary < ##((1/3) * (loan_amount - account_balance)) when True assertEqual(mortgage_approval(100, 250, 500, 500, 600, "Doe"), 'No') #Testing (last_name == "Doe") and ##(non_cash_assets < 750000) when True assertEqual(mortgage_approval(5, 600, 500, 500, 600, "Hi"), 'Yes') #Testing account_balance > loan_amount when True assertEqual(mortgage_approval(700, 600, 500, 500, 800, "Hi"), 'Maybe') #Testing else