Unit 8: Arrays

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is output by: print(len(stuff))

3

1 / 1 pts Consider the following code: def tryIt(b):b = b + 100#**********MAIN***********x = 99print(x)tryIt(x)print(x) What is output?

99 99

The _____________ is the address of a piece of data.

index

Using more than one array to store related data is called _____________ arrays.

parallel

Consider the following code: values = []for i in range(5):values.append(int(input("Next number: ")))print(values) If the line print(len(values)) is appended to this code, what is output by that print(len(values)) line?

5

Which of the following would NOT be stored using an array?

A first name

A search algorithm returns the element that we're searching for, rather than the index of the element we're searching for.

False

Suppose we used our searching algorithm to look for a specific element in a list. The algorithm returned -1. What does this mean?

Our algorithm did not find the element we were looking for.

Consider the following code: words = ["This", "is", "a", "sentence"]for i in range(len(words)):print(words[i]) What does this code do?

Print out, on separate lines, each word in the list

Test 8

Test answers hidden

nums = [30, 10, 30, 14, 10, 3, 9, 7] print(nums)nums.insert(3, -89) print (nums) What is output by the code above?

[30, 10, 30, -89, 14, 10, 3, 9, 7]

nums = [30, 10, 30, 14, 10, 3, 9, 7]print(nums)nums.extend([24, 3, 21, 19])print(nums) What is output by the second print statement in the code above?

[30, 10, 30, 14, 10, 3, 9, 7, 24, 3, 21, 19]

nums = [30, 10, 30, 14, 10, 3, 9, 7]nums.pop(1)nums.pop(1)print(nums) What is output by the code above?

[30, 14, 10, 3, 9, 7]

A(n) ___________ is an individual piece of data in an array.

element

Consider the following code: price = [12.99, 10.00, 2.50, 1.99] This code is an example of a(n) ______________ _____________.

initializer list

What data type are the indexes in stuff?

int

In Python, an array is called a ______________.

list

8.3 Code Practice

nums = [14, 36, 31, -2, 11, -6] print(nums)

Considering the following code: x = [1,0,6,3]y = [5,8,1,2]for i in range(len(x)):print("(" + str(x[i]), + "," + str(y[i]) + ")") This code prints out (x,y) coordinate pairs. It is an example of using ______________ arrays.

parallel

A ___________ is a variable used to pass information to a method.

parameter

An algorithm used to find a value in an array is called a ______________.

search

For Questions 3-5, consider the following code: stuff = []stuff.append("emu")stuff.append("frog")stuff.append("iguana")print(stuff) What data type are the elements in stuff?

string

An array stores data using multiple variable names.

False

We use arrays in programs to ______________ data.

organize

8.10 Code Practice: Question 1

x = input("Enter a word: ") y = input("Enter a word: ") x,y = y, x print(x) print(y)

Quiz 8

On your own for this one

8.6 Code Practice: Question 1

import random def buildArray(arr, nums): i = 0 while i < nums: arr.append(random.randint(10,99)) i+=1 arr = [] numbers = int(input("How many values to add to the array:\n")) buildArray(arr, numbers) print(arr)

8.7 Code Practice: Question 3

w = ["Algorithm", "Logic", "Filter", "Software", "Network", "Parameters", "Analyze", "Algorithm", "Functionality", "Viruses"] s=[] for x in w: s.append(len(x)) for x in range(len(w)): print(str(s[x])+": "+str(w[x]))

Where does append add the new elements?

To the end of an array.

nums = [30, 10, 30, 14, 10, 3, 9, 7]nums.remove(10)print(nums) What is output by the code above?

[30, 30, 14, 10, 3, 9, 7]

Consider the following code: def tryIt(b):for i in range(len(b)):b[i] = b[i] + 100#***********MAIN************x = []x = [56, 78, 88]print(x)tryIt(x)print(x)

[56, 78, 88] [156, 178, 188]

Why do we add the line of code stuff = [] before using an array?

This creates the data structure "stuff" and lets Python know it is an array.

Assignment 8: Personal Organizer

