Skip to content

Commit e1ce497

Browse files
committed
flake8 revisions
1 parent 37c4141 commit e1ce497

9 files changed

+239
-182
lines changed

coin_flip_streaks.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coin_flip_streaks.py
22

3-
# A program that investigates how often a streak of N head or N tails
3+
# A program that investigates how often a streak of N head or N tails
44
# comes up in a randomly generated list of heads and tails.
55

66
import random
@@ -22,13 +22,14 @@ def coin_flips(sample_size=100):
2222
"""
2323
SAMPLE_SIZE = sample_size
2424
results_list = []
25-
26-
for experiment_num in range(SAMPLE_SIZE): # Generate list of heads and tails values.
27-
coin_flip = random.choice(['H', 'T']) # Random 'H' or 'T'
25+
26+
# Generate list of heads and tails values.
27+
for experiment_num in range(SAMPLE_SIZE):
28+
# Random 'H' or 'T'
29+
coin_flip = random.choice(['H', 'T'])
2830
results_list.append(coin_flip)
29-
30-
return results_list
3131

32+
return results_list
3233

3334

3435
def streak_tracker(flips_list=[], streak_of=3):
@@ -48,51 +49,60 @@ def streak_tracker(flips_list=[], streak_of=3):
4849
A dictionary of the form: {'HEAD STREAKS': ###, 'TAIL STREAKS': ###},
4950
that displays how many of each type of streak occurred.
5051
"""
51-
STREAKS = {'HEADS': 0, 'TAILS': 0} # Keep a dictionary to track counts.
52-
HEAD_STREAKS, TAIL_STREAKS = (0, 0) # Keep track of the type of streak and how many.
53-
54-
if (flips_list != []): # CASE: Nonempty list
52+
# Keep a dictionary to track counts.
53+
STREAKS = {'HEADS': 0, 'TAILS': 0}
54+
# Keep track of the type of streak and how many.
55+
HEAD_STREAKS, TAIL_STREAKS = (0, 0)
56+
57+
# CASE: Nonempty list
58+
if (flips_list != []):
59+
# Initialize previous for first iteration.
5560
previous = None
61+
5662
for i, current in enumerate(flips_list):
5763
# Catch streaks & Reset dictionary values
5864
if (STREAKS['HEADS'] == streak_of):
59-
HEAD_STREAKS += 1
65+
HEAD_STREAKS += 1
6066
STREAKS['HEADS'] = 0
6167
elif (STREAKS['TAILS'] == streak_of):
6268
TAIL_STREAKS += 1
6369
STREAKS['TAILS'] = 0
64-
70+
6571
# CASE: Same as previous value
66-
if (current==previous):
67-
if (current=='H'):
72+
if (current == previous):
73+
if (current == 'H'):
6874
STREAKS['HEADS'] += 1
69-
elif (current=='T'):
75+
elif (current == 'T'):
7076
STREAKS['TAILS'] += 1
7177
# CASE: Different than previous value
72-
elif (current!=previous):
73-
if (current=='H'):
78+
elif (current != previous):
79+
if (current == 'H'):
7480
STREAKS['HEADS'] = 1
75-
elif (current=='T'):
81+
elif (current == 'T'):
7682
STREAKS['TAILS'] = 1
77-
78-
previous = flips_list[i] # Store current as previous for next iteration
83+
84+
# Store current as previous for next iteration
85+
previous = flips_list[i]
86+
7987
return {'HEAD STREAKS': HEAD_STREAKS, 'TAIL STREAKS': TAIL_STREAKS}
80-
81-
else: # CASE: Empty list
82-
return {'HEAD STREAKS': None, 'TAIL STREAKS': None}
8388

89+
# CASE: Empty list
90+
else:
91+
return {'HEAD STREAKS': None, 'TAIL STREAKS': None}
8492

8593

