midterm_1_uofzona

.py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

244

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

16

Uploaded by MajorTankBarracuda45

Report
#ODD ALL THE ODD NUMBER FROM A LIST def add_odd(list): sum = 0 for i in list: if i % 2 != 0: sum += i return sum list = [2,3,4,17,6] print(add_odd(list)) #-------------------------------------------------- #ODD THE EVEN DIGITS OF A NUMBER def even_sum(n): sum = 0 for i in str(n): if int(i) % 2 == 0: sum += int(i) return sum print(even_sum(4456)) #-------------------------------------------------- #RETURN THE LARGEST NUMBER AMONG 3 NUMBERS def find_max(a,b,c): max = 0 if a >= b and a >= c: max = a elif b > c: # b > a and c > a max = b else: max = c return max print(find_max(0,0,1)) #--------------------------------------------------- #COMPUTE AND RETURN THE SUM OF SQUARE UP TO THE GIVEN NUMBER N def sum_of_squares(n): sum = 0
for i in range(n): sum += (i**2) return sum print(sum_of_squares(100)) #---------------------------------------------------- #GIVEN A STRING, RETURN TRUE IF ITS PURE, OTHERWISE RETURN FALSE def is_pure(str1): alpha = "abcdefghijklmnopqrstuvwxyz" str2 = str1.lower() print(str2) for i in str2: if (i in alpha) == False: print(i) return False break for i in str2: if (i in alpha) == True: print(i) return True print(is_pure("GhJifJ")) #----------------------------------------------------- #COMPUTE THE AVERAGE OF NUMBERS IN A STRING def average(str1): sum = 0 count = 0 for i in str1: if i.isnumeric(): sum += int(i) count += 1 num = sum / count return num print(average("2gos6nds4")) #------------------------------------------------------ #RETURN THE GREATEST COMMON DEVISOR AMONG 3 NUMBERS def gcd(a,b,c): lowest = 0 if (a <= b) and (a <= c):
lowest = a elif b <= c: lowest = b else: lowest = c divisors = [] for x in range(1,lowest+1): if ((a % x == 0) and (b % x == 0) and (c % x == 0)): divisors.append(x) return max(divisors) print(gcd(12,16,14)) #------------------------------------------------------- #RETURN BASE 12 NUMBER OF A DECIMAL NUMBER def base12(x): result = " " while (x > 0): r = x % 12 if (x == 10): result = "A" + result elif (x == 11): result = "B" + result else: result = str(r) + result x = x // 12 return result print(base12(912)) #-------------------------------------------------------- #SEPERATE THE NUMBERS IN THE STRING AND ODD THEM def sum_of_num(str1): sum = 0 for i in str(str1): if i.isnumeric(): sum += int(i) return sum print(sum_of_num("asdk5ksd8dsk3"))
#-------------------------------------------------- # SUM OF ALTERNATIVE NUMBERS def num_sum(str1): sum = 0 list = str1.split(",") count = 0 for i in list: count += 1 if (count % 2 != 0): sum = float(i) + sum elif (count % 2 == 0): sum = sum - float(i) return sum print(num_sum("65.21,7.3,44,12.1,33.2")) print(num_sum("65.21,7.3,44,12.1,33.2,1,0")) print(num_sum("65.21,7.3,44,12.1,33.2,1,0,2.0")) print(num_sum("65.21,7.3,44,12.1,33.2,1,0,2.0,5.0")) # -------------------------------------------------- # SMALLEST NUMBER def smallest(a, b, c): min = 0 if a < b and a < c: min = a elif b < c: min = b else: min = c return min print(smallest(8, 8, 10)) # -------------------------------------------------- # REMOVE THE NUMBERS FROM THE STRING def number(str1): str2 = "" for i in str1:
if not (i.isnumeric()): str2 = str2 + str(i) return str2 print(number("h3y2jda")) # -------------------------------------------------- # LARGEST CUBE BEFORE THE GIVEN NUMBER def cube(n): largest = 0 x = 1 while (x ** 3) <= n: if (x ** 3) > largest: largest = (x ** 3) x += 1 return largest print(cube(100)) # -------------------------------------------------- # GIVE THE NO. OF PALINDROME NUMBERS SMALLER THAN 'N' def palindrome(n): count = 0 for i in range(n): # going from 0 to n-1 counting 0 as a palindrome if str(i)[0] == str(i)[len(str(i)) - 1]: count += 1 return count print(palindrome(44)) # -------------------------------------------------- # GIVEN A NUMBER PRINT TRUE IF THE NUMBER IS PERFECT AS # N FOR WHICH THE SUM OF DIVISORS IS 2N, N = 6 IS A PERFECT NUMBER SINCE WE HAVE # 1 + 2 + 3 + 6 = 12.
def num(n): sum = 0 for i in range(1, n + 1): if (n % i) == 0: sum += i if sum == (2 * n): return True else: return False print(num(6)) # -------------------------------------------------- #Write a program that examines three variables x , y , and z, and prints the largest odd number #among them. If none of them are odd, it should print a message to that effect. x = 888 y = 653 z = 34 max = 0 if (x >= 0) and (x % 2 == 1): max = x elif (y >= 0) and (y % 2 == 1): max = y elif (z >= 0) and (z % 2 == 1): max = z else: print("No numbers are odd") print(max) #-------------------------------------------------- #Write a program that asks the user to input 10 integers, and then prints the largest odd number #that was entered. If no odd number was entered, it should print a message to that effect. This is a #more general version of Question 1. For example of the integers were: 10, 9, 7, 12, 2, 5, 15, 100, 90, 60, #then the program would print 15 as the largest odd number.
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