Skip to content

Commit 96eabe4

Browse files
authored
Add files via upload
Edited the examples to better conform to PEP 8.
1 parent 7d9c658 commit 96eabe4

File tree

14 files changed

+232
-118
lines changed

14 files changed

+232
-118
lines changed

cards.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def outranks(self, other, trump=None):
9393
# concepts are sufficiently high level. One from your graded labs:
9494

9595

96-
def winning_card(cards, trump=None, winner_so_far=None):
97-
for card in cards:
96+
def winning_card(cards_, trump=None, winner_so_far=None):
97+
for card in cards_:
9898
if card.outranks(winner_so_far, trump):
9999
winner_so_far = card
100100
return winner_so_far

comprehensions.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Since a comprehension can be made out of any existing sequence,
1010
# more complex lists can be created out of existing comprehensions.
1111

12-
# The members of previous list that contain are between 500 and 2000.
12+
# Elements of the previous list that are between 500 and 2000.
1313

1414
b = [x for x in a if 500 < x < 2000]
1515
print(b)
@@ -18,7 +18,6 @@
1818
# For example, let's find all the Unicode characters that are lowercase
1919
# letters and produce a list of them.
2020

21-
2221
letter_category = ud.category('a')
2322
c = [chr(c) for c in range(100000)
2423
if ud.category(chr(c)) == letter_category]
@@ -107,8 +106,6 @@
107106
# This technique allows us to partition a sequence into another sequence
108107
# of its consecutive elements, either overlapping or not.
109108

