Skip to content

Commit

Permalink
some easy puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
jmegner committed Jun 26, 2015
1 parent 3dc997b commit fd57621
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bird-language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
VOWELS = "aeiouy"

def translate(phrase):
translatedPhrase = ''
charIdx = 0

while charIdx < len(phrase):
char = phrase[charIdx]

translatedPhrase += char

if char == ' ':
charIdx += 1
elif char in VOWELS:
# increment three to get past next 2 vowels
charIdx += 3
else:
# increment two to get past next 1 vowels
charIdx += 2

return translatedPhrase

if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert translate("hieeelalaooo") == "hello", "Hi!"
assert translate("hoooowe yyyooouuu duoooiiine") == "how you doin", "Joey?"
assert translate("aaa bo cy da eee fe") == "a b c d e f", "Alphabet"
assert translate("sooooso aaaaaaaaa") == "sos aaa", "Mayday, mayday"
21 changes: 21 additions & 0 deletions good-radix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def checkio(numStr):
for radix in range(1, 37):
try:
num = int(numStr, radix)
except ValueError:
continue

if num % (radix - 1) == 0:
return radix

return 0


if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("18") == 10, "Simple decimal"
assert checkio("1010101011") == 2, "Any number is divisible by 1"
assert checkio("222") == 3, "3rd test"
assert checkio("A23B") == 14, "It's not a hex"
assert checkio("IDDQD") == 0, "k is not exist"
print('Local tests done')
11 changes: 11 additions & 0 deletions hamming-distance2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def checkio(n, m):
# xor to get bit differences, then sum them with help of map
# string slice starts at 2 to get rid of leading "0b" from bin()
return sum(map(int, bin(n ^ m)[2:]))


if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(117, 17) == 3, "First example"
assert checkio(1, 2) == 2, "Second example"
assert checkio(16, 15) == 5, "Third example"
4 changes: 4 additions & 0 deletions restricted-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def checkio(data):
if data:
return data[0] + checkio(data[1:])
return 0
26 changes: 26 additions & 0 deletions rotate-hole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def rotate(goodCannons, usedPipeNumbers):
goodRotations = []

for rotateCount in range(len(goodCannons)):
rotationGood = True

for cannonIdx in usedPipeNumbers:
if not goodCannons[cannonIdx]:
rotationGood = False
break

if rotationGood:
goodRotations.append(rotateCount)

# rotate for next loop iteration
goodCannons.insert(0, goodCannons.pop())

return goodRotations


if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8], "Example"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == [], "Mission impossible"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0], "Don't touch it"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5], "Two cannonballs in the same pipe"
30 changes: 30 additions & 0 deletions weak-point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def weak_point(matrix):
def idxAndListToSum(idxAndListPair): return sum(idxAndListPair[1])

weakestRowIdx, weakestRowVals = min(
enumerate(matrix),
key = idxAndListToSum)

weakestColIdx, weakestColVals = min(
enumerate(zip(*matrix)),
key = idxAndListToSum)

return weakestRowIdx, weakestColIdx


if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(weak_point([[1]]), (list, tuple)), "The result should be a list or a tuple"
assert list(weak_point([[7, 2, 7, 2, 8],
[2, 9, 4, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [3, 3], "Example"
assert list(weak_point([[7, 2, 4, 2, 8],
[2, 8, 1, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [1, 2], "Two weak point"
assert list(weak_point([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])) == [0, 0], "Top left"
21 changes: 21 additions & 0 deletions word-pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def check_command(patternNum, command):
# convert to binary string, strip "0b", left-pad with 0's
requiredLetterPattern = bin(patternNum)[2:].rjust(len(command), '0')

commandLetterPattern = ''.join(map(
lambda char: '1' if char.isalpha() else '0',
command))

return commandLetterPattern == requiredLetterPattern

if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert check_command(42, "12a0b3e4") == True, "42 is the answer"
assert check_command(101, "ab23b4zz") == False, "one hundred plus one"
assert check_command(0, "478103487120470129") == True, "Any number"
assert check_command(127, "Checkio") == True, "Uppercase"
assert check_command(7, "Hello") == False, "Only full match"
assert check_command(8, "a") == False, "Too short command"
assert check_command(5, "H2O") == True, "Water"
assert check_command(42, "C2H5OH") == False, "Yep, this is not the Answer"

0 comments on commit fd57621

Please sign in to comment.