Skip to content

Commit c663c3c

Browse files
authored
Add files via upload
1 parent b831742 commit c663c3c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

defdemo.py

+23-24
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def factorial(n):
2020
"""Return the factorial of a positive integer.
2121
n -- The positive integer whose factorial is computed.
2222
"""
23-
total = 1
23+
result = 1
2424
for i in range(2, n+1):
25-
total *= i
26-
return total
25+
result *= i
26+
return result
2727

2828
# The return statement at the end of the function is used to
2929
# tell what the function gives back to whoever calls it. Now
@@ -33,44 +33,43 @@ def factorial(n):
3333

3434

3535
# All right, that out of the way, let's write some more functions that
36-
# operate on lists. First, find the largest element in the list. We do
37-
# this first by explicitly iterating over the elements of the list.
36+
# operate on lists. First, find the largest element in the list, same
37+
# as the Python built-in function max.
3838

3939

4040
def maximum(items):
4141
""" Return the largest item in the given items."""
4242
if not items: # Testing whether the sequence is empty
43-
# This is how you make a function crash when its arguments are invalid.
43+
# This is how you make a function crash for invalid arguments.
4444
raise ValueError("Empty list has no maximum")
4545
king = None
46-
for x in items:
47-
if king is None or x > king:
48-
king = x
46+
for e in items:
47+
if king is None or e > king:
48+
king = e
4949
return king
5050

5151

5252
# Next, a function that creates and returns another list where
5353
# each element equals the sum of the elements in the original
5454
# list up to that index.
5555

56-
def accumulate(seq):
57-
result = []
58-
total = 0
59-
for x in seq:
60-
total += x
56+
def accumulate(items):
57+
result, total = [], 0
58+
for e in items:
59+
total += e
6160
result.append(total)
6261
return result
6362

6463

6564
# Select precisely the elements that are larger than their predecessor.
6665

67-
def select_upsteps(seq):
68-
prev = None
69-
result = []
70-
for x in seq:
71-
if prev is None or x > prev:
72-
result.append(x)
73-
prev = x
66+
def select_upsteps(items):
67+
prev, result = None, []
68+
for e in items:
69+
if prev is None or e > prev:
70+
result.append(e)
71+
# The current element becomes previous element for the next round.
72+
prev = e
7473
return result
7574

7675

@@ -130,10 +129,10 @@ def fizzbuzz_translate(n):
130129
return str(n)
131130

132131

133-
def fizzbuzz(start=1, end=100):
134-
"""Play the game of fizzbuzz from start to end, inclusive."""
132+
def fizzbuzz(start=1, end=101):
133+
"""Play the game of fizzbuzz from start to end, exclusive."""
135134
result = []
136-
for n in range(start, end+1):
135+
for n in range(start, end):
137136
result.append(fizzbuzz_translate(n))
138137
result = ", ".join(result)
139138
return result

0 commit comments

Comments
 (0)