# CSC 15 Basic Practice Problems # Sample solutions will be given to you when you turn in your attempts. # You will be given credit for a problem as long as you showed serious # attempt at solving it yourself. Test your solutions in Python (make # sure you can call the function too). ####################################### #0a Write code to construct a list containing all the numbers that are # evenly divisible by 8 from 0 to 10000, i.e., the list # [0,8,16,24,32,40,48,56,64, ... 10000]. Hint: first line is M = [] #0b. Write a function 'product' that multiplies all numbers in an array. # For example, product([3,2,5]) should return 30. Hint: the product # of the empty array is 1. hint: don't multiply by i, multiply by A[i]!! # Think clearly: are you refering to the index or the element. def prod(A): # A is the list passed in when the function is called. #1. Write a function 'doubles' that takes a list as parameter and constructs # a new list with two copies of each element of the parameter list. # For example, doubles([2,4,6,8]) should return [2,2,4,4,6,6,8,8]. Hint: # inside the loop, do two appends. #2. Write a function to reverse a list. That is, reverse([2,4,6,8]) should # construct and return the list [8,6,4,2] #3a. Write a function h that returns True iff there is a number greater than # 100 in a list. For example, h([4,6,1,109,3]) should return True. It # should return False otherwise. #3b Write a function h2 that returns True iff there is at least 2 numbers # in the given list that's greater than 100. For example, h2([4,6,1,109,3]) # should return False, and h2([45,180,21,346,12]) should return True #4 Write a nested loop that prints X X X X X X X X X X X X X X X # The outer loop should run 5 times. The inner loop prints each line. #5. Write a function 'odds' that takes an array as an argument and returns # and array containing all the odd numbers of the given array. For # example, odds([4,3,6,8,1,9]) should return [3,1,9], which are all the # odd numbers of the given (parameter) list. hint: start with an empty # list, then append all the odd numbers you find to the list. A number # is odd if its remainder after dividing by 2 is 1. #6 write a function sublist(A,B) that returns True iff every element of # A also appears in B. For example, sublist([2,4,6], [1,2,3,4,5,6,7]) # should return True. (implicitly returns False if A is not a sublist of B) # hint: the expression (x in B) is True iff x occurs in array B #7 (similar) write a function intersection(A,B) that returns the # intersection of the lists A, B. That is, it should return a list M # that contains all elements that are in both A and B. Duplicates are # fine. For example, intersection([2,4,6,8], [1,2,3,4,5]) should # return [2,4], which is the intersection of the two lists. Hint: # you don't need a nested loop. All you have to do is to go through # the list A and look for the elements that are also in B. #8 write a function 'occurs(x,A)' that returns the first index in A where # x is found. If x does not exist in A, then the function should return # -1 # write a function count that returns the number of times x occurs in A. # for example, count(3,[4,3,7,3,1]) should return 2 # # (harder) a permutation is a list of length n that contains the numbers # 0 to n-1. For example, [4,2,0,1,3] is a permutation of length 5. It # permutes the numbers 0 to 4. That is, 4 becomes 0, 2 becomes 1, 0 becomes 2, # etc. Now we can use a permutation to permute or "scramble" a string of # the same length, such as "hello" by applying the permutation to each # character. Applying the above permutation list to "hello" will result # in "olhel": #hello #01234 #olhel