Skip to content

Commit 9e7616c

Browse files
author
Dadsaster
committed
Added two debugging puzzles
1 parent bb006ec commit 9e7616c

38 files changed

+1291
-0
lines changed

basic/debug1.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Basic string exercises
2+
# Fill in the code for the functions below. main() is already set up
3+
# to call the functions with a few different inputs,
4+
# printing 'OK' when each function is correct.
5+
# The starter code for each function includes a 'return'
6+
# which is just a placeholder for your code.
7+
# It's ok if you do not complete all the functions, and there
8+
# are some additional functions to try in string2.py.
9+
10+
11+
# A. donuts
12+
# Given an int count of a number of donuts, return a string
13+
# of the form 'Number of donuts: <count>', where <count> is the number
14+
# passed in. However, if the count is 10 or more, then use the word 'many'
15+
# instead of the actual count.
16+
# So donuts(5) returns 'Number of donuts: 5'
17+
# and donuts(23) returns 'Number of donuts: many'
18+
def donuts(count):
19+
if count < 10:
20+
return 'Number of donuts: ' + count
21+
else:
22+
return 'Number of donuts: many'
23+
24+
# B. both_ends
25+
# Given a string s, return a string made of the first 2
26+
# and the last 2 chars of the original string,
27+
# so 'spring' yields 'spng'. However, if the string length
28+
# is less than 2, return instead the empty string.
29+
def both_ends(s):
30+
if len(s) < 2:
31+
return ''
32+
first2 = s[0:2]
33+
last2 = s[-2:]
34+
return first2, last2
35+
36+
37+
# C. fix_start
38+
# Given a string s, return a string
39+
# where all occurences of its first char have
40+
# been changed to '*', except do not change
41+
# the first char itself.
42+
# e.g. 'babble' yields 'ba**le'
43+
# Assume that the string is length 1 or more.
44+
# Hint: s.replace(stra, strb) returns a version of string s
45+
# where all instances of stra have been replaced by strb.
46+
def fix_start(s)
47+
front = s[0]
48+
back = s[1:]
49+
fixed_back = back.replace(front, '*')
50+
return front + fixed_back
51+
52+
53+
# D. MixUp
54+
# Given strings a and b, return a single string with a and b separated
55+
# by a space '<a> <b>', except swap the first 2 chars of each string.
56+
# e.g.
57+
# 'mix', pod' -> 'pox mid'
58+
# 'dog', 'dinner' -> 'dig donner'
59+
# Assume a and b are length 2 or more.
60+
def mix_up(a b):
61+
a_swapped = b[:2] + a[2:]
62+
b_swapped = a[:2] + b[2:]
63+
return a_swapped + ' ' + b_swapped
64+
65+
66+
# Provided simple test() function used in main() to print
67+
# what each function returns vs. what it's supposed to return.
68+
def test(got, expected):
69+
if got = expected:
70+
prefix = ' OK '
71+
else:
72+
prefix = ' X '
73+
print('{} got: {} expected: {}'.format(
74+
prefix, repr(got), repr(expected)
75+
)
76+
)
77+
78+
79+
# Provided main() calls the above functions with interesting inputs,
80+
# using test() to check if each result is correct or not.
81+
def main():
82+
print('donuts')
83+
# Each line calls donuts, compares its result to the expected for that call.
84+
test(donuts(4), 'Number of donuts: 4')
85+
test(donuts(9), 'Number of donuts: 9')
86+
test(donuts(10), 'Number of donuts: many')
87+
test(donuts(99), 'Number of donuts: many')
88+
89+
print()
90+
print('both_ends')
91+
test(both_ends('spring'), 'spng')
92+
test(both_ends('Hello'), 'Helo')
93+
test(both_ends('a'), '')
94+
test(both_ends('xyz'), 'xyyz')
95+
96+
97+
print()
98+
print('fix_start')
99+
test(fix_start('babble'), 'ba**le')
100+
test(fix_start('aardvark'), 'a*rdv*rk')
101+
test(fix_start('google'), 'goo*le')
102+
test(fix_start('donut'), 'donut')
103+
104+
print()
105+
print('mix_up')
106+
test(mix_up('mix', 'pod'), 'pox mid')
107+
test(mix_up('dog', 'dinner'), 'dig donner')
108+
test(mix_up('gnash', 'sport'), 'spash gnort')
109+
test(mix_up('pezzy', 'firm'), 'fizzy perm')
110+
111+
112+
# Standard boilerplate to call the main() function.
113+
if __name__ == '__main__':
114+
main()

