Given the lists list1 and list2 that are of the same length, create a new list consisting of the first element of list1 followed by the first element of list2, followed by the second element of list1, followed by the second element of list2, and so on (in other words the new list should consist of alternating elements of list1 and list2). For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6], then the new list should contain [1, 4, 2, 5, 3, 6]. Associate the new list with the variable list3.
  • list3=[]for i in range(max(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • list3=[]for i in range(min(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
  • list3 = [] for i in range(max(len(list1),len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
Given that play_list has been defined to be a list, write a statement that sorts the list.
  • play_list=list(t)
  • play_list.sort()
  • play_list[0:3]="spam","eggs","vikings"
  • plist[-1] += 5
Given that play_list has been defined to be a list, write a statement that makes the first 3 elements of play_list be "spam", "eggs" and "vikings" (in that order).
  • play_list[k:j]
  • play_list[k:j+1]
  • del alist[0]
  • play_list[0:3]="spam","eggs","vikings"
Assume that the variable plist has been defined and refers to a list. Write a statement that assigns the next to last element of the list to x.
  • plist[-1]=-1
  • del alist[k]
  • x=plist[-2]
  • del alist[0]
Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data, compute the average temperature and assign it to a variable named avg_temp. Besides temps and avg_temp, you may use two other variables -- k and total.
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • k = 0total = 0while k < len(temps): total += temps[k] k += 1avg_temp = total / len(temps)
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
Assume that print_list is a function that expects one parameter, a list. The function prints the contents of the list; it does not return a value.Assume that inventory is a list, each of whose elements is an int.Write a statement that prints the contents of the list inventory by calling the function print_list.
  • plist[k-1]=22
  • play_list[0]=play_list[-1]*2
  • print_list(inventory)
  • plist[j] = plist[j+1]*2
Assume that a list of integers named salary_steps that contains exactly five elements has been defined.Write a statement that changes the value of the last element in the list to 160000.
  • salary_steps[-1]=160000
  • plist[0]+=10
  • play_list=list(t)
  • salary_steps[0]=30000
Write an expression that evaluates to the value of the first element of the tuple that t refers to.
  • t[0]
  • t=(42,56,7)
  • t=()
  • plist[-1]
Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders.
  • def is_reverse(a, b): return a == b[::-1]
  • lesser_offenders=worst_offenders[5:]
  • plist2=plist1[k:]
  • play_list[0:3]="spam","eggs","vikings"
We informally define the term corresponding element as follows:The first element and the last element of a list are corresponding elements.Similarly, the second element and the element just before the last element are corresponding elements.The third element and the element just before the element just before the last element are corresponding elements -- and so on.Given that the variable a is associated with a list, write an expression for the corresponding element of a[i].
  • plist[-1]
  • t[k]
  • plist[k]=15
  • a[-i-1]
Given that k refers to an int that is non-negative and that plist1 has been defined to be a list with at least k+1 elements, write a statement that defines plist2 to be a new list that contains all the elements from index k of plist1 and beyond. Do not modify plist1.
  • plist1+plist2
  • plist[k-1]=22
  • plist[-1]=-1
  • plist2=plist1[k:]
A geometric progression is a sequence of numbers in which each value (after the first) is obtained by multiplying the previous value in the sequence by a fixed value called the common ratio. For example the sequence 3, 12, 48, 192, ... is a geometric progression in which the common ratio is 4.Given the positive integer ratio greater than 1, and the non-negative integer n, create a list consisting of the geometric progression of numbers between (and including) 1 and n with a common ratio of ratio. For example, if ratio is 2 and n is 8, the list would be [1, 2, 4, 8].Associate the list with the variable geom_prog.
  • most_tickets = 0for k in parking_tickets: if most_tickets < k: most_tickets = k
  • term = 1geom_prog = []while(term<=n): geom_prog.append(term) term = term*ratio
  • fib = [0, 1]index = 1while fib[index] + fib[index - 1] <= n: fib.append(fib[index] + fib[index - 1]) index += 1
  • list3 = []for i in range(len(list1)): list3.append(list1[len(list1)-i-1]) list3.append(list2[len(list2)-i-1])
Assume that a variable named plist refers to a list with 12 elements, each of which is an int. Assume that the variable k refers to a value between 0 andWrite a statement that assigns 15 to the list element whose index is k.
  • play_list[0]=play_list[-1]*2
  • plist[-1]=-1
  • plist[k]=15
  • plist[k-1]=22
Assume that plist has been defined and is associated with a non-empty list. Write a statement that increases the first element of this list by 10.
  • plist[0]+=10
  • x=plist[-2]
  • plist[-1] += 5
  • plist[0]=3
Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .
  • plist[-1]
  • plist=['spam','eggs','vikings']
  • plist=[10,20,30,40,50,60,70,80,90,100]
  • plist.append(5)
Reverse the list associated with the variable words.
  • del alist[0]
  • a.reverse()
  • words.reverse()
  • plist.append(5)
Given the list lst of positive integers, associate the largest duplicated element with the variable max_dup. If the list contains no duplicates, associate -1 with max_dup
  • new_list = []for num in lst1: if num not in lst2: new_list.append(num)for num in lst2: if num not in lst1: new_list.append(num)new_list.sort()
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • unique_lst = [ ]max_dup = -1for i in range(len(lst)): if lst[i]>-1 and lst[i] in unique_lst: if lst[i] > max_dup: max_dup = lst[i] else: unique_lst += [lst[i]]
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
Given that a refers to a list, write the necessary code to reverse the elements of the list.
  • plist[-1] += 5
  • a.reverse()
  • t[0]
  • words.reverse()
Given that alist has been defined to be a list with at least 4 elements, write a statement that removes its 4th element.
  • plist1+plist2
  • 3 in plist
  • del alist[3]
  • del alist[k]
Write a statement that associates t with a tuple that contains the following elements: 42, 56, 7 .
  • t=()
  • x=plist[-2]
  • len(play_list)
  • t=(42,56,7)
Given that plist1 and plist2 both refer to lists, write an expression that evaluates to a list that is the concatenation of plist1 and plistDo not modify plist1 or plist2.
  • plist[0]=3
  • plist1+plist2
  • play_list[0:5]
  • play_list=list(t)
Given that L1 and L2 both refer to lists, write a statement that replaces the elements in L1 from index 5 through (and including) index 8 with all the elements of L2.
  • plist1+plist2
  • plist[0]=3
  • plist[-1]=-1
  • L1[5:9]=L2
You are given a variable named zipcode_list that has been defined and refers to a list of postal codes.Write some code that assigns True to duplicates if any two elements in the list have the same value, but that otherwise assigns False to duplicates. You may, if you wish, use two other variables, j and k.Use only j, k, zipcode_list, and duplicates.
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
  • duplicates = Falsefor k in range(len(zipcode_list)-1): if zipcode_list[k]==zipcode_list[k+1]: duplicates = True
  • most_tickets = 0for k in parking_tickets: if most_tickets < k: most_tickets = k
  • j = 0k = 0duplicates = Falsefor j in range(0, len(zipcode_list)): for k in range(0, len(zipcode_list)): if (zipcode_list[j] == zipcode_list[k] and j != k): duplicates = True
Write statement that defines plist to be a list of the following ten elements: 10, 20, 30, ..., 100 in that order.
  • tax_rates=[0.10,0.15,0.21,0.28,0.31]
  • denominations=[1,5,10,25,50,100]
  • t=(42,56,7)
  • plist=[10,20,30,40,50,60,70,80,90,100]
Given a variable t that is associated with a tuple whose elements are numbers, write some statements that use a while loop to count the number of times the first element of the tuple appears in the rest of the tuple, and associate that number with the variable repeats. Thus if the tuple contains (1,6,5,7,1,3,4,1), then repeats would be assigned the value 2 because after the first "1" there are two more "1"s.
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
  • list3 = []for i in range(len(list1)): list3.append(list1[len(list1)-i-1]) list3.append(list2[len(list2)-i-1])
  • if member_id in current_members: is_a_member = Trueelse: is_a_member = False
  • list3 = [] for i in range(max(len(list1),len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
Given that k refers to a non-negative int value and that t has been defined and refers to a tuple with at least k+1 elements, write an expression that evaluates to the kth element of t.
  • plist[0]=3
  • del alist[0]
  • t[k]
  • L1[5:9]=L2
Given that play_list has been defined to be a list, write an expression that evaluates to a new list containing the elements at index 0 through index 4 play_list. Do not modify play_list.
  • play_list[0:3]="spam","eggs","vikings"
  • play_list[0:5]
  • play_list[k:j]
  • play_list.sort()
Given the string, s, and the list, lst, associate the variable contains with True if every string in lst appears in s (and False otherwise). Thus, given the string Hello world and the list ["H", "wor", "o w"], contains would be associated with True.
  • contains = [k for k in lst if k not in s] == []
  • list3 = []for i in range(len(list1)): list3.append(list1[len(list1)-i-1]) list3.append(list2[len(list2)-i-1])
  • unique_lst = [ ]max_dup = -1for i in range(len(lst)): if lst[i]>-1 and lst[i] in unique_lst: if lst[i] > max_dup: max_dup = lst[i] else: unique_lst += [lst[i]]
  • has_dups = len(list1) > len(list(set(list1)))
Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of list1 followed by the last element of list2, followed by the second to last element of list1, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements of the reverse of list1 and list2). For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6], then the new list should contain [3, 6, 2, 5, 1, 4]. Associate the new list with the variable list3.
  • list3=[]for i in range(min(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
  • unique_lst = [ ]max_dup = -1for i in range(len(lst)): if lst[i]>-1 and lst[i] in unique_lst: if lst[i] > max_dup: max_dup = lst[i] else: unique_lst += [lst[i]]
  • list3 = []for i in range(len(list1)): list3.append(list1[len(list1)-i-1]) list3.append(list2[len(list2)-i-1])
Create a list named tax_rates, consisting of the following five elements: 0.10, 0.15, 0.21, 0.28, 0.31, in that order.
  • tax_rates=[0.10,0.15,0.21,0.28,0.31]
  • tmp = list(t)tmp.sort()t = tuple(tmp)
  • has_dups = len(list1) > len(list(set(list1)))
  • del alist[3]
Given a list named play_list, write an expression whose value is the length of play_list.
  • len(play_list)
  • play_list=list(t)
  • k in play_list
  • play_list.sort()
Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j+1 elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j of list play_list . Do not modify play_list .
  • del alist[0]
  • play_list[k:j+1]
  • plist2=plist1[k:]
  • plist[0]=3
Sort the list, lst (use the list sort method).
  • lst.sort()
  • words.reverse()
  • play_list.sort()
  • a.reverse()
Given that k refers to an int and that play_list has been defined to be a list, write a expression that evaluates to True if the value associated with k is an element of play_list.
  • play_list=list(t)
  • t=tuple(play_list)
  • plist2=plist1[k:]
  • k in play_list
Given that plist1 and plist2 both refer to lists, write a statement that defines plist3 as a new list that is the concatenation of plist1 and plistDo not modify plist1 or plist2.
  • k in play_list
  • plist[-1]=-1
  • tmp = list(t)tmp.sort()t = tuple(tmp)
  • plist3=plist1+plist2
Given that t refers to a tuple, write a statement that assigns the value of its first element to k.
  • t[0]
  • del alist[k]
  • t[k]
  • plist[0]
Given that alist has been defined to be a non-empty list (that is with at least one element), write a statement that removes its first element.
  • del alist[0]
  • x=plist[-2]
  • plist[-1] += 5
  • play_list[0:5]
An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6.Given the positive integer distance and the integers m and n, create a list consisting of the arithmetic progression between (and including) m and n with a distance of distance (if m > n, the list should be empty.) For example, if distance is 2, m is 5, and n is 12, the list would be [5, 7, 9, 11].Associate the list with the variable arith_prog.
  • fib = [0, 1]index = 1while fib[index] + fib[index - 1] <= n: fib.append(fib[index] + fib[index - 1]) index += 1
  • temporaryTriangulars=[]sumValue=0triangulars=[]for eachIndex in range(1, n+1): sumValue = sumValue + eachIndex if sumValue <=n: temporaryTriangulars.append(sumValue) else: breakfor eachIndex in range(0, len(temporaryTriangulars)): if temporaryTriangulars[eachIndex] >= m: triangulars.append(temporaryTriangulars[eachIndex])
  • arith_prog=list(range(m,n+1,distance))
  • term = 1geom_prog = []while(term<=n): geom_prog.append(term) term = term*ratio
An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers is the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6.Given the positive integer distance and the non-negative integer n, create a list consisting of the arithmetic progression between (and including) 1 and n with a distance of distance. For example, if distance is 2 and n is 8, the list would be [1, 3, 5, 7].Associate the list with the variable arith_prog.
  • arith_prog=list(range(1,n+1,distance))
  • has_dups = len(list1) > len(list(set(list1)))
  • temporaryTriangulars=[]sumValue=0triangulars=[]for eachIndex in range(1, n+1): sumValue = sumValue + eachIndex if sumValue <=n: temporaryTriangulars.append(sumValue) else: breakfor eachIndex in range(0, len(temporaryTriangulars)): if temporaryTriangulars[eachIndex] >= m: triangulars.append(temporaryTriangulars[eachIndex])
  • term = 1geom_prog = []while(term<=n): geom_prog.append(term) term = term*ratio
Given the lists list1 and list2, not necessarily of the same length, create a new list consisting of alternating elements of list1 and list2 (that is, the first element of list1 followed by the first element of list2 , followed by the second element of list1, followed by the second element of list2, and so on. Once the end of either list is reached, the remaining elements of the longer list is added to the end of the new list. For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6, 7, 8], then the new list should contain [1, 4, 2, 5, 3, 6, 7, 8]. Associate the new list with the variable list3.
  • list3=[]for i in range(max(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • unique_lst = [ ]max_dup = -1for i in range(len(lst)): if lst[i]>-1 and lst[i] in unique_lst: if lst[i] > max_dup: max_dup = lst[i] else: unique_lst += [lst[i]]
Given that t has been defined and refers to a tuple write a statement that associates play_list with a list containing the same elements as t.
  • play_list.sort()
  • play_list[k:j+1]
  • plist2=plist1[k:]
  • play_list=list(t)
In the following sequence, each number (except the first two) is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence.We speak of the i'th element of the sequence (starting at 0)-- thus the 0th element is 0, the 1st element is 1, the 2nd element is 1, the 3rd element is 2 and so on. Given the positive integer n, associate the nth value of the fibonacci sequence with the variable result. For example, if n is associated with the value 8 then result would be associated with 21.
  • term = 1geom_prog = []while(term<=n): geom_prog.append(term) term = term*ratio
  • def fibonacci(n): if (n<=1): return n else: return (fibonacci(n-1)+fibonacci(n-2)) n = 8result = fibonacci(n)
  • temporaryTriangulars=[]sumValue=0triangulars=[]for eachIndex in range(1, n+1): sumValue = sumValue + eachIndex if sumValue <=n: temporaryTriangulars.append(sumValue) else: breakfor eachIndex in range(0, len(temporaryTriangulars)): if temporaryTriangulars[eachIndex] >= m: triangulars.append(temporaryTriangulars[eachIndex])
  • fib = [0, 1]index = 1while fib[index] + fib[index - 1] <= n: fib.append(fib[index] + fib[index - 1]) index += 1
Given:a variable current_members that refers to a list, anda variable member_id that has been defined.Write some code that assigns True to a variable is_a_member if the value associated with member_id can be found in the list associated with current_members, but that otherwise assigns False to is_a_member. Use only current_members, member_id, and is_a_member.
  • if member_id in current_members: is_a_member = Trueelse: is_a_member = False
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
  • number_of_incompletes=len([k for k in incompletes if k==student_id])
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
Given that play_list has been defined and refers to a list, write a statement that associates t with a tuple containing the same elements as play_list.
  • t=tuple(play_list)
  • play_list[0:5]
  • play_list[0:3]="spam","eggs","vikings"
  • plist[-1]=-1
Given that plist refers to a non-empty list ,write a statement that assigns the int -1 to the last element of the list.
  • plist[-1] += 5
  • 3 in plist
  • plist[-1]
  • plist[-1]=-1
Given the list names, find the largest element in the list and swap it with the last element. For example, the list ["Carlton", "Quincy" "Adam", "Bernard"] would become ["Carlton", "Bernard", "Adam", "Quincy"]. Assume names is not empty.
  • unique_lst = [ ]max_dup = -1for i in range(len(lst)): if lst[i]>-1 and lst[i] in unique_lst: if lst[i] > max_dup: max_dup = lst[i] else: unique_lst += [lst[i]]
  • new_list = []for num in lst1: if num not in lst2: new_list.append(num)for num in lst2: if num not in lst1: new_list.append(num)new_list.sort()
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • list3=[]for i in range(max(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j-1 of list play_list . Do not modify play_list .
  • play_list.sort()
  • play_list[k:j]
  • plist2=plist1[k:]
  • del alist[3]
Given that a variable named plist has been defined and refers to a non-empty list, write a statement that associates its first element with 3.
  • del alist[0]
  • plist[0]
  • plist[k]=15
  • plist[0]=3
Assume that play_list refers to a non-empty list, and that all its elements refer to values of type int. Write a statement that associates a new value with the first element of the list. The new value should be equal to twice the value of the last element of the list.
  • play_list[0:3]="spam","eggs","vikings"
  • salary_steps[-1]=160000
  • play_list[0]=play_list[-1]*2
  • print_list(inventory)
Given that t has already been defined and refers to a tuple, write an expression whose value is the tuple's length.
  • len(t)
  • len(play_list)
  • tmp = list(t)tmp.sort()t = tuple(tmp)
  • play_list=list(t)
Write the definition of a function, is_reverse, whose two parameters are arrays of integers of equal size. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)
  • def is_reverse(a, b): return a == b[::-1]
  • contains = [k for k in lst if k not in s] == []
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • play_list[0]=play_list[-1]*2
Associate the sum of the non-negative values in the list numbers with the variable sum.
  • tax_rates=[0.10,0.15,0.21,0.28,0.31]
  • salary_steps[0]=30000
  • sum = 0for value in numbers: if value > 0: sum += value
  • has_dups = len(list1) > len(list(set(list1)))
Given:a variable named incompletes that refers to a list of student ids, anda variable student_idWrite some code that counts the number of times the value associated with student_id appears in the list associated with incompletes and assigns this value to number_of_incompletes. You may use, if you wish, an additional variable, k.You may use only k, incompletes, student_id, and number_of_incompletes.
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
  • if member_id in current_members: is_a_member = Trueelse: is_a_member = False
  • number_of_incompletes=len([k for k in incompletes if k==student_id])
  • lesser_offenders=worst_offenders[5:]
Given the lists list1 and list2, not necessarily of the same length, create a new list consisting of alternating elements of list1 and list2 (that is, the first element of list1 followed by the first element of list2, followed by the second element of list1, followed by the second element of list2, and so on. Once the end of either list is reached, no additional elements are added. For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6, 7, 8], then the new list should contain [1, 4, 2, 5, 3, 6]. Associate the new list with the variable list3.
  • list3 = [] for i in range(max(len(list1),len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • list3=[]for i in range(min(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lstFor example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort the new list.
  • repeats = 0if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1
  • list3 = [] for i in range(max(len(list1),len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])
  • index = names.index(max(names))names[index], names[-1] = names[-1], names[index]
  • new_list = []for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)
Assume that salary_steps refers to a non-empty list, write a statement that assigns the value 30000 to the first element of this list.
  • plist[0]+=10
  • plist[k]=15
  • play_list.sort()
  • salary_steps[0]=30000
0 h : 0 m : 1 s

Answered Not Answered Not Visited Correct : 0 Incorrect : 0