Skip to content

Commit 8ff6d15

Browse files
committed
add the exercises I've done so far
1 parent 524b88e commit 8ff6d15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1528
-0
lines changed

abcdCAllStack.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def a():
2+
print('a() starts')
3+
b()
4+
d()
5+
print('a() returns')
6+
7+
def b():
8+
print('b() starts')
9+
c()
10+
print('b() returns')
11+
12+
def c():
13+
print('c() starts')
14+
print('c() returns')
15+
16+
def d():
17+
print('d() starts')
18+
print('d() returns')
19+
20+
a()

allMyCats1.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
print('Enter the name of cat 1:')
2+
catName1 = input()
3+
print('Enter the name of cat 2:')
4+
catName2 = input()
5+
print('Enter the name of cat 3:')
6+
catName3 = input()
7+
print('Enter the name of cat 4:')
8+
catName4 = input()
9+
print('Enter the name of cat 5:')
10+
catName5 = input()
11+
print('Enter the name of cat 6:')
12+
catName6 = input()
13+
print('The cat names are:')
14+
print(catName1 + ' ' + catName2 + ' ' + catName3 + ' ' + catName4 + ' ' +
15+
catName5 + ' ' + catName6)

allMyCats2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
catNames = []
2+
while True:
3+
print('Enter the name of a cat ' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):')
4+
name = input()
5+
if name == '':
6+
break
7+
catNames = catNames + [name]
8+
print('The cat names are: ')
9+
for name in catNames:
10+
print(' ' + name)

birthday.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
2+
3+
while True:
4+
print('Enter a name: (blank to quit)')
5+
name = input()
6+
if name == '':
7+
break
8+
9+
if name in birthdays:
10+
print(birthdays[name] + ' is the birthday of ' + name)
11+
else:
12+
print('I do not have birthday information for ' + name)
13+
print('What is their birthday?')
14+
bday = input()
15+
birthdays[name] = bday
16+
print('Birthday database updated')

bulletPointAdder.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! python3
2+
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
3+
# of each line of text on the clipboard.
4+
5+
import pyperclip
6+
7+
text = pyperclip.paste()
8+
9+
# Separate lines and add stars.
10+
11+
lines = text.split('\n')
12+
for i in range(len(lines)): # loop through all indexes in the "lines" list
13+
lines[i] = '* ' + lines[i] # add star to each string in the "lines" list
14+
15+
text = '\n'.join(lines)
16+
17+
pyperclip.copy(text)

catnapping.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
print('''Dear Alice,
2+
3+
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
4+
5+
Sincerely,
6+
Bob''')

characterCount.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
2+
3+
count = {}
4+
5+
for character in message:
6+
count.setdefault(character, 0)
7+
count[character] = count[character] + 1
8+
9+
print(count)

