Skip to content

Commit 787d312

Browse files
author
Divesh
committed
Merge pull request #3 from PythonCHB/master
week 4 pull request
2 parents add1794 + 9519c62 commit 787d312

19 files changed

+1101
-56
lines changed

PossibleTopics.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This repository contains the demonstration software for the UW PCE Python
2+
class. The worked homework problems are in subdirectories.
3+

assertions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Precondition, postcondition, invariant
3+
4+
A while-loop can be derived by filling in this template
5+
with blocks S and T and condition P
6+
that establish invariant I and postcondition I and not P
7+
8+
S
9+
# pre: I
10+
while (P):
11+
# I
12+
T
13+
# post: I and not P
14+
"""
15+
16+
def quotient(n, d):
17+
"""
18+
Divide n by d, integer division by repeated subtraction
19+
n: dividend, d: divisor, q: quotient, r: remainder
20+
The definition of integer division is: n == q*d + r and r < d
21+
"""
22+
assert n >= 0 and d > 0 # function precondition
23+
r = n
24+
q = 0
25+
# assert n == q*d + r # loop precondition
26+
while (r >= d):
27+
# assert n == q*d + r # loop invariant
28+
r = r - d
29+
q = q + 1
30+
# assert n == q*d + r and r < d # postcondition
31+
return q
32+
33+
print '9/3 %s' % quotient(9,3)
34+
print '8/3 %s' % quotient(8,3)
35+
print '3/3 %s' % quotient(3,3)
36+
print '2/3 %s' % quotient(2,3)
37+
38+
# what happens if preconditions are violated?

ch_1/sentences.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I dropped the penny
2+
The penny dropped I

raw_strings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#! /usr/bin/python
2+
#
3+
#
4+
s = "Level 0\n\tLevel 1\n\t\tLevel 2"
5+
print s
6+
7+
# raw string, note the leading r
8+
r = r"Level 0\n\tLevel 1\n\t\tLevel 2"
9+
print r
10+
11+
# Regular expressions with delimited strings
12+
re_1 = "\\w\\d{2,3}"
13+
print re_1
14+
15+
re_2 = r"\w\d{2,3}"
16+
print re_2

schedule_speakers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! /usr/bin/env python
2+
#
3+
# This prorgram reads in a list of students from a file, and sorts them in
4+
# random order
5+
#
6+
import sys
7+
import random
8+
import string
9+
import csv
10+
11+
date_students = {0:"Oct 16th", 3:"Oct 23rd", 6:"Oct 30th", 9:"Nov 6th", \
12+
12:"Nov 13th", 16:"Nov 20th", 20:"Nov 27th", 24:"Dec 4th",
13+
28:"Dec 11th" }
14+
filename = "Student_list.csv"
15+
f = open(filename,"r")
16+
raw_student_list = csv.reader(f, delimiter='\t', quotechar='"')
17+
student_list = []
18+
for a_line in raw_student_list:
19+
# Column 0 is the student last name, Column 1 is the student first name
20+
# student's name (last name first)
21+
student_list.append( string.strip( a_line[0]+" "+a_line[1] ) )
22+
random.shuffle( student_list, )
23+
counter = 0
24+
for n in student_list :
25+
if counter in date_students.keys() :
26+
print "----", date_students[counter]
27+
counter += 1
28+
print counter, n
29+
print "----"
30+
31+
32+

0 commit comments

Comments
 (0)