basic/debug2.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Basic list exercises
2+
# Fill in the code for the functions below. main() is already set up
3+
# to call the functions with a few different inputs,
4+
# printing 'OK' when each function is correct.
5+
# The starter code for each function includes a 'return'
6+
# which is just a placeholder for your code.
7+
# It's ok if you do not complete all the functions, and there
8+
# are some additional functions to try in list2.py.
9+
10+
# A. match_ends
11+
# Given a list of strings, return the count of the number of
12+
# strings where the string length is 2 or more and the first
13+
# and last chars of the string are the same.
14+
# Note: python does not have a ++ operator, but += works.
15+
def match_ends(words):
16+
count = 0
17+
for word in words:
18+
if len(word) >= 2 and word[0] == word[-1]:
19+
count += count + 1
20+
return count
21+
22+
23+
# B. front_x
24+
# Given a list of strings, return a list with the strings
25+
# in sorted order, except group all the strings that begin with 'x' first.
26+
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
27+
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
28+
# Hint: this can be done by making 2 lists and sorting each of them
29+
# before combining them.
30+
def front_x(words):
31+
x_list = []
32+
other_list = []
33+
for w in words:
34+
if w.startswith(x):
35+
x_list.append(w)
36+
else:
37+
other_list.append(w)
38+
return sorted(x_list) + sorted(other_list)
39+
40+
41+
42+
43+
def last(a):
44+
return a[-1]
45+
46+
# C. sort_last
47+
# Given a list of non-empty tuples, return a list sorted in increasing
48+
# order by the last element in each tuple.
49+
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
50+
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
51+
# Hint: use a custom key= function to extract the last element form each tuple.
52+
def sort_last(tuples):
53+
print sorted(tuples, key=last)
54+
55+
56+
# Simple provided test() function used in main() to print
57+
# what each function returns vs. what it's supposed to return.
58+
def test(got, expected):
59+
if got == expected:
60+
prefix = ' OK '
61+
else:
62+
prefix = ' X '
63+
print('{} got: {} expected: {}'.format(prefix, repr(got), repr(expected)))
64+
65+
66+
# Calls the above functions with interesting inputs.
67+
def main():
68+
print('match_ends')
69+
test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
70+
test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
71+
test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
72+
73+
print()
74+
print('front_x')
75+
test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
76+
['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
77+
test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
78+
['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
79+
test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
80+
['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
81+
82+
83+
print()
84+
print('sort_last')
85+
test(sort_last([(1, 3), (3, 2), (2, 1)]),
86+
[(2, 1), (3, 2), (1, 3)])
87+
test(sort_last([(2, 3), (1, 2), (3, 1)]),
88+
[(3, 1), (1, 2), (2, 3)])
89+
test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
90+
[(2, 2), (1, 3), (3, 4, 5), (1, 7)])
91+
92+
93+
if __name__ == '__main__':
94+
main()

basic/magic.py

Whitespace-only changes.

crypt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# simple substitution cypher
2+
3+
numbers = range(1,27)
4+
alphabet = "abcdefghijklmnopqrstuvwxyz"
5+
alpha_dict = dict(zip(alphabet, numbers))
6+
numbers_dict = dict(zip(numbers, alphabet))
7+
# print(alpha_dict, numbers_dict)
8+
def crypt(string, shift):
9+
output = ''
10+
for char in string:
11+
if char.lower() in alphabet:
12+
offset = alpha_dict[char.lower()] + shift
13+
if offset > 26:
14+
offset = offset % 26
15+
print(offset, numbers_dict[offset])
16+
output += numbers_dict[offset]
17+
else:
18+
output += char
19+
return output
20+
print(crypt('Zest string!', 5))

picker.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import random
2+
3+
def picker(list):
4+
list_out = []
5+
list_in = list[:]
6+
while list:
7+
pick = random.choice(list)
8+
list_out.append(pick)
9+
index = list.index(pick)
10+
list.pop(index)
11+
print(dict(zip(list_in, list_out)))
12+
13+
14+
list = ['a', 'b', 'c', 'd', 'e']
15+
print(picker(list))

teaching/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teaching/.idea/misc.xml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teaching/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teaching/.idea/teaching.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teaching/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)