8694
# TESTS COMMENTED BELOW:
8795
'''
8896
coin_flip_results = coin_flips(1000)
8997
print(' '.join(coin_flip_results))
9098
91-
sample = [ 'H', 'H', 'T', 'H', 'T', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'T',
92-
'H', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'H' ]
99+
sample = [ 'H', 'H', 'T', 'H', 'T', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T',
100+
'H', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'T',
101+
'H', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H',
102+
'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'H']
93103
head_sample = ['H', 'H', 'H', 'H', 'H', 'H', 'H']
94104
tail_sample = ['T', 'T', 'T', 'T', 'T', 'T', 'T']
95105
96106
report_back = streak_tracker(coin_flip_results, 7)
97107
print(report_back)
98-
'''
108+
'''

collatz_sequence.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ def collatz(num):
1111
num : int
1212
The integer that starts the sequence.
1313
"""
14-
if (num == 1): # CASE: '1' -- end recursion
14+
# CASE: '1' -- end recursion
15+
if (num == 1):
1516
print("Sequence Complete!")
16-
17-
else: # CASE: not '1'
18-
if (num % 2 == 0): ## CASE: EVEN -- print and return (num // 2)
17+
# CASE: not '1'
18+
else:
19+
# CASE) Even -- print and return (num // 2)
20+
if (num % 2 == 0):
1921
new_num = num // 2
20-
else: ## CASE: ODD -- print and return (3 * number +1)
22+
# CASE) Odd -- print and return (3 * number +1)
23+
else:
2124
new_num = 3 * num + 1
22-
25+
2326
print(new_num)
2427
return collatz(new_num)
2528

2629

27-
2830
def netrunner():
2931
"""
3032
Driver code of the program.
@@ -33,14 +35,12 @@ def netrunner():
3335
# Accept user input
3436
integer = int(input("\nEnter an integer: "))
3537
collatz(integer)
36-
3738
except ValueError:
38-
print("Value Error! -- Please enter a positive or negative whole number.")
39+
print("Enter a positive or negative whole number.")
3940
netrunner()
40-
4141
except RecursionError:
42-
print("ZERO (booooo!)\nPlease enter a positive or negative whole number.")
43-
42+
print("Enter a positive or negative whole number. Zero is neither.")
43+
4444

45-
# Run program
46-
netrunner()
45+
# Run program
46+
netrunner()

comma_code.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@ def list_the_list(the_list=[]):
1515
list_string: str
1616
A string listing the items found in 'the_list'.
1717
"""
18-
if (the_list != []): # CASE: List is nonempty
18+
# CASE: List is nonempty
19+
if (the_list != []):
1920
list_string = ''
2021
list_length = len(the_list)
2122
for index, item in enumerate(the_list):
22-
item = str(item) # Force to string (just in case).
23-
if (index == 0): ## CASE: First item in the list.
23+
# Force to string
24+
item = str(item)
25+
26+
# CASE) First item in the list.
27+
if (index == 0):
2428
list_string += item
25-
elif (index == list_length - 1): ## CASE: Last item in the list.
29+
# CASE) Last item in the list.
30+
elif (index == list_length - 1):
2631
list_string += ', and ' + item + '.'
27-
else: ## CASE: Between first and last item in the list.
32+
# CASE) Between first and last item in the list.
33+
else:
2834
list_string += ', ' + item
29-
30-
else: # CASE: List is empty
35+
# CASE: List is empty
36+
else:
3137
list_string = '(An empty list)'
32-
3338
return list_string
3439

3540

36-
3741
# COMMENTED SOME TESTS BELOW:
3842
'''
3943
test_list = [11, 'eleven', 'twelve', 'cat', 'dog', 'cow', 'pig', False]
@@ -45,4 +49,4 @@ def list_the_list(the_list=[]):
4549
print(list_the_list(empty_list))
4650
print()
4751
print(list_the_list(list_in_a_list))
48-
'''
52+
'''

conways_game.py

+67-45
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,103 @@
22

33
# A demonstration of Conway's Game of Life:
44

5-
# An example of cellular automata (a set of rules governing
6-
# the behavior of a field made up of discrete cells).
7-
# Although the the rules are simple, there are several
8-
# surprising behaviors that emerge. Patterns can move,
9-
# self-repliate, and even mimic CPUs.
5+
# An example of cellular automata (a set of rules governing
6+
# the behavior of a field made up of discrete cells).
7+
# Although the the rules are simple, there are several
8+
# surprising behaviors that emerge. Patterns can move,
9+
# self-repliate, and even mimic CPUs.
1010

11-
import time, random, copy, sys
11+
import copy
12+
import random
13+
import sys
14+
import time
1215

1316
WIDTH, HEIGHT = (50, 25)
1417
ALIVE, DEAD = ('■', '□')
1518

