Unit 8 CS Essential Edhesive
Consider the following code: stuff = ["dog", "cat", "frog", "zebra", "bat", "pig", "mongoose"] c = 0 for i in range(len(stuff)): c = c + len(stuff[i]) print (c) What is output? 35 29 7 21
29
Consider the following code: h = [8, 4, 5, 1, 1, 6, 7, 9, 3, 3, 9, 3] print (h[1]) What is output? 8 4 5 1
4
An array is: Individual letters, digits, or symbols. A data structure that holds many pieces of data at the same time. A variable that stores letters, numbers, and words, and is not used for calculations. A collection of commands that are given a name.
A data structure that holds many pieces of data at the same time
In an array, what is the difference between an element and an index? An element is a type of index. There is no difference. An element refers to actual data within the array while an index refers to a memory location. An index refers to actual data within the array while an elements refers to a memory location.
An element refers to actual data within the array while an index refers to a memory location.
_____________ is storing a specific value in the array. Assigning Indexing Summing Iterating
Assigning
Consider the following code that works on an array of integers: for i in range(len(values)): if (values[i] < 0): values[i] = values [i] * -1 What does it do? Changes all negative numbers to positives. Nothing, values in arrays must be positive. Subtracts one from every value in the array. Changes all positives numbers to negatives.
Changes all negative numbers to positive
Consider the following code: def mystery (a): for i in range(len(a)): if (i % 2 == 0): print(a[i], end = " ") # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) What is output? Juneau Helena Annapolis Nothing is output, you cannot do modular division on strings Atlanta Madison Topeka Juneau Atlanta Helena Madison Annapolis Topeka
Juneau Helena Annapolis
Why do we use for loops with arrays? For loops let us access one specific element of an array. For loops let Python handle the values as strings not numbers. To store one piece of data at a time. Lets us quickly process contents of the array.
Lets us quickly process contents of the array
Consider the following code: def mystery (a): for i in range(len(a)): if (a[i] % 2 == 0): print(a[i], end = " ") # ********** MAIN ********** s = [63, 72, 21, 90, 64, 67, 34] mystery(s) What is output? Nothing is output, you cannot do modular division on strings 63 21 64 34 72 90 64 34 63 21 67
NOT 63, 21, 64, 34 NOT Nothing is output
Consider the following code: def mystery (a): for i in range(len(a)): if (a[i] % 2 == 0): print(a[i]), # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) What is output? Juneau Atlanta Helena Madison Annapolis Topeka Juneau Helena Annapolis Nothing is output, you cannot do modular division on strings. Atlanta Madison Topeka
Nothing is output, you cannot do modular division on strings.
Consider the following code: tests = [78, 86, 83, 80, 89, 92, 91, 94, 67, 72, 80, 95] c = int(input("Cutoff value: ")) n = 0 for i in range(len(tests)): if (tests[i] >= c): n = n + 1 print("Values above " + str(c) + ": " + str(n)) What is output when the user enters 80? Values above 80: 12 Values above 80: 9 Values above 80: 7 Values above 80: 5
Values above 80: 9
Consider the following code: def mystery (a): for i in range(len(a)): j = a[i] a[i] = j.replace("a", "@") # ********** MAIN ********** stuff = ["Juneau", "Atlanta", "Helena", "Madison", "Annapolis", "Topeka"] mystery(stuff) print(stuff) What is output? ['June@u', 'Atl@nt@', 'Helen@', 'M@dison', 'Ann@polis', 'Topek@'] ['June@u', '@tl@nt@', 'Helen@', 'M@dison', '@nn@polis', 'Topek@'] ['Juneau', '@tlanta', 'Helena', 'Madison', '@nnapolis', 'Topeka'] ['Jun@au', 'Atlanta', 'H@l@na', 'Madison', 'Annapolis', 'Top@ka']
['June@u', 'Atl@nt@', 'Helen@', 'M@dison', 'Ann@polis', 'Topek@']
Consider the following code: def mystery (a): for i in range(len(a)): a[i] = a[i] * 100 # ********** MAIN ********** ar = [84, 11, 67, 70, 93, 39, 46, 27] mystery(ar) print (ar) What is output? [84, 11, 67, 70, 93, 39, 46, 27] [8400, 1100, 6700, 7000, 9300, 3900, 4600, 2700] [4, 1, 7, 0, 3, 9, 6, 7] [8, 1, 6, 7, 9, 3, 4, 2]
[8400, 1100, 6700, 7000, 9300, 3900, 4600, 2700]
Consider the following code: s = [63, 72, 21, 90, 64, 67, 34] s.sort(reverse = True) print(s) What is output? [34, 67, 64, 90, 21, 72, 63] [21, 34, 63, 64, 67, 72, 90] [90, 72, 67, 64, 63, 34, 21] [63, 72, 21, 90, 64, 67, 34]
[90, 72, 67, 64, 63, 34, 21]
Consider the following: stuff = ["dog", "cat", "frog", "zebra", "bat", "pig", "mongoose"] "frog" is ____________. a sum a list an index an element
an element
___________ allows an entire array to be created with stored values at the same time. An index An initializer list An element Assigning
an initializer list
The ___________ method adds a new element onto the end of the array. add len input append
append
A(n) ____________ is a variable that holds many pieces of data at the same time. index array element length
array
A(n) ____________ is a piece of data stored in an array. length array element index
element
The ___________ method adds all the elements of a list onto the end of an array. insert append pop extend
extend
The _____________ method returns the length of an array. len append input add
len
Consider the following code: tests = [78, 86, 83, 89, 92, 91, 94, 67, 72, 95] sum = 0 for i in range(_____): sum = sum + tests[i] print("Class average: " + str((sum/_____))) What should go in the ____________ to make sure that the code correctly finds the average of the test scores? len(tests) - 1 val(tests) len(tests) sum
len(tests)
Sorting algorithms are used to ____________ arrays, while search algorithms ____________ values in arrays. index, find find, order order, find index, find
order, find
The ___________ method removes elements from an array by the value, not the location. remove extend append pop
remove
The _________________ algorithm requires two for loops max reverse sort search
sort
Consider the following array: stuff = ["dog", "cat", "frog", "zebra", "bat", "pig", "mongoose"] for i in range(len(stuff)): print (______) To print the words in the arrays in all UPPERCASE you would replace the ____________ with: stuff[i].upper() upper(stuff) stuff.upper(i) stuff.upper[i]
stuff[i].upper()
Consider the following code: def tryIt(s): temp = "" for i in range(len(s)): if(s[i] == "e"): temp = temp + "@" else: temp = temp + s[i] print (temp) # ********** MAIN ********** f = "to be or not to be" tryIt(f) What is output? to be or not to be to b@ or not to be @o be or no@ @o be to b@ or not to b@
to b@ or not to b@
Which of the following is NOT a reason to use arrays? To do number calculations. Organize information. To quickly process large amounts of data. To store data in programs.
to do number calculations
Where does append add a new element? To the end of an array. To the beginning of an array. To the middle of an array. In alphabetical/numerical order.
to the end of an array
Consider the following code: stuff = ["dog", "cat", "frog", "zebra", "bat", "pig", "mongoose"] print(stuff[3]) What is output? zebra ['dog', 'cat', 'frog', 'zebra', 'bat', 'pig', 'mongoose'] frog bat
zebra