1 of 44

Welcome to Lecture 7:

Nested Lists + Number Representation

Class will start at 10:10.

​

In the meantime, we will go around. Tell me your name, where you’re from, & favorite mythical creature.

2 of 44

Today’s Topics

  • Announcements
  • Review
  • Nested lists
  • How to represent a board
  • Accessing elements of nested list
  • Binary numbers
  • Hex numbers
  • Converting numbers

3 of 44

Announcements

  • I’ll post today’s lecture quiz later tonight (worst case Friday)
  • Project 2 - Project Party Friday from 2 to 6PM in SDH-200
  • Project 2 is due Monday, 7/1
  • Reminder: 2 Lab assignments per lab (conceptual AND coding)
  • All office hour locations + times are on website
  • Any OH after 7PM will be online (hybrid may be offered)
  • *Andrew out next week - no OH

4 of 44

Review from Last Lecture

  • Higher Order Functions
    • We can create our own
    • Function that takes in a function as input
  • Lambdas
    • Temporary, anonymous functions
    • We can set variables to functions / lambdas
  • Both
    • We need to use “call” or “run” to invoke the function
    • We need to specify inputs when invoking if the function has inputs

5 of 44

So far with lists, …

  • We’ve seen containing numbers, words, symbols
  • But lists can also contain functions!

​

​

6 of 44

So far with lists, …

  • We’ve seen containing numbers, words, symbols
  • But lists can also contain functions!

​

​

  • And other lists!

​

​

  • Lists can contain any object and any object type! And lists can mix and match data types for each element

7 of 44

Why use Nested Lists?

  • Organizing data
  • Represent data structures
    • Can represent a board with rows and columns
    • Can represent an Excel sheet with titles and then data
    • Can create different models of data
    • Can represent x, y values and coordinates

8 of 44

Representing a Board

  • Representing a Tic-Tac-Toe Board game

9 of 44

Representing X, Y Coordinates

  • Representing X, Y Coordinates

10 of 44

Guess that Output!

11 of 44

Guess that Output!

12 of 44

Why use Nested Lists?

  • Organizing data
  • Represent data structures
    • Can represent a board with rows and columns
    • Can represent an Excel sheet with titles and then data
    • Can create different models of data
    • Can represent x, y values and coordinates

13 of 44

Representing a Board

  • Representing a Tic-Tac-Toe Board game

14 of 44

Representing X, Y Coordinates

  • Representing X, Y Coordinates

15 of 44

Guess that Output!

16 of 44

Guess that Output!

17 of 44

Accessing Elements + Lengths

  • Outputs the first ROW of nested list
    • The row data type is a list!
  • To access an entire column (also outputs a list):
    • We can use map!
  • To access a specific values at row, column:

​

  • Length of nested list: Outputs the number of rows
  • Length of : Outputs the number of columns in row 1

18 of 44

Guess that Output!

​

  • ​

19 of 44

Guess that Output!

​

  • ​

20 of 44

Guess that Output!

​

  • ​

21 of 44

Guess that Output!

​

  • ​

22 of 44

Accessing Columns

​

  • How can we report all items in column B?

23 of 44

Accessing Columns

​

  • How can we report all items in column B?

​

​

​

​

​

  • Since each element of coordinates is a list, “map” applies the “item 2” function over each element.

24 of 44

Buggy Code. What went wrong?

  • I wanted to get a specific “X”

25 of 44

Accessing Row, Columns with Loops!

  • This structure gives me a specific non-list element

​

  • To generalize this, we’ll need NESTED loops!

26 of 44

Code that Function!

  • Objective: Create a sum function that takes in a nested list. Outputs the sum of all the values.

27 of 44

Code that Function!

  • the sum of all the values.

28 of 44

Code that Function!

  • the sum of all the values.

29 of 44

Code that Function!

  • Objective: Add a new element to the end of each row. The new element should be the sum of that particular row

30 of 44

Code that Function!

  • Objective: Add a new element to the end of each row. The new element should be the sum of that particular row

31 of 44

