MyProgrammingLab Starting out with Python Ch.7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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.

L1[5:9]=L2

Given: a variable current_members that refers to a list, and a 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 = True else: is_a_member = False

Given that t has been defined and refers to a tuple write some statements that associate with t a new tuple containing the same elements as the original but in sorted order.

tmp = list(t) tmp.sort() t = tuple(tmp)

A triangular number is a number that is the sum of the integers from 1 to some integer n. Thus 1 is a triangular number because it's the sum of the integers from 1 to 1; 6 is a triangular number because it's 1+2+3=6. Given the non-negative integer n, create a list of the first n triangular numbers. Thus is n was 5, the list would be: [1, 3, 6, 10, 15]. Associate the list with the variable triangulars.

triangulars = [] sumofIntegers = 0 addNumber = 1 for each in range(0, n): sumofIntegers = sumofIntegers + addNumber triangulars.append(sumofIntegers) addNumber = addNumber + 1

Given that plist has been defined to be a list, write an expression that evaluates to True if 3 is an element of plist.

3 in plist

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]. Associate the list with the variable fib.

a = 0 b = 1 fib = [] while a <= n: if a>=m: fib.append(a) temp = a a = b b = temp + b

Given that a refers to a list, write the necessary code to reverse the elements of the list.

a.reverse()

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].

a[-i-1]

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] == []

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]

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 refers to a non-negative int 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]. Associate the list with the variable fib.

fib = [0, 1] index = 1 while fib[index] + fib[index - 1] <= n: fib.append(fib[index] + fib[index - 1]) index += 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.

index = names.index(max(names)) names[index], names[-1] = names[-1], names[index]

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.

j = 0 k = 0 duplicates = False for 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

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.

k = 0 total = 0 while k < len(temps): total += temps[k] k += 1 avg_temp = total / len(temps)

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.

k in play_list

Given a list named play_list, write an expression whose value is the length of play_list.

len(play_list)

Given that t has already been defined and refers to a tuple, write an expression whose value is the tuple's length.

len(t)

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.

lesser_offenders=worst_offenders[5:]

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(len(list1)): list3.append(list1[len(list1)-i-1]) list3.append(list2[len(list2)-i-1])

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])

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(min(len(list1), len(list2))): if i < len(list1): list3.append(list1[i]) if i < len(list2): list3.append(list2[i])

Sort the list, lst (use the list sort method).

lst.sort()

A list named parking_tickets has been defined to be the number of parking tickets given out by the city police each day since the beginning of the current year. (Thus, the first element of the list contains the number of tickets given on January 1; the last element contains the number of tickets given today.) Write some code that associates most_tickets with the largest value found in parking_tickets. You may, if you wish, use one additional variable, k.

most_tickets = 0 for k in parking_tickets: if most_tickets < k: most_tickets = k

Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that do not appear in lst2 together with all the elements of lst2 that do not appear in lst1. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4, 1, 5], then the new list would be [1, 1, 3, 5, 6]. Note that duplicate elements 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.

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()

Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For 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.

new_list = [] for val1 in lst1: for val2 in lst2: if val1 == val2: new_list.append(val1)

Write statement that defines plist to be a list of the following ten elements: 10, 20, 30, ..., 100 in that order.

plist=[10,20,30,40,50,60,70,80,90,100]

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

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.

plist[0]=3

Assume that plist is associated with a list that has 12 elements. Assume further that k refers to an int between 2 and 8. Assign 9 to the element just after the element in plist whose index is k .

plist[k+1]=9

Assume that a variable named plist has been defined and is associated with a list that consists of 12 elements. Assume further that k refers to an int between 2 and 8. Assign 22 to the element just before the element in plist whose index is k .

plist[k-1]=22

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 and 6. Write a statement that assigns 15 to the list element whose index is k.

plist[k]=15

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.

print_list(inventory)

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 = 0 if len(t) > 0: first = t[0] index = 1 while index < len(t): if t[index] == first: repeats += 1 index += 1

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

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.

salary_steps[0]=30000

Associate the sum of the non-negative values in the list numbers with the variable sum.

sum = 0 for value in numbers: if value > 0: sum += value

Write a statement that associates t with the empty tuple.

t=()

Write a statement that associates t with a tuple that contains the following elements: 42, 56, 7 .

t=(42,56,7)

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)

Given that t refers to a tuple, write a statement that assigns the value of its first element to k.

t[0]

Write an expression that evaluates to the value of the first element of the tuple that t refers to.

t[0]

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

unique_lst = [ ] max_dup = -1 for 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]]

Reverse the list associated with the variable words.

words.reverse()

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.

x=plist[-2]

Associate True with the variable has_dups if the list list1 has any duplicate elements (that is if any element appears more than once), and False otherwise.

has_dups = len(list1) > len(list(set(list1)))

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.

term = 1 geom_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))

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.

arith_prog=list(range(m,n+1,distance))

