ITEC 109 Lecture 22 Ways to use lists

  • Slides: 18
Download presentation
ITEC 109 Lecture 22 Ways to use lists

ITEC 109 Lecture 22 Ways to use lists

Review • Lists – Copying / Pass by reference Ways to use lists

Review • Lists – Copying / Pass by reference Ways to use lists

Objectives • Look at how to use lists / arrays – Capacity – Marking

Objectives • Look at how to use lists / arrays – Capacity – Marking / Segmenting – Parallel Lists / Arrays Ways to use lists

Arrays • Fixed size • How can we “tombstone” a particular spot in an

Arrays • Fixed size • How can we “tombstone” a particular spot in an array i. e. declare it is dead and all after it is dead as well X Ways to use lists

What can we do with this? What do we have to be careful with?

What can we do with this? What do we have to be careful with? Solution • Added variable “Dead places” 0 1 2 3 4 5 6 7 tombstone =3; Ways to use lists

Capacity • Create an array, but not fill it with data end=0; array[end] =

Capacity • Create an array, but not fill it with data end=0; array[end] = int(raw_input()); end = end +1; How do you make sure you don’t overfill the capacity of the array? Ways to use lists 0 End 44 0 1 1 2 2 End

Example • Create a function that will set a value in an array and

Example • Create a function that will set a value in an array and update where the capacity is or return -1 if you try to insert into the end of the array Ways to use lists

Swapping • Replace one value w/ another and vice versa 3 5 4 6

Swapping • Replace one value w/ another and vice versa 3 5 4 6 7 8 9 10 0 1 2 3 4 5 6 7 Ways to use lists

Method • How do you swap the values stored in variables x, y in

Method • How do you swap the values stored in variables x, y in this scenario? x=5; y=4; • What is different than the array scenario? • How do we swap values in an array? Ways to use lists

Inserting Insert 2 here What is the algorithm that you would use to move

Inserting Insert 2 here What is the algorithm that you would use to move the numbers around 1 3 4 6 7 8 9 0 1 2 3 4 5 6 Ways to use lists 7

Code capacity 1 3 4 6 7 8 9 0 1 2 3 4

Code capacity 1 3 4 6 7 8 9 0 1 2 3 4 5 6 7 slot. To. Insert = 1; for i in range(capacity, slot. To. Insert, -1): array[i] = array[i-1]; array[slot. To. Insert]=2; capacity=capacity+1; capacity Ways to use lists 1 2 3 4 6 7 8 0 1 2 3 4 5 6 9 7

Marking • • Modified tombstone Array of sorted values (test scores) Need to find

Marking • • Modified tombstone Array of sorted values (test scores) Need to find a cutoff point (70) How many people are on each side? What is the average of each side? Ways to use lists

Marketing • Have an array of salaries for company • Need to figure out

Marketing • Have an array of salaries for company • Need to figure out how many are making – <5 k (Temp workers) – Between 5 -20 k – Between 20 -40 k – Between 40 -100 k – Greater than or equal to 100 k Ways to use lists

Similar to marking a piece of wood before cutting it. Visualization Similar to substring

Similar to marking a piece of wood before cutting it. Visualization Similar to substring calls • Several state variables <20 k Ways to use lists 40 k 100 k

Parallel Arrays • Storing multiple related values together • One array can’t do it

Parallel Arrays • Storing multiple related values together • One array can’t do it • Many arrays can First name Last name Account Balance John Doe 300. 00 Alpha Bet 26. 00 El Ite 1337. 00 Ways to use lists

Parallel Arrays • Multiple arrays • One index used to get related values account=[];

Parallel Arrays • Multiple arrays • One index used to get related values account=[]; first=[]; last=[]; //Code to create and initialize the arrays user. To. Print=int(raw_input()); print. Now(first[user. To. Print] + “ “ + last[user. To. Print])); print. Now(“Account Balance: “ + str(account[user. To. Print])); Ways to use lists

Issues • Adding and removing values from parallel arrays – Stock data (symbol, high,

Issues • Adding and removing values from parallel arrays – Stock data (symbol, high, low, current) • How do you add? • How do you remove? Ways to use lists

Review • Array usage – Swap / Insert • Marking • Parallel arrays Ways

Review • Array usage – Swap / Insert • Marking • Parallel arrays Ways to use lists