Lecture 23 - Files

Lecture Date: Friday, March 18

We’re going to keep looking at files today! We’re going to work with a couple large data sets and see if we can write some interesting programs.

Download these data sets and put them into your PyCharm project directory:

Here are some other datasets! http://introcs.cs.princeton.edu/java/data/

If we get to it today, we’ll look at reading datasets directly from the web, like our weather data: http://www.wunderground.com/history/airport/KCHO/2015/10/15/DailyHistory.html?format=1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def load_spelling_file(filename):
    correct_spelling = {}
    misspelling = {}

    datafile = open(filename, "r")
    for line in datafile:
        line = line.split(",")
        correct_spelling[line[1].strip()] = line[0]
        misspelling[line[0]] = line[1].strip()

    return correct_spelling, misspelling

correct_spelling, misspelling = load_spelling_file("misspellings.csv")
done = False
while not done:
    word = input("Please enter a word (END to quit): ")
    if word == "END":
        done = True
        break
    if word in correct_spelling:
        print("The word '", word, "' is spelled correctly!")
    elif word in misspelling:
        print("I think that '", word, "' is spelled", misspelling[word])
    else:
        print("I don't know that word.  Sorry!")
1
2
3
4
5
6
7
8
9
import urllib.request

link = input ( 'Web page: ' )

stream = urllib.request.urlopen( link )

for line in stream:
    decoded = line.decode("UTF-8")
    print(decoded.strip())
1
2
3
4
5
6
7
8
9
10
import urllib.request
year = "2012"
month = "03"
day = "17"
url = "http://www.wunderground.com/history/airport/KCHO/" + year + "/" + month +"/" + day + "/DailyHistory.html?format=1"

stream = urllib.request.urlopen(url)
for line in stream:
    decoded = line.decode("UTF-8").strip().split(",")
    print(decoded)


Lecture 22 - Functions and Files

Lecture Date: Wednesday, March 16

Up to this point, you have been getting all of your input for your programs from the user from the keyboard. This is great, but what if you wanted to read in hundreds, or even thousands, of data points to run your program against. To say it would get tedious is probably an understatement.

For the next few lectures, we’ll learn how to read files as input instead of just the keyboard.

First, though, we’ll make our own very simple data set. Create a new file in a project called names.csv. In that file, put the names of the five or six people sitting around you.

Then try out this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
def read_list_of_names(filename):
    names = []
    datafile = open(filename, "r")

    for line in datafile:
        line = line.strip()
        names.append(line)
    datafile.close()

    return names


print(read_list_of_names("names.txt"))

In this example, we have written a function that will read a file based upon a filename we provide. Let’s make it even more generic:

1
2
filename = input("What file would you like to read?")
print(read_list_of_names(filename))

Now, we can read any file of names that a user provides!

So, why did we write a function for this? We want to 1) be able to read any file, 2) remove that code from the main part of the program so it’s easier read, and 3) this makes it way easier to test.

Where else can we get data? Let’s look at some weather data! Weather Underground Historical Weather

Some files:

Code From Today
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# input: name of file to read
# output: list of all names in the file
def read_name_list(file_name):
    names = []
    name_file = open(file_name, "r")

    for line in name_file:
        line = line.strip()
        names.append(line)

    return names

def read_temperature_data(file_name):
    temperatures = []
    data_file = open(file_name, "r")
    burn_line = True

    for line in data_file:
        if burn_line:
            burn_line = False
            continue
        entry = line.split(",")
        temperatures.append(float(entry[1]))

    return temperatures

# input: file name of weather data to read
# output: average temp (float), high temp, low temp
def statistics(file_name):
    temperatures = read_temperature_data(file_name)

    length = len(temperatures)
    total = sum(temperatures)

    return total / length, max(temperatures), min(temperatures)



print(statistics("weather.csv"))


Lecture 21 - Functions and Files

Lecture Date: Monday, March 14