eventName = [] eventMonth = [] eventDay = [] eventYear = [] def addEvent(): userEventName = input("Enter event name: ") userEventMonth = int(input("Enter event month (1-12): ")) userEventDay = int(input("Enter event day(1-31): ")) userEventYear = int(input("Enter event year (Ex:2020): ")) userEventMonth = validateMonth(userEventMonth) userEventDay = validateDay(userEventMonth,userEventDay,userEventYear) eventName.append(userEventName) eventMonth.append(userEventMonth) eventDay.append(userEventDay) eventYear.append(userEventYear) def validateMonth(month): if month >= 1 and month <= 12: return month else: return 1 def validateDay(month,day,year): # invalid days if day < 1 or day > 31 : return 1 # if the month is february if month == 2: isleap = False if year%4 == 0: if year%100 == 0: if year%400 == 0: isleap = True else: isleap = True # if the year is leap if isleap: if day < 30: return day else: return 1 else: if day < 29: return day else: return 1 # month with 31 days if month in [1,3,5,7,8,10,12]: return day # month with 30 days if month in [4,6,9,11] and day < 31: return day else: return 1 def printEvents(): print("****** LIST OF EVENTS *********") months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] for index in range(len(eventName)): print(eventName[index]) print("Date: "+months[eventMonth[index] -1]+ " " + str(eventDay[index]) + ", " + str(eventYear[index])) def printEventsForMonth(month): months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] print("****** EVENTS IN " + months[month -1] + " *********") for index in range(len(eventName)): if eventMonth[index] == month: print(eventName[index]) print("Date: "+months[eventMonth[index] -1]+ " " + str(eventDay[index]) + ", " + str(eventYear[index])) userChoice = "yes" while userChoice.upper() != "NO": addEvent() userChoice = input("Do you want to enter another event? NO to stop: ") printEvents()

Why do we use for loops with arrays?

Because they let us quickly process the contents of an array, which have many values.

How many for loops does it take to sort numbers (using the sorting algorithm covered)?

2

A swap is:

an algorithm exchanging two values stored in variables

8.7 Code Practice: Question 1

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)

8.6 Code Practice: Question 2

import random def buildArray(a, n): for i in range (n): a.append(random.randint(10,99)) arr = [] def sumArray(a): tot = 0 for i in range(len(a)): tot = tot + a [i] return tot arr = [] numbers = int(input("How many values to add to the array:\n")) buildArray(arr, numbers) print(arr) print("Total " + str(sumArray(arr)) )

In a swap you need a _____________ variable so that one of the values is not lost.

temp

8.2 Code Practice

temperatures = [] i = 0 while i < 5: try: t = int(input('Enter a temperature: ')) temperatures.append(t) i += 1 except ValueError: print('Enter a number') print(temperatures)

8.10 Code Practice: Question 3

terms = ["Bandwidth", "Hierarchy", "IPv6", "Software", "Firewall", "Cybersecurity", "Lists", "Program", "Logic", "Reliability"] def swap(arr, in1, in2): w = arr[in1] arr[in1] = arr[in2] arr[in2] = w print(terms) swap(terms, 5, 1) swap(terms, 2, 4) swap(terms, 3, 5) swap(terms, 5, 6) swap(terms, 6, 8) swap(terms, 8, 9) print(terms)

8.5 Code Practice

twainQuotes = ['I have never let my schooling interfere with my education.', 'Get your facts first, and then you can distort them as much as you please.', "If you tell the truth, you don't have to remember anything.", 'The secret of getting ahead is getting started.', "Age is an issue of mind over matter. If you don't mind, it doesn't matter. "] print(twainQuotes) twainQuotes.sort() print(twainQuotes) twainQuotes.insert(1,'Courage is resistance to fear, mastery of fear, not absence of fear.' ) print(twainQuotes)

8.9 Code Practice (83%)

v = [24, 20, 29, 32, 34, 29, 49, 46, 39, 23, 42, 24, 38] usr = int(input("Enter number: ")) print(f"Index: {v.index(usr)}") if usr in v else print(-1)

8.10 Code Practice: Question 2

vocab = ["Libraries", "Bandwidth", "Hierarchy", "Software", "Firewall", "Cybersecurity","Phishing", "Logic", "Productivity"] print (vocab) for i in range (1, len (vocab)): count = i - 1 key = vocab[i] while (vocab[count] > key) and (count >= 0): vocab[count+1] = vocab[count] count -= 1 vocab [count+1] = key print(vocab)

8.7 Code Practice: Question 2

w = ["Algorithm", "Logic", "Filter", "Software", "Network", "Parameters", "Analyze", "Algorithm", "Functionality", "Viruses"] for x in w: if(x[0]=='A'): print(x)


Kaugnay na mga set ng pag-aaral

2.4 Reading: Apply Information Literacy Skills to Web Searches

View Set

Ch.8: Survey Research, an overview

View Set

Religion Chapter 6: The Resurrection of Jesus Christ

View Set

Sherpath Lesson Ch. 15, 16, 17: Ears, Nose, and Throat

View Set

Chapter 27: Management of Patients with Coronary Vascular Disorders

View Set