Python Functions def count_odd_divisors(n): Given a positive int, find out how m
ID: 3871245 • Letter: P
Question
Python Functions def count_odd_divisors(n): Given a positive int, find out how many odd numbers are divisors. o o o o o Assume: n is a positive integer. Restrictions: no extra restrictions. count-odd-divisors(15) count-odd-divisors(8) count odd-divisors (12) 4 1 2 # divisors : 1,3, 5,15. 4 are odd. # divisors : 1,2,4,8. 1 is odd. # divisors : 1,2,3,4,6,12. 2 are odd. def inchworm(up, down, height): An inchworm is stuck in a well height feet deep. Starting at the bottom, it climbs up up inches, then slides down down inches. How many upwards climbs does it take for the inchworm to reach the top and escape? (hint: if the top is reached, we don't slide back down.!) Assume: up, down, and height are all non-negative int values. Restrictions: you must use a loop in your implementation to receive credit. inchworm(2,1,18) inchworm(10,100,8) inchworm(8,6,20) o o o 9 o def count_max(xs): Given a list of numbers, how many times does the largest value occur? If the list i empty, return zero. o o o o Assume: xs is a sequence of any length, and all values are numbers Restrictions: remember, you may not call count(),max() or other related built-in functions count max([5,1,5,2,5,3,5]) count_max([7,7,7,81) count-max([]) # largest is s; it occurs 4 times. # largest is 8; it occurs once. # no max value; it occurs zero times. 4 1Explanation / Answer
def count_odd_divisors(n):
count = 0
for i in range(n):
if (i+1) % 2 != 0:
if n % (i+1) == 0:
count = count +1
return count
def inchworm(up,down,height):
reach = 0
count = 0
while reach < height: #This is the basic loop.It checks whether we have reached the height or not.
reach = reach + up # so first reach is updated with up count . if we reached, we don't have to
count = count + 1 #consider down. But if we haven't reached, we need to come down by "down"
if reach >= height:
return count
reach = reach - down
return count
def count_max(xs):
max = -100000
for i in range(len(xs)): # loop to get the max of the list. first max is assigned 0 and then
if max <= xs[i]: #updated as we encounter bigger and bigger number.
max = xs[i]
count = 0
for i in range(len(xs)): # loop to check how many times the max has occured.
if max == xs[i]: #every element is compared with the max and count increases
count = count +1 # accordingly.
return count
print(count_odd_divisors(15))
print(count_odd_divisors(8))
print(count_odd_divisors(12))
print(inchworm(2,1,10))
print(inchworm(10,100,8))
print(inchworm(8,6,20))
print(count_max([5,1,5,2,5,3,5]))
print(count_max([7,7,7,8]))
print(count_max([]))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.