The Magic Square is a grid with 3 rows and 3 columns with the following properties: • The grid contains every number from 1 to 9. • The sum of each row, each column, and each diagonal all add up to the same number. This is an example of a Magic Square: 4 9 2 3 5 7 8 1 6 You can simulate a 3x3 grid using a two-dimensional list. For example, the list corresponding to the grid above would be: [[4, 9, 2], [3, 5, 7], [8, 1, 6]] Write a function that accepts a two-dimensional list as an argument and returns whether the list represents a Magic Square (either True or False). Create a program that tests the function on the following two-dimensional lists and prints out the results each on a separate line: [[4, 9, 2], [3, 5, 7], [8, 1, 6]] [[2, 7, 6], [9, 5, 1], [4, 3, 8]] [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[4, 9, 2], [3, 5, 5], [8, 1, 6]]

def Sq_magic(inp): #declare the size size = len(inp[0]) #initialise the list lst = [] #for loop for vertical angle for c in range(size): #append the values lst.append(sum(r[c] for r in inp)) #set the horizontal value lst.extend([sum (lines) for lines in inp]) #set the diagonal value rst = 0 #for loop to find the result for i in range(0,size): rst +=inp[i][i] #append the values lst.append(rst) d_rst = 0 #for loop to find the result for i in range(size-1,-1,-1): d_rst +=inp[i][i] #append the values lst.append(d_rst) #check the length of the list if len(set(lst))>1: #return false return False return True #function call that accepts two dimensional list print(Sq_magic([[4,9,2], [3,5,7], [8,1,6]])) print(Sq_magic([[2,7,6], [9,5,1], [4,3,8]])) print(Sq_magic([[1,2,3], [4,5,6], [7,8,9]])) print(Sq_magic([[4,9,2], [3,5,5], [8,1,6]]))

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.

def fibonacci(n): if (n<=1): return n else: return (fibonacci(n-1)+fibonacci(n-2)) n = 8 result = fibonacci(n)

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]

Write a statement that defines the variable denominations, and associates it with a list consisting of the following six elements: 1, 5, 10, 25, 50, 100, in that order.

denominations=[1,5,10,25,50,100]

You are given a variable zipcode_list that refers to a list. Write some code that assigns True to a variable duplicates if there are two adjacent elements in the list that have the same value, but that otherwise assigns False to duplicates otherwise. In order to accomplish this, you may, if you wish, use one other variable, k. Use only k, zipcode_list, and duplicates.

duplicates = False for k in range(len(zipcode_list)-1): if zipcode_list[k]==zipcode_list[k+1]: duplicates = True

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])

Given: a variable named incompletes that refers to a list of student ids, and a variable student_id Write 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.

number_of_incompletes=len([k for k in incompletes if k==student_id])

Given that play_list has been defined to be a list, write a statement that sorts the list.

play_list.sort()

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=list(t)

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[0:3]="spam","eggs","vikings"

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:5]

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]=play_list[-1]*2

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 .

play_list[k:j+1]

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[k:j]

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

plist.append(5)

Given that plist1 and plist2 both refer to lists, write an expression that evaluates to a list that is the concatenation of plist1 and plist2. Do not modify plist1 or plist2.

plist1+plist2

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.

plist2=plist1[k:]

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 plist2. Do not modify plist1 or plist2.

plist3=plist1+plist2

Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .

plist=['spam','eggs','vikings']

Write a statement that defines plist to be the empty list.

plist=[]

Given a variable plist, that refers to a list with 34 elements, write an expression that refers to the last element of the list.

plist[-1]

Given that plist has been defined to be a list of 30 elements, add 5 to its last element.

plist[-1] += 5

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]=-1

Given a variable plist, that refers to a non-empty list, write an expression that refers to the first element of the list.

plist[0]

Assume that plist refers to a list containing exactly five elements. Assume, in addition, that j refers to an int with a value that is between 0 and 3. Write a statement that associates a new value with the element of the list indexed by j. This new value should be equal to twice the value of the next element of the list (i.e. the element after the element indexed by j.

plist[j] = plist[j+1]*2

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.

t[k]

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]

A triangular number is a number that is the sum of the integers from 1 to some integer n. Thus 1 is a triangular number because it's the sum of the integers from 1 to 1; 6 is a triangular number because it's 1+2+3=6. Given the non-negative integers m and n (with m < n), create a list of the triangular numbers between (and including) m and n. Thus if m is 3 and n is 20, the list would be: [3, 6, 10, 15]. Associate the list with the variable triangulars.

temporaryTriangulars=[] sumValue=0 triangulars=[] for eachIndex in range(1, n+1): sumValue = sumValue + eachIndex if sumValue <=n: temporaryTriangulars.append(sumValue) else: break for eachIndex in range(0, len(temporaryTriangulars)): if temporaryTriangulars[eachIndex] >= m: triangulars.append(temporaryTriangulars[eachIndex])


Set pelajaran terkait

Module 5.01: Developing a Community Diagnosis

View Set

Chemical Reactions 5 - Acids and Bases

View Set

Observing God's World Ch 1: Plentiful Plants

View Set

Los pasatiempos y el ocio de perú

View Set

GENETICS FINAL!!!! BIO 2300 Pt. 2

View Set