We will continue talking about functions today. We will write a few new functions and cover some specific nuances of function usage:

  • calling a function from another function
  • local and global variables
  • variable name collision
  • keyword parameters
  • global variables vs. global constants
  • how to test a function
  • returning multiple values

Let’s consider some different types of functions:

Functions might or might not return a value
All functions return - that’s what happens when they reach the end or finish by using the keyword return. The difference is some bring back to the place where they were called some value of something. Others don’t - we call these void functions.

Some functions have effects, even if they are void
Consider print(text). This function is technically void - it does not return anything. However, it does print stuff to the screen. A function can have an effect on the system even if it does not return a value. We will see this more when we consider how parameters are passed by value or passed by reference.

You can call functions from other files
The point of a library is to provide a set of functionality with functions that you can use in your program. You can then import that library in to a bunch of different programs. It’s completely reusable.

When you invoke a function, the following happens in order:

  1. A new piece of memory (called a “stack frame” or “activation record”) is created.
  2. The parameters of the function are created in that new memory.
  3. The parameters you pass in to the function invocation get copied into the new memory’s parameters in order.
  4. We note where we left off running the old function
  5. We stack the new memory on top of the old memory, covering up the old memory completely
  6. We start running the code in the new function’s body

When you return from a function, either with the return keyword or by reaching the end of the body, the new memory is removed (“popped”) from the stack, leaving us with the calling function’s memory.

Let’s watch and see what happens at the Python Tutor Visualizer!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import random

def who_wins(team_1, team_2):
    winner_choice = random.randint(0,1)
    if winner_choice == 0:
        return team_1
    else:
        return team_2

team_1 = input("Please enter Team 1: ")
team_2 = input("Please enter Team 2: ")

team_1_wins = 0
team_2_wins = 0

for i in range(10000):
    winner = who_wins(team_1, team_2)
    if winner == team_1:
        team_1_wins += 1
    else:
        team_2_wins += 1

if team_1_wins > team_2_wins:
    print(team_1 + " is the overall winner!  (" + str(team_1_wins) + " to " + str(team_2_wins) + ")")
elif team_2_wins > team_1_wins:
    print(team_2 + " is the overall winner!  (" + str(team_2_wins) + " to " + str(team_1_wins) + ")")
else:
    print("It's a tie!  (" + str(team_2_wins) + " to " + str(team_1_wins) + ")")
1
2
3
4
5
6
7
8
def my_function(name="Mark", school="UVa"):
    print(name + " goes to " + school)

my_function()
my_function("Steve")
my_function(school="VT")
my_function("Ann", "GMU")
my_function(school="GMU", name="Ann")

No Audio Today due to malfunction



Lecture 19 - Functions

Lecture Date: Wednesday, March 2

Since the first day we began coding in this class, we have been using (or “calling”) methods and functions to do various things:

