Write algorithms made up of instructions of the type shown above to draw each of the following pictures substituting in appropriate values for x, y, s and r in each instruction.
For each algorithm, the result of your last algorithmic step should be the final answer. NOTE: In the second computation, you should write only four instructions to generate the answer.
def lcm(x,y): p = x * y while y != 0: temp = y y = x % y x = temp q = p / x return q
Show how this function computes lcm(36,48) by creating a table that shows the values of each variable at the end of each iteration of the loop. We have started the table for you with the initial values of the variables before the first iteration of the loop:
===================================== x y temp p q ===================================== 36 48 --- 1728 --- =====================================
def lcm_helper(x, y, a, b): if (a == b): return a elif (a < b): return lcm_helper(x, y, a+x, b) else: return lcm_helper(x, y, a, b+y) def lcm_recursive(x, y): return lcm_helper(x, y, x, y)
Show how lcm_helper(36, 48) is computed recursively here by listing the chain of recursive calls that are made until an answer is found. We have started the chain for you below:
lcm_recursive(36, 48) --> lcm_helper(36, 48, 36, 48) --> lcm_helper(36, 48, 72, 48) -->
1. Set total equal to 0. 2. Set n equal to the length of the list. 3. Set i equal to 0. 4. While i is less than n, do the following: a. Add list[i] to total. b. Add 1 to i. 5. Return total / n.
def compute_average(list):
def compute_average2(list):
def mystery1(codes): for item in codes: if item % 1111 == 0: print(item)
def mystery2(codes): return codes.count(codes[len(codes)-1])
> s = "Pirates" => "Pirates" >> len(s) => 7
Let dictionary represent an list of words stored as strings. For example:
dictionary = ["buick", "cadillac", "chevrolet", "gmc", "olds", "pontiac", "saturn"]
def print_second_word(dictionary):
def remove_words_over_4(dictionary):
def sift(list,k): # remove all multiples of k from list - UPDATED index = 0 while index < len(list): if list[index] % k == 0: list.remove(list[index]) else: index = index + 1 return list def sieve(n): numlist = [] for i in range(2,n+1): numlist.append(i) primes = [] while len(numlist) > 0: primes.append(numlist[0]) lastprime = primes[len(primes)-1] numlist = sift(numlist, lastprime) return primes
def sieve(n): numlist = [] for i in range(2,n+1): numlist.append(i) primes = [] while numlist[0] <= math.sqrt(n): primes.append(numlist[0]) lastprime = primes[len(primes)-1] numlist = sift(numlist, lastprime) return primes + numlist
The return statement returns a list containing the values in primes followed by the values in numlist using concatenation. Briefly explain why we need to do this.