1619
# Create a list of lists for the cells:
1720
next_cells = []
1821
for x in range(WIDTH):
19-
column = [] # Create a new column.
22+
# Create a new column.
23+
column = []
2024
for y in range(HEIGHT):
25+
# CASE) Add a 'living' cell.
2126
if (random.randint(0, 1) == 0):
22-
column.append(ALIVE) # Add a 'living' cell
27+
column.append(ALIVE)
28+
# CASE) Add a 'dead' cell.
2329
else:
24-
column.append(DEAD) # Add a 'dead' cell
25-
26-
next_cells.append(column) # next_cells is a list of column lists.
27-
28-
try:
29-
while True: # Main program loop
30-
print('\n\n\n\n\n') # Add seperate between each step.
30+
column.append(DEAD)
31+
# next_cells is a list of column lists.
32+
next_cells.append(column)
33+
34+
try:
35+
# Main Program Loop
36+
while True:
37+
# Add seperate between each step.
38+
print('\n\n\n\n\n')
3139
current_cells = copy.deepcopy(next_cells)
32-
33-
for y in range(HEIGHT): # Print current_cells
40+
41+
# Print current_cells
42+
for y in range(HEIGHT):
3443
for x in range(WIDTH):
35-
print(current_cells[x][y], end='') # Print # or space
36-
37-
print() # Print newline at the end of the row.
38-
39-
for x in range(WIDTH): # Calculate the next step's cells based on current step's cells.
40-
for y in range(HEIGHT): # Get neighboring coordinates:
41-
left = (x - 1) % WIDTH # '% WIDTH' ensures that left (coordinate) is always btw 0 and WIDTH - 1.
44+
# Print # or space
45+
print(current_cells[x][y], end='')
46+
# Print newline at the end of the row.
47+
print()
48+
49+
# Calc the next step's cells based on current step's cells.
50+
for x in range(WIDTH):
51+
# Get neighboring coordinates:
52+
for y in range(HEIGHT):
53+
# '% WIDTH' ensures that left is always btw 0 and WIDTH - 1.
54+
left = (x - 1) % WIDTH
4255
right = (x + 1) % WIDTH
4356
above = (y - 1) % HEIGHT
4457
below = (y + 1) % HEIGHT
45-
46-
num_neighbors = 0 # Count number of living neighbors.
47-
48-
if (current_cells[left][above] == ALIVE): # CASE: Top-left neighbor is alive.
58+
59+
# Count number of living neighbors.
60+
num_neighbors = 0
61+
62+
# CASE: Top-left neighbor is alive.
63+
if (current_cells[left][above] == ALIVE):
4964
num_neighbors += 1
50-
if (current_cells[x][above] == ALIVE): # CASE: Top neighbor is alive.
65+
# CASE: Top neighbor is alive.
66+
if (current_cells[x][above] == ALIVE):
5167
num_neighbors += 1
52-
if (current_cells[right][above] == ALIVE): # CASE: Top-right neighbor is alive.
68+
# CASE: Top-right neighbor is alive.
69+
if (current_cells[right][above] == ALIVE):
5370
num_neighbors += 1
54-
if (current_cells[left][y] == ALIVE): # CASE: Left neighbor is alive.
71+
# CASE: Left neighbor is alive.
72+
if (current_cells[left][y] == ALIVE):
5573
num_neighbors += 1
56-
if (current_cells[right][y] == ALIVE): # CASE: Right neighbor is alive.
74+
# CASE: Right neighbor is alive.
75+
if (current_cells[right][y] == ALIVE):
5776
num_neighbors += 1
58-
if (current_cells[left][below] == ALIVE): # CASE: Bottom-left neighbor is alive.
77+
# CASE: Bottom-left neighbor is alive.
78+
if (current_cells[left][below] == ALIVE):
5979
num_neighbors += 1
60-
if (current_cells[x][below] == ALIVE): # CASE: Bottom neighbor is alive.
80+
# CASE: Bottom neighbor is alive.
81+
if (current_cells[x][below] == ALIVE):
6182
num_neighbors += 1
62-
if (current_cells[right][below] == ALIVE): # CASE: Bottom-right neighbor is alive.
83+
# CASE: Bottom-right neighbor is alive.
84+
if (current_cells[right][below] == ALIVE):
6385
num_neighbors += 1
64-
65-
# Set cell based on Conway's Game of Life rules:
66-
67-
# CASE: Living cells wiht 2 or 3 neighbors stay alive.
68-
if (current_cells[x][y] == ALIVE and (num_neighbors == 2 or num_neighbors ==3)):
86+
87+
# Set cell based on Conway's Game of Life rules:
88+
89+
# CASE: Living cells with 2 or 3 neighbors stay alive.
90+
if (current_cells[x][y] == ALIVE and
91+
(num_neighbors == 2 or num_neighbors == 3)):
6992
next_cells[x][y] = ALIVE
70-
7193
# CASE: Dead cells with 3 neighbors become alive.
7294
elif (current_cells[x][y] == ' ' and num_neighbors == 3):
7395
next_cells[x][y] = ALIVE
74-
7596
# CASE: Everything else dies or stays dead.
7697
else:
7798
next_cells[x][y] = DEAD
78-
79-
time.sleep(1.5) # Add a pause to reduce flickering.
80-
99+
100+
# Add a pause to reduce flickering.
101+
time.sleep(1.5)
102+
81103
except KeyboardInterrupt:
82104
sys.exit()

0 commit comments

Comments
 (0)