110-
words = "Hello there, world! How are you doing today?".split()
111-
112109
# Overlapping sequences of 3 consecutive elements by having the range
113110
# of n skip by default 1.
114111
u2 = [" ".join([words[x] for x in range(n, n + 3)])

conditions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def sign(a):
2-
if (a < 0):
2+
if a < 0:
33
return -1
44
elif a > 0:
55
return +1

defdemo.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def factorial(n):
1616
"""Return the factorial of a positive integer.
1717
n -- The positive integer whose factorial is computed.
1818
"""
19-
result = 1
19+
total = 1
2020
for i in range(2, n + 1):
21-
result *= i
22-
return result
21+
total *= i
22+
return total
2323

2424
# The return statement at the end of the function is used to
2525
# tell what the function gives back to whoever calls it. Now
@@ -43,8 +43,8 @@ def factorial(n):
4343
# assigned to point some place else.
4444

4545
f = factorial # copy a reference to the function object
46-
result = f(30) # make an actual function call
47-
print(result) # 265252859812191058636308480000000
46+
factorial_30 = f(30) # make an actual function call
47+
print(factorial_30) # 265252859812191058636308480000000
4848

4949
# All right, that out of the way, let's write some more functions that
5050
# operate on lists. First, find the largest element in the list. We do
@@ -53,7 +53,7 @@ def factorial(n):
5353

5454
def maximum(seq):
5555
""" Return the largest element in sequence."""
56-
if seq == []:
56+
if not seq:
5757
raise ValueError("Empty list has no maximum")
5858
first = True
5959
for x in seq:
@@ -194,15 +194,16 @@ def roll_dice(rolls, faces=6):
194194

195195
total1 = roll_dice(10, 12)
196196
print(f"Rolling a 12-sided die 10 times gave a total of {total1}.")
197+
197198
# With a default parameter, we don't need to give its value.
198199
total2 = roll_dice(6)
199200
print(f"Rolling a 6-sided die 10 times gave a total of {total2}.")
200201

201202

202-
# Fizzbuzz is a game where you try to list the numbers from start to end
203-
# but so that if a number is divisible by 3, you say "fizz", and if a
204-
# number is divisible by 5, you say "buzz", and if by both 3 and 5, you
205-
# say "fizzbuzz".
203+
# Fizzbuzz is a mental game where you try to list the numbers from
204+
# start to end but so that if a number is divisible by 3, you say
205+
# "fizz", and if a number is divisible by 5, you say "buzz", and
206+
# if divisible by both 3 and 5, you say "fizzbuzz".
206207

207208
def fizzbuzz_translate(n):
208209
"""Convert positive integer n to its fizzbuzz representation."""

first.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
a = 42
2121
b = a * 2 - 3
2222

23-
# In addition to numbers, Python can also represent values that are text,
24-
# called strings. They are given between either single or double quotes.
25-
# You can take your pick which type of quote you prefer. Using one type
26-
# allows the use of other type inside the string.
23+
# Python can also represent and handle text strings. They are given
24+
# between either single or double quotes. You can take your pick
25+
# which type of quote you prefer. Using one kind of quote allows
26+
# the use of other type inside the string.
2727

2828
c = 'Hello world'
2929
d = "Another 'string' given between double quotes"
@@ -84,13 +84,14 @@
8484
print(math.pow(x, math.pi))
8585

8686
# Python even supports complex numbers right out of the box, and its
87-
# arithmetic operations just do the correct complex number arithmetic.
87+
# arithmetic operations just do the complex number arithmetic.
8888

8989
z1 = complex(4, -2) # 4 - 2j
9090
z2 = complex(-3, 1) # -3 + j
9191
z3 = z1 * z2 # -10+10j
9292
print(f"The real part is {z3.real} and the imaginary part is {z3.imag}.")
9393

94+
# However, Python uses floating point arithmetic for complex numbers.
9495

9596
f1 = Fraction(-2, 7) # a fraction from two integers
9697
f2 = Fraction('5/9') # a fraction from a string
@@ -100,17 +101,17 @@
100101
# Joe and Moe are peeling potatoes. Working by himself, Joe could peel
101102
# the entire pile in three hours, whereas Moe could peel the same pile
102103
# in five hours. How long will it take for these two men to peel the
103-
# potatoes if they work together? (No, the answer is not four hours,
104-
# the average of three and five.)
104+
# potatoes if they work together? (No, the answer is *not* four hours,
105+
# the simple average of three and five.)
105106

106-
joe = Fraction(1, 3)
107-
moe = Fraction(1, 5)
108-
together = joe + moe
107+
joe_speed = Fraction(1, 3)
108+
moe_speed = Fraction(1, 5)
109+
together = joe_speed + moe_speed
109110
time = 1 / together
110111
print(f"Together, Joe and Moe finish in {time} hours.")
111112

112-
# Remember that strings and integers are not the same thing, even as they
113-
# can be trivially converted to one another.
113+
# Remember that strings and integers are not the same thing, even as
114+
# they can be trivially converted to one another.
114115

115116
a = 22 + 22
116117
print(a) # 44
@@ -136,8 +137,8 @@
136137
print(f"After swap, x equals {x}, and y equals {y}.") # 42 17
137138

138139
# In basic arithmetic, division is handled with a couple of different
139-
# operators depending on what kind of division you want. Couple of things
140-
# about it can first be a bit surprising.
140+
# operators depending on what kind of division you want. Couple of
141+
# things about them can first be surprising.
141142

142143
print(11 / 4) # 2.75, the usual everyday division
143144
print(11 / -4) # -2.75
@@ -155,4 +156,4 @@
155156
idontknow = None
156157
print(idontknow)
157158
del idontknow
158-
print(idontknow) # crash with NameError
159+
print(idontknow) # crash with NameError

functional.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def rising_power(n, k):
5151
# needing more than constant amount of extra memory.
5252

5353

54-
def is_eventually_periodic(f, x, giveup=1000):
54+
def is_eventually_periodic(f, x, give_up=1000):
5555
tortoise, hare = x, f(x)
56-
while tortoise != hare and giveup > 0:
56+
while tortoise != hare and give_up > 0:
5757
tortoise = f(tortoise)
5858
hare = f(f(hare))
59-
giveup -= 1
59+
give_up -= 1
6060
return tortoise == hare
6161

6262
# Next, let's examine how functions can be given to other functions

0 commit comments

Comments
 (0)