chess.py

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
goodBoard = {
2+
'1h': 'bking',
3+
'6c': 'wqueen',
4+
'2g': 'bbishop',
5+
'5h': 'bqueen',
6+
'3e': 'wking'}
7+
badBoardPrefix = {'1h': 'bking', '6c': 'wqueen',
8+
'2g': 'bbishop', '5h': 'queen', '3e': 'wking'}
9+
badBoardPiece = {'1h': 'bking', '6c': 'wqueen',
10+
'2g': 'bbishop', '5h': 'wace', '3e': 'wking'}
11+
badBoardPawnCount = {'1h': 'bking',
12+
'6c': 'wqueen',
13+
'2g': 'bbishop',
14+
'5h': 'wace',
15+
'3e': 'wking',
16+
'2a': 'wpawn',
17+
'2b': 'wpawn',
18+
'2c': 'wpawn',
19+
'2d': 'wpawn',
20+
'2e': 'wpawn',
21+
'2f': 'wpawn',
22+
'3g': 'wpawn',
23+
'2h': 'wpawn',
24+
'3h': 'wpawn'}
25+
26+
badBoardGridNumber = {'1h': 'bking',
27+
'6c': 'wqueen',
28+
'2g': 'bbishop',
29+
'9h': 'wpawn',
30+
'3e': 'wking'}
31+
32+
badBoardGridLetter = {'1h': 'bking',
33+
'6c': 'wqueen',
34+
'2g': 'bbishop',
35+
'3j': 'wpawn',
36+
'3e': 'wking'}
37+
badBoardKings = {
38+
'1h': 'bking',
39+
'2h': 'bking',
40+
'6c': 'wqueen',
41+
'2g': 'bbishop',
42+
'5h': 'bqueen',
43+
'3e': 'wking'}
44+
45+
goodBoardStartingPositions = {
46+
'1a': 'wrook',
47+
'1b': 'wknight',
48+
'1c': 'wbishop',
49+
'1d': 'wqueen',
50+
'1e': 'wking',
51+
'1f': 'wbishop',
52+
'1g': 'wknight',
53+
'1h': 'wrook',
54+
'2a': 'wpawn',
55+
'2b': 'wpawn',
56+
'2c': 'wpawn',
57+
'2d': 'wpawn',
58+
'2e': 'wpawn',
59+
'2f': 'wpawn',
60+
'2g': 'wpawn',
61+
'2h': 'wpawn',
62+
'8a': 'brook',
63+
'8b': 'bknight',
64+
'8c': 'bbishop',
65+
'8d': 'bqueen',
66+
'8e': 'bking',
67+
'8f': 'bbishop',
68+
'8g': 'bknight',
69+
'8h': 'brook',
70+
'7a': 'bpawn',
71+
'7b': 'bpawn',
72+
'7c': 'bpawn',
73+
'7d': 'bpawn',
74+
'7e': 'bpawn',
75+
'7f': 'bpawn',
76+
'7g': 'bpawn',
77+
'7h': 'bpawn',
78+
}
79+
80+
badBoardStartingPositionsExtra = {
81+
'1a': 'wrook',
82+
'3a': 'wrook',
83+
'1b': 'wknight',
84+
'1c': 'wbishop',
85+
'1d': 'wqueen',
86+
'1e': 'wking',
87+
'1f': 'wbishop',
88+
'1g': 'wknight',
89+
'1h': 'wrook',
90+
'2a': 'wpawn',
91+
'2b': 'wpawn',
92+
'2c': 'wpawn',
93+
'2d': 'wpawn',
94+
'2e': 'wpawn',
95+
'2f': 'wpawn',
96+
'2g': 'wpawn',
97+
'2h': 'wpawn',
98+
'8a': 'brook',
99+
'8b': 'bknight',
100+
'8c': 'bbishop',
101+
'8d': 'bqueen',
102+
'8e': 'bking',
103+
'8f': 'bbishop',
104+
'8g': 'bknight',
105+
'8h': 'brook',
106+
'7a': 'bpawn',
107+
'7b': 'bpawn',
108+
'7c': 'bpawn',
109+
'7d': 'bpawn',
110+
'7e': 'bpawn',
111+
'7f': 'bpawn',
112+
'7g': 'bpawn',
113+
'7h': 'bpawn',
114+
}
115+
116+
117+
def isValidChessBoard(board):
118+
isValid = True
119+
120+
validNumbers = ['1', '2', '3', '4', '5', '6', '7', '8']
121+
validLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
122+
pieceCount = {}
123+
for coord, value in board.items():
124+
color = value[0]
125+
piece = value[1:]
126+
pieceCount.setdefault(color, {})
127+
pieceCount[color].setdefault(piece, 0)
128+
pieceCount[color][piece] = pieceCount[color][piece] + 1
129+
coordNumber = coord[0]
130+
coordLetter = coord[1]
131+
132+
# all pieces must be on a valid space from 1a to 8h
133+
if coordNumber not in validNumbers:
134+
isValid = False
135+
if coordLetter not in validLetters:
136+
isValid = False
137+
138+
validColorPrefix = ['b', 'w']
139+
validPieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king']
140+
141+
totals = {}
142+
for key, pieces in pieceCount.items():
143+
# limit of 1 bking and 1 wking
144+
if pieces.get('king', 0) > 1:
145+
isValid = False
146+
# at most 8 pawns
147+
if pieces.get('pawn', 0) > 8:
148+
isValid = False
149+
150+
# each piece name begins with a w or b, followed by 'pawn', 'knight', 'bishop', 'rook', 'queen', 'king'
151+
if key not in validColorPrefix:
152+
isValid = False
153+
for piece, count in pieces.items():
154+
if piece not in validPieces:
155+
isValid = False
156+
totals.setdefault(key, 0)
157+
totals[key] = totals[key] + count
158+
159+
for total in totals.values():
160+
# at most 16 pieces per player
161+
if total > 16:
162+
isValid = False
163+
164+
# must return boolean
165+
return isValid
166+
167+
168+
print(isValidChessBoard(goodBoard))
169+
print(isValidChessBoard(badBoardPrefix))
170+
print(isValidChessBoard(badBoardPiece))
171+
print(isValidChessBoard(badBoardGridNumber))
172+
print(isValidChessBoard(badBoardGridLetter))
173+
print(isValidChessBoard(badBoardKings))
174+
print(isValidChessBoard(badBoardPawnCount))
175+
print(isValidChessBoard(goodBoardStartingPositions))
176+
print(isValidChessBoard(badBoardStartingPositionsExtra))

