1 of 16

Welcome to CSE 160! Week 2

Section AA with Andre

2 of 16

Agenda

  1. Recap: if statements, for loops, range
  2. Practice problems
  3. Mini-Project: password cracking

​

Goals:

  • Gain better familiarity with if statements, for loops, and range
  • Grasp the power of sheer computational brute force
    • …and how to design systems resistant to it

3 of 16

Logistics

  • HW0 was due yesterday, Wed. Jan 10
  • Week 2 Programming Check-In due Mon. Jan 15
  • HW1 has been released and is due on Wed. Jan 17

4 of 16

For loops

for i in [0, 1, 2, 3]:

print(i)

  • You can pick any variable name in place of “i”
  • Indentation is important
  • Motivation: loops let you avoid repeating code, which makes
    • code more readable,
    • avoids bugs,
    • makes it easy to quickly change program details

5 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for i in range(5): # equivalent to range(0, 5, 1)

print(“hi!”)

​

[out]:

hi!

hi!

hi!

hi!

hi!

6 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for i in range(5): # equivalent to range(0, 5, 1)

print(i)

​

[out]:

0

1

2

3

4

7 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for i in range(3, 5): # equivalent to range(3, 5, 1)

print(i)

​

[out]:

3

4

8 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for i in range(0, 10, 2):

print(i)

​

[out]:

0

2

4

6

8

9 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for i in [‘apple’, ‘banana’, ‘orange’]:

print(i + ‘s’)

​

[out]:

apples

bananas

oranges

10 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for letter in “five”:

print(letter)

​

[out]:

f

i

v

e

11 of 16

range

syntax: range(start [inclusive], stop [exclusive], step)

​

for letter in “five”:

print(letter * 5)

​

[out]:

fffff

iiiii

vvvvv

eeeee

12 of 16

Practice Problem #1

Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. The example solution is two lines long.

​

[out]:

24

27

30

33

36

13 of 16

Practice Problem #1

Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. The example solution is two lines long.

​

[out]:

24

27

30

33

36

14 of 16

Practice Problem #2

Write the output to the following program:

for i in [1, 2, 3]:

for j in [1, 2, 3]:

print(i + j)

15 of 16

Practice Problem #3

Write the output to the following program:

sum = 0

for i in [1, 2, 3]:

for j in [1, 2, 3]:

sum = sum + i

print(sum)

16 of 16

Mini-Project: Password Cracking

  • iPhone passwords are 6 digits long.
  • How long would it take for a computer to crack your password?
    • Approach #1: structured search
    • Approach #2: random search
  • We’ll play with ways to decrease password crackability