CS115: Chapter 7

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

Use the selectionSort function presented in this section to answer this question. Assume lst is [3.1, 3.1, 2.5, 6.4, 2.1], what is the content of list after the first iteration of the outer loop in the function?

2.1 (smallest) is swapped with 3.1. so 2.1, 3.1, 2.5, 6.4, 3.1

Suppose list1 = [1, 2, 3], what is the output of the following code? for i in list1: print(i * 2, end = '')

246

Given lst = [1, 2, 3, 7, 14], what is binarySearch(lst, 7) for the binarySearch function in the text?

3

What will be displayed by the following code? def f(value, values): v = 1 values[0] = 44 t = 3 v = [1, 2, 3] f(t, v) print(t, v[0])

3 44

Given lst = [1, 2, 14, 7, 3], for the linear search function in the text, what is linearSearch(lst, 3)?

4

linear search

A function to search an element in a list. Linear search compares the key with the element in the list sequentially.

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], Which of the following is correct? A. print(list1[0]) B. print(list1[:2]) C. print(list1[:-2]) D. print(list1[4:6])

ABCD

What happens when your program attempts to access a list element with an invalid index?

Causes a run time error (index out of range)

True or false? When a list is passed to a function, a new list is created and passed to the function.

False. The reference of the list is passed to the parameter in the function. No new list is created.

Suppose the following code is written to reverse the contents in a list, explain why it is wrong. How do you fix it? lst = [1, 2, 3, 5, 4] for i in range(len(lst)): temp = lst[i] lst[i] = lst[len(lst) - i - 1] lst[len(lst) - i - 1] = temp print(lst)