coinFlipStreaks.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import random
2+
3+
numberOfStreaks = 0
4+
5+
def findStreak(flips):
6+
streaksCount = 0
7+
activeStreakLength = 0
8+
previousItem = None
9+
for index, currentItem in enumerate(flips):
10+
if currentItem == previousItem:
11+
activeStreakLength += 1
12+
else:
13+
activeStreakLength = 0
14+
15+
if activeStreakLength == 5:
16+
streaksCount += 1
17+
previousItem = currentItem
18+
19+
return streaksCount
20+
21+
for experimentNumber in range(10000):
22+
flips = []
23+
for flipNumber in range(100):
24+
currentFlip = random.randint(0, 1)
25+
flips.append(currentFlip)
26+
27+
28+
activeExperimentStreaks = findStreak(flips)
29+
if activeExperimentStreaks > 0:
30+
numberOfStreaks += 1
31+
32+
33+
34+
print('numberOfStreaks is: ' + str(numberOfStreaks))
35+
print('Chance of streak: %s%%' % (numberOfStreaks / 10000))

collatz.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
def collatz(number):
4+
if number % 2 == 0:
5+
answer = number // 2
6+
print(answer)
7+
return answer
8+
else:
9+
answer = 3 * number + 1
10+
print(answer)
11+
return answer
12+
13+
try:
14+
while True:
15+
global number
16+
number = None
17+
print('Enter number:')
18+
while number == None:
19+
try:
20+
number = int(input())
21+
except ValueError:
22+
print('You must enter an integer.')
23+
24+
while number != 1:
25+
number = collatz(number)
26+
27+
except KeyboardInterrupt:
28+
sys.exit()

commaCode.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spam = ['apples', 'bananas', 'tofu', 'cats']
2+
3+
def stringifier(listToStringify):
4+
newString = ''
5+
for index, element in enumerate(listToStringify):
6+
if index == len(listToStringify) - 1:
7+
newString += 'and '
8+
9+
newString += element
10+
if index < len(listToStringify) - 1:
11+
newString += ', '
12+
return newString
13+
14+
print(stringifier(spam))

commas.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
commaRegex = re.compile(r'''(
3+
(^\d{1,3})# First group of numbers will be 1-3 digits long and at the start of the string
4+
(,\d{3})* # Any subsequent groups will be lead by a comma
5+
$ # the end of the string must come immediately after the full pattern.
6+
)''', re.VERBOSE)
7+
8+
print(commaRegex.search('42').group())
9+
print(commaRegex.search('1,234').group())
10+
print(commaRegex.search('6,368,745').group())
11+
12+
print(commaRegex.search('12,34,567') == None)
13+
print(commaRegex.search('1234') == None)

0 commit comments

Comments
 (0)