06-04 list-addition/retrieval/deletion

¡Supera tus tareas y exámenes ahora con Quizwiz!

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]. Assign the list to the variable arith_prog.

Solution 1 arith_prog = [] for i in range(1, n+1, distance): [Tab Key]arith_prog.append(i) Solution 2 arith_prog = [i for i in range(1, n+1, distance)]

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]. Assign the list to the variable arith_prog.

Solution 1 arith_prog = [] for i in range(m, n+1, distance): [Tab Key]arith_prog.append(i) Solution 2 arith_prog = [i for i in range(m, n+1, distance)]

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]

Given that alist has been defined to be a list with at least 4 elements, write a statement that removes its 4th element.

del alist[3]

Given that k contains a non-negative integer and that alist has been defined to be a list with at least k+1 elements, write a statement that removes the element at index k.

del alist[k]

In the following sequence, each number (except the first two) is the sum of the previous two number: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence. Given the positive integer n create a list consisting of the portion of the Fibonacci sequence less than or equal to n. For example, if n is 6, then the list would be [0, 1, 1, 2, 3, 5] and if n is 1, then the list would be [0, 1, 1]. Assign the list to the variable fib.

fib = [0, 1] i = 1 lastI = 0 while i+lastI <= n: [Tab Key]lastI, i = i, lastI + i [Tab Key]fib.append(i)

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]. Assign the new list to the variable list3.

list3 = [] for i in range(max(len(list1), len(list2))): [Tab Key]if i < len(list1): list3.append(list1[i]) [Tab Key]if i < len(list2): list3.append(list2[i])

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]. Assign the new list with the variable list3.

list3 = [] for i in range(min(len(list1), len(list2))): [Tab Key]list3.append(list1[i]) [Tab Key]list3.append(list2[i])

In the following sequence, each number (except the first two) is the sum of the previous two number: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence. Given the positive integers m and n (with m < n) create a list consisting of the portion of the Fibonacci sequence greater than or equal to m and less than or equal to n. For example, if m is 3 and n is 6, then the list would be [3, 5] and if m is 2 and n is 20, then the list would be [2, 3, 5, 8, 13]. Assign the list to the variable fib.

fib = [] if m <= 0 and n >= 0: fib.append(0) if m <= 1 and n >= 1: fib.append(1) i = 1 lastI = 0 while i+lastI <= n: [Tab Key]lastI, i = i, lastI + i [Tab Key]if i >= m: [Tab Key][Tab Key]fib.append(i)

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 being 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]. Assign the list to the variable geom_prog.

geom_prog = [] i = 1 while i <= n: [Tab Key]geom_prog.append(i) [Tab Key]i *= ratio

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]. Assign the new list to the variable list3.

list3 = [] for i in range(len(list1)): [Tab Key]list3.append(list1[i]) [Tab Key]list3.append(list2[i])

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]. Assign the new list to the variable list3.

list3 = [] for i in range(len(list1)-1, -1, -1): [Tab Key]list3.append(list1[i]) [Tab Key]list3.append(list2[i])

Given a variable named plist that contains a list, write a statement that adds another element, 5 to the end of the list.

plist.append(5)


Conjuntos de estudio relacionados

Which of the following common components of culture is the one that "gives away" or tells us so much more about a group's culture than any of the others? Question options: group identity symbols values language norms

View Set

Rosenthal NCE Practice Questions

View Set

Ch.27: Safety, security and emergency preparedness

View Set

BSG Quiz 1, BUS 490 BSG Simulation Quiz 1, BSG Quiz 1, BSG Chapter 1 Quiz

View Set

Cell Junctions, Cell Adhesion, and the Extracellular Matrix

View Set

History 202 Final Exam UTM Barber

View Set

Hill & Walton: A Survey of the Old Testament: Key Ideas Final Exam OT 1

View Set