Turtle Example
1
2
3
4
5
6
7
8
9
def draw_square(t, x, y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    rand_color = random.randint(0,len(colors)-1)
    t.color(colors[rand_color])
    for i in range(4):
        t.forward(100)
        t.left(90)

Everything with ()s after it are either methods or functions. Methods are, in effect, verbs in our programs. They tell objects to do things, change their state somehow, or provide us with information. In the code above, we are telling the t to do some specific tasks. Each one of those tasks is a method. A function is a bit of code that we separate out to do a particular task in a very similar way to a method, but the difference is it is not directly tied to an object and called with a period. def draw_square() above is a function. We are going to focus on functions in this class.

The things inside the parentheses are called parameters or arguments. Parameters provide extra information to the method to tell it how to perform a method or what data it should act on. So, when we tell t to do forward(100), we are saying that 100 is important information in order for the method to execute properly.

Methods form the basis of many programs for several reasons:

  • Code reuse: Often you’ll have a bit of code you want to use over and over. Methods make that happen in a very nice way, while also allowing for modification with parameters.
  • Organization: It’s a lot easier to see what’s going on when you read code when you have a set of methods saying what they do! Reading forward() and backward() in the Turtle code is completely understandable - having the code to do those things might not be. It’s an added layer of abstraction.
  • Testing: Once you verify a function is correct and working, you can move on to other parts of your system.

Another thing to know about functions is how parameters are passed into them.

  • Pass by Value: This is how non-mutable types are passed (or given) to a function. A full copy of the variable (value and everything) is sent to the function. Any changes made to the variable passed in ARE NOT REFLECTED back where the function was called.
  • Pass by Reference: This is how mutable types are passed to a method. A pointer (think “road sign”), which is a copy of the memory address of the object, is sent to the function. Any changes made to the variable passed in ARE REFLECTED back where the function was called.
Passing Parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Mark Sherriff (mss2x)

my_list = ['a', 'b', 'c', 'd']
my_value = 11

def change_a_value(some_value):
    print("Inside change_a_value()")
    print("   some_value starts as:", some_value)
    some_value *=2
    print("   some_value now is:", some_value)

def change_a_ref(some_list):
    print("Inside change_a_ref()")
    print("   some_list starts as:", some_list)
    some_list.append('x')
    print("   some_list now is:", some_list)

print("Starting the program!")
print("my_list starts as:", my_list)
change_a_ref(my_list)
print("my_list now is:", my_list)
print("my_value starts as:", my_value)
change_a_value(my_value)
print("my_value now is still:", my_value)

No Audio Today


Lecture 18 - Encryption Chase

Lecture Date: Monday, February 29

Today’s lecture is encrypted! Solve the puzzle up on the board / screen to figure out the proper shift value needed to decrypt the first clue. After that, follow the instructions you are given.

ytifd’x qjhyzwj, fx dtz hfs xjj, mfx gjjs jshwduyji. tsqd ymj knwxy ufwy tk ymj qjhyzwj hfs gj ijhwduyji zxnsl ymj hfjxfw hnumjw dtz ozxy zxji. yt xjj jajwdymnsl ymfy ytifd mfx yt tkkjw, dtz’qq mfaj yt zsijwyfpj f kjb yfxpx. knwxy, dtz rfd sty knsnxm jajwdymnsl izwnsl ymj qjhyzwj ynrj ytifd, fsi ymfy’x tpfd. dtz hfs it ymnx tajw ynrj, tw nk dtz hfs’y knsnxm ny, n’qq lnaj dtz ymj ijhwduynts pjdx. gzy ymfy’x st kzs. stb, vznjyqd, ufhp zu dtzw ymnslx fsi qjfaj ymj hqfxxwttr. tshj dtz fwj tzyxnij, lt yt myyux://xyfwithp.hx.anwlnsnf.jiz/qfbshmfxj ktw ymj sjcy xjy tk hqzjx.

No Audio Today


Lecture 17 - Encryption Overview

Lecture Date: Friday, February 26

What is encryption?

  • Change the form of data to conceal the meaning
  • hidden text

Why do we care? Why in CS?

  • Data Security
  • Do not want people to snoop on us
  • Do want to know we are talking to a particular person

Vocabulary:

  • plaintext - what we are hiding
  • ciphertext - what it looks like after we hide it
  • encrypt - go from plaintext to ciphertext
  • decrypt - go from ciphertext to plaintext
  • cipher - a technique for encrypting/decrypting
  • key - which variant of the cipher we are using

strs:

  • s[index] -> a character at that index (starting at 0)
  • len(s) -> how many chars there are
  • s += "w" -> add a w to the end of s

Assume s is a str. What is the index of the last character in s?

Ciphers to discuss:

  • Caesar
  • Vignere
  • Route
  • Scytale
  • Line-Word-Letter
oppish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Mark Sherriff (mss2x)

plaintext = input("What should we turn into oppish?: ")
plaintext = plaintext.lower()
vowels = ['a', 'e', 'i', 'o', 'u']
ciphertext = ""
for letter in plaintext:
    if letter in vowels:
        ciphertext += "op"

    ciphertext += letter

print(ciphertext)

index = 0
decrypted = ""

while index < len(ciphertext):
    if ciphertext[index] == 'o' and ciphertext[index + 1] == 'p':
        decrypted += ciphertext[index + 2]
        index += 3
    else:
        decrypted += ciphertext[index]
        index += 1

print(decrypted)



Lecture 15 - Test 1 Review

Lecture Date: Monday, February 22

See the Test 1 Review Guide for information on what’s on the test.

higher/lower
1
2
3
4
5
6
7
8
9
10
11
12
13
14
print("Think of a number between 1 and 100")

highest_value = 100
lowest_value = 1
answer = "no"

while highest_value != lowest_value:
    answer = input("Is the number bigger than " + str((highest_value + lowest_value) // 2) + "? ")
    if answer == "yes":
        lowest_value = ((highest_value + lowest_value) // 2) + 1
    else:
        highest_value = ((highest_value + lowest_value) // 2)

print("Your number is " + str(highest_value))


Test 1 Review Guide

Test date: Wednesday, February 24, 2016
Test location: normal lecture hall (you MUST go to your assigned section)
Test duration: 50 minutes (for 1110 and 1111)
Test format: writing on paper; bring pen/pencil (and nothing else)
Review session: Tuesday, February 23, 5:00-6:00 PM in Chem 402 (TA Office Hours are canceled during the review session)

Review Sheet

General Points:

  • There will be several coding questions; problem solving will be involved.
  • One coding question will be a “big” coding question, along the lines of a simple POTD.
  • Other coding questions will ask for a couple lines of code, such as an if else statement.
  • You should be able to read code and interpret what it is doing.
  • There will be a mix of other types of questions: multiple choice, short answer, fill-in-the-blank, etc.

Not on the test:

  • Drawing with the turtle will not be on the test
  • You do not have to know what to import to get access to various things (we will provide this for you)

CS in General:

  • Where do we see computers in the world?
  • What is ambiguity? What has that got to do with CS?
  • Why is it important for people to have a basic understanding of computing?

Primitives and Types:

  • Know the basic primitive types (int, float, bool, str)
  • Know the basic mutable collection types (list and dictionary)
  • Know how casting works (i.e., how to turn a str into an int)
  • Know the operators, including %, +=, /, //, and the various comparison and boolean operators (!=, not, or, and)
  • Know how to declare a variable and change what is inside it
  • Know how to get a random integer
  • Know how to get input from the user
  • Know the basic methods for use with lists and dictionaries
  • Know how to split a string into a list
  • Know how to access items in a list or dictionary

Decision structures:

  • Know how to write if, if-elif, and if-elif-else statements
  • Know how to interpret code that uses if, if-elif, and if-elif-else statements, including ifs inside of other ifs
  • Know how scope works: a variable declared inside an if cannot be seen if that if statement never executes

Loops:

  • Know how to write while loops - checking for a condition and also for verifying user input
  • Know how to write for loops - both using range() and over a list

Study Hints

Practice problems

Here are some problems you can practice with (however, we do not have the solutions available - you can probably Google for them):

  • Chapter 3 (p. 116): 5, 8, 9, 12, 13, 14
  • Chapter 4 (p. 161): 1, 2, 8, 11, 12
  • Chapter 7 (p. 334): 1, 2, 4, 6

Practice coding on paper

You’ll be writing code on paper. This feels different than writing it in PyCharm. You don’t want to be surprised by how different it feels. Practice writing code by hand.

A few small syntax errors is OK. But if you are really off we will take off points. Try to write correct code.

We’ll give you any imports you might need - so don’t worry about memorizing those.

Try re-solving the POTD and Lab problems on paper without looking at your code or textbook.

You can find more sample problems in Programming Challenges in the textbook. We do not, however, have the answer key to share with you.

Also remember that speed matters. 50 minutes is not a long time.

Practice reading code

We will show you code and ask you what it does. Practice thinking through code without running it.

Review the Lectures

Not everything in the book is equally important. Review the lecture notes to see what we emphasized. If you are confused by some point, check the audio. You might want to listen to the audio of the other instructor (the one you didn’t hear in class) so that you can get a different perspective on the material.