Code that Function!

  • ​

32 of 44

Code that Function!

  • Objective: Create a function that finds the maximum number in the nested list.

33 of 44

Code that Function!

  • ​

34 of 44

Code that Function!

  • Objective: Create a function that finds the maximum number in the nested list. Do this using HOFs. Hint: You will need to find the maximum of each row first.

35 of 44

Code that Function!

36 of 44

Number Representation: Decimal

  • The decimal system has 10 digits
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • We can represent the number: 5423
    • (5 * 103 ) + (4 * 102) + (2 * 101) + (3 * 100)
  • When we are using the decimal system, we say we are using base 10
    • 542310 means this number is in decimal
  • But there are other numerical systems
  • We can write any number in any base!

37 of 44

Number Representation: Base 8

  • Let’s say I want to represent a base 8 number
  • Let’s use 5423 (but it is no longer the same value/number in base 8)
  • We can represent the number: 54238 in decimal
    • (5 * 83 ) + (4 * 82) + (2 * 81) + (3 * 80)
    • (2560) + (512) + (16) + (3)
    • = 2835 in decimal!

​

38 of 44

Number Representation: Binary

  • The “language” of a computer is binary (ex: 011010101), which is base 2
    • Computers store data as binary
    • Represent data structures as binary
    • Images, numbers, text, code, code, etc!
  • Let’s convert 1011 from binary to decimal
    • (1 * 23 ) + (0 * 22) + (1 * 21) + (1 * 20)
    • (8) + (0) + (2) + (1)
    • = 11 in decimal!

​

39 of 44

Number Representation: Binary

  • Let’s convert 25 from decimal to binary
    • First write out the powers of 2
    • Keep writing powers of 2 until we reach a number greater than 25
      • (25) + (24) + (23) + (22 ) + (21) + (20) =
      • (32) + (16) + (8) + (4) + (2) + (1)
    • Next, place place a 1 for all the numbers that allow us to sum to 25, and 0 otherwise - left associative!

​

32

16

8

4

2

1

​

​

​

​

​

​

​

1

​

​

​

​

40 of 44

Number Representation: Binary

  • Let’s convert 25 from decimal to binary
    • Next, place place a 1 for all the numbers that allow us to sum to 25, and 0 otherwise - left associative!

​

​

​

  • Final answer is 11001

​

32

16

8

4

2

1

​

1

1

0

0

1

16 + 8 + 1 = 25

41 of 44

Number Representation: Hexadecimal

  • Has 16 different digits:
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
    • A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
  • Let’s convert A3C to decimal:
    • (10 * 162 ) + (3 * 161) + (12 * 160)
    • (2560) + (48) + (12)
    • = 2620 in decimal

​

42 of 44

Number Representation: Hexadecimal

  • Has 16 different digits:
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
    • A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
  • Let’s convert 574 to hexadecimal:
    • Continuously divide our number by 16, save the remainders
    • ​

​

Division

Equals

Whole #

Remainder

574 / 16

35.875

35

14

35 / 16

2.1875

2

3

2 / 16

0.125

0

2

43 of 44

Number Representation: Hexadecimal

  • Has 16 different digits:
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
    • A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
  • Let’s convert 574 to hexadecimal:
    • Continuously divide our number by 16, save the remainders
    • Remainders give us hexadecimal BUT the Most Significant Digit (MSD) is bottom going up
    • Also convert any number > 10 to a letter
    • 14 = E
    • Answer: 23E16

Remainder

14 = E

3

2

MSD

LSD

44 of 44

Number Representation: Operations

  • Let’s add two different bases: Find E4 + 101, then convert to base 8
    • First convert both to decimal
      • E4 → (15 * 161) + (4 * 160) = 244
      • 101 → (1 * 22) +(0 * 21) + (1 * 20) = 5
    • Then add
      • 244 + 5 = 249
    • Then convert
      • Answer: 3718

​

Division

Equals

Whole #

Remainder

249 / 8

31.125

31

1

31 / 8

3.875

3

7

3 / 8

0.375

0

3