Here is the fix: lst = [1, 2, 3, 5, 4] for i in range(len(lst) // 2): temp = lst[i] lst[i] = lst[len(lst) - i - 1] lst[len(lst) - i - 1] = temp print(lst)

If the binary search function returns -4, is the key in the list? Where should the key be inserted if you wish to insert the key into the list?

The key is not in the list. The key should be inserted at -(-4 + 1) = 3.

Off-by-one error

Where the first element in a list with index 1, but it should be 0

"Welcome to Python".split() is ________

["Welcome", "to", "Python"]

What is list("a#b#c#d".split('#')?

['a', 'b', 'c', 'd']

What is list("abcd")?

['a', 'b', 'c', 'd']

What is the output of the following code? lst = [1, 2, 3, 4, 5, 6] for i in range(1, 6): lst[i] = lst[i - 1] print(lst)

[1, 1, 1, 1, 1, 1]

If the binary search function returns -4, where should the key be inserted if you wish to insert the key into the list?

at index 3 (-insertionPoint - 1) = -4. So insertionPoint is 3.

How do you create an empty list and a list with the three integers 1, 32, and 2?

emptyList = [] lst = [1, 32, 2]

A Python list's size is

flexible. it can grow and shrink on demand

A string

is a sequence of characters

Given lst = [30, 1, 2, 1, 0], what is the list after applying each of the following statements? Assume that each line of code is independent. lst.append(40) lst.insert(1, 43) lst.extend([1, 43]) lst.remove(1) lst.pop(1) lst.pop() lst.sort() lst.reverse() random.shuffle(lst)

lst.append(40) => [30, 1, 2, 1, 0, 40] lst.insert(1, 43) => [30, 43, 1, 2, 1, 0] lst.extend([1, 43]) => [30, 1, 2, 1, 0, 1, 43] lst.remove(1) => [30, 2, 1, 0] lst.pop(1) => [30, 2, 1, 0] lst.pop() => [30, 1, 2, 1] lst.sort() => [0, 1, 1, 2, 30] lst.reverse() => [0, 1, 2, 1, 30] random.shuffle(list) => list is randomly shuffled

List

stores a sequential collection of elements

A list can contain

the elements of the same type or mixed types

concatenation operator (+)

to join two lists

repetition operator (*)

to replicate elements

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is len(list1)?

8

Indicate true or false for the following statements: a. Every element in a list must have the same type. b.A list's size is fixed after it is created. c.A list can have duplicate elements. d.The elements in a list can be accessed via an index operator.

a. False b. False c. True d. True

What will be displayed by the following code? myList = [1, 5, 5, 5, 5, 1] max = myList[0] indexOfMax = 0 for i in range(1, len(myList)): if myList[i] > max: max = myList[i] indexOfMax = i print(indexOfMax)

indexOfMax is the index of the first max element in the list. So, it is 1.

How do you obtain a list from a string? Suppose s1 is welcome, what is s1.split('o')?

list(s) s1.split('o') is ['Welc', 'me']

Use the selectionSort function presented in this section to answer this question. What is list1 after executing the following statements? list1 = [3.1, 3.1, 2.5, 6.4] selectionSort(list1)

list1 is 2.5 3.1, 3.1, 6.4

list1 = [1, 43] list2 = [x for x in list1] list1[0] = 22

list1 is [22, 43] list2 is [1, 43]

What are list1 and list2 after the following lines of code? list1 = [1, 43] list2 = list1 list1[0] = 22

list1 is [22, 43] and list2 is [22, 43].

To add 5 to the end of list1, use _______.

list1.append(5)

To insert 5 to the third position in list1, use _______.

list1.insert(2, 5) The first position is 0. So, the third position is 2

To remove string "red" from list1, use _______.

list1.remove("red")

For the binarySearch function in Section 7.9.2, what is low and high after the first iteration of the while loop when invoking binarySearch([1, 4, 6, 8, 10, 15, 20], 11)?

low is 4 and high is 6

Given lst = [30, 1, 2, 1, 0], what is the return value of each of the following statements? lst.index(1) lst.count(1) len(lst) max(lst) min(lst) sum(lst)

lst.index(1) => 1 lst.count(1) => 2 len(list) => 5 max(list) => 30 min(list) => 0 sum(list) => 34

Suppose list1 is [1, 3, 2], What is list1 * 2?

[1, 3, 2, 1, 3, 2]

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[:-1]?

[1, 3, 2, 4, 5, 2, 1]

What will be displayed by the following code? def f(i, values = []): values.append(i) return values f(1) f(2) v = f(3) print(v)

[1,2,3]

What is the output of the following code? list1 = list(range(1, 10, 2)) list2 = [] + list1 list1[0] = 111 print(list1) print(list2)

[111, 3, 5, 7, 9] [1, 3, 5, 7, 9]

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is max(list1)?

25

What is the output of the following code? list1 = list(range(1, 10, 2)) list2 = list1 list1[0] = 111 print(list1) print(list2)

[111, 3, 5, 7, 9] [111, 3, 5, 7, 9]

What will be displayed by the following code? list1 = [1, 3] list2 = list1 list1[0] = 4 print(list2)

[4,3]

What will be displayed by the following code? def f(values): values[0] = 44 v = [1, 2, 3] f(v) print(v)

[44, 2, 3]

Show the output of the following two programs: (a) def main(): list1 = m(1) print(list1) list2 = m(1) print(list2) def m(x, lst = [1, 1, 2, 3]): if x in lst: lst.remove(x) return lst main() (b) def main(): list1 = m(1) print(list1) list2 = m(1) print(list2) def m(x, lst = None): if lst == None: lst = [1, 1, 2, 3] if x in lst: lst.remove(x) return lst main()

(a) [1, 2, 3] [2, 3] (b) [1, 2, 3] [1, 2, 3]

Show the output of the following two programs: (a) def main(): number = 0 numbers = [10] m(number, numbers) print("number is", number, "and numbers[0] is", numbers[0]) def m(x, y): x = 3 y[0] = 3 main() (b) def main(): lst = [1, 2, 3, 4, 5] reverse(lst) for value in lst: print(value, end = ' ') def reverse(lst): newLst = len(lst) * [0] for i in range(len(lst)): newLst[i] = lst[len(lst) - 1 - i] lst = newLst main()

(a) number is 0 and numbers[0] is 3 (b) 1 2 3 4 5 This is a tricky question. You need to read the code carefully to get the correct output. Note that the reverse function is supposed to reverse the list, but it was coded incorrectly so it did not reverse the original list. In order to reverse the elements in the original list, modify the function as follows: def reverse(lst): for i in range(int(len(lst) / 2)): temp = lst[i] lst[i] = lst[len(lst) - 1 - i] lst[len(lst) - 1 - i] = temp

If a key is not in the list, the binarySearch function returns _________.

-(insertion point + 1)

Given lst = [1, 2, 14, 7, 3], for the linear search function in the text, what is linearSearch(lst, 4)?

-1

Given lst = [1, 2, 3, 7, 14], what is binarySearch(lst, 5) for the binarySearch function in the text?

-4

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[-1]?

0

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is min(list1)?

1

what is the output of the following code? def m(i, lst): i = 9 lst[0] = 9 def main(): x = [1, 2] i = 1 m(i, x) print(i, x[0], end = '') main()

1 9

1. append(x) Adds an element x to the end of the list. 2. count(x) Returns the number of times element x appears in the list. 3. extend(antherList) Appends all the elements in anotherList to the list. 4. index(x) Returns the index of the first occurrence of element x in the list. 5. insert(index, x) Inserts an element x at a given index. Note that the first element in the list has index 0. 6. pop(index) Removes the element at the given index and return it. The parameter index is optional. If it is not specified, list.pop() removes and returns the last element in the list. 7. remove(x) Removes the first occurrence of element x from the list. 8. reverse() Reverses the elements in the list. sort() 9. Sorts the elements in the list in an ascending order.

1. append(x) Adds an element x to the end of the list. 2. count(x) Returns the number of times element x appears in the list. 3. extend(antherList) Appends all the elements in anotherList to the list. 4. index(x) Returns the index of the first occurrence of element x in the list. 5. insert(index, x) Inserts an element x at a given index. Note that the first element in the list has index 0. 6. pop(index) Removes the element at the given index and return it. The parameter index is optional. If it is not specified, list.pop() removes and returns the last element in the list. 7. remove(x) Removes the first occurrence of element x from the list. 8. reverse() Reverses the elements in the list. sort() 9. Sorts the elements in the list in an ascending order.

1. x in s True if element x is in sequence s. 2. x not in s True if element x is not in sequence s. 3. s1 + s2 Concatenates two sequences s1 and s2. 4. s * n, n * s n copies of sequence s concatenated. 5. s[i] ith element in sequence s. 6. s[i : j] Slice of sequence s from index i to j-1. 7. len(s) Length of sequence s, i.e., the number of elements in s 8. min(s) Smallest element in sequence s. 9. max(s) Largest element in sequence s. 10. sum(s) Sum of all numbers in sequence s. 11. for loop Traverses elements from left to right in a for loop. 12. <, <=, >, >=, ==, != Compares two sequences.

1. x in s True if element x is in sequence s. 2. x not in s True if element x is not in sequence s. 3. s1 + s2 Concatenates two sequences s1 and s2. 4. s * n, n * s n copies of sequence s concatenated. 5. s[i] ith element in sequence s. 6. s[i : j] Slice of sequence s from index i to j-1. 7. len(s) Length of sequence s, i.e., the number of elements in s 8. min(s) Smallest element in sequence s. 9. max(s) Largest element in sequence s. 10. sum(s) Sum of all numbers in sequence s. 11. for loop Traverses elements from left to right in a for loop. 12. <, <=, >, >=, ==, != Compares two sequences.

__________ creates a list. A. list1 = list() B. list1 = [] C. list1 = list([12, 4, 4]) D. list1 = [12, 4, 4] E. list1 = [1, "3", "red"]

ABCDE

BEFORE: list2 = list1 List 1 = Contents of list1 List 2 = Contents of list2

AFTER: list2 = list1 List 1 = Contents of list1 List 2 = Contents of list1

selection sort

An approach to sort list. It finds the largest number in the list and places it last. It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number.

binary search

An efficient function to search a key in a list. Binary search first compares the key with the element in the middle of the list and reduces the search range by half. For binary search to work, the list must be pre-sorted.

Index

An integer value used to specify the position of an element in the list. The list index is an int value starting with 0 for the first element, 1 for the second, and so on.

garbage collection

An object that is not referenced is a garbage. Garbage is automatically collected by Python.

Given lst = [30, 1, 12, 14, 10, 0], how many elements are in lst? What is the index of the first element in lst? What is the index of the last element in lst? What is lst[2]? What is lst[-2]?

There are 6 elements in the list. The index of the first element is 0. The index of the last element is 5. lst[2] is 12. list[-2] is list[6-2], which is 10.


Conjuntos de estudio relacionados

Securities Industry Essentials Exam

View Set

HRT 3740- Ch.11-13 Practice Questions

View Set

Chapter 03: Gathering and Appraising the Literature

View Set

Assessment and Management of Patients with Male Reproductive Disorders

View Set

Legal Requirment, HACCP, and inspections

View Set

155A.271 Continuing Education Requirements

View Set

Chapter 2 and 3 Accounting Vocab

View Set