Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 14 additions & 47 deletions exercises/secret-handshake/example.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,17 @@
gestures = ['wink', 'double blink', 'close your eyes', 'jump']


def handshake(s):
s = list(sanitize(s))
s.reverse()
seq = []
lim = len(s) if len(s) <= len(gestures) else len(gestures)
for i1 in range(lim):
if s[i1] == '1':
seq.append(gestures[i1])
if len(s) == 5:
seq.reverse()
return seq


def code(seq):
if not seq or set(seq) - set(gestures):
return '0'
s = find_subseq(seq)
if not s:
s = ['1'] + find_subseq(reversed(seq))
return "".join(s)


def sanitize(s):
if not(isinstance(s, int) or isinstance(s, str)):
raise TypeError('Unknown type')
if isinstance(s, int):
if s < 0:
return ""
s = bin(s)[2:]
elif set(s) - set(['0', '1']):
return ""
if len(s) > 5:
raise ValueError('Binary string too long')
return "0" * (len(gestures) - len(s)) + s


def find_subseq(seq):
idx = 0
s = []
for g in seq:
if g not in gestures[idx:]:
return []
newidx = gestures.index(g, idx) + 1
s.extend(['0'] * (newidx - idx - 1) + ['1'])
idx = newidx
s.reverse()
return s
def handshake(code):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your names for parameters, what do you think of updating https://github.com/exercism/python/blob/master/exercises/secret-handshake/secret_handshake.py for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

actions = [gestures[i] for i in range(len(gestures))
if (code >> i) % 2 == 1]
return actions if code < 16 else list(reversed(actions))


def secret_code(actions):
actions = [a for a in actions if a in gestures]
result = sum(1 << i for i, action in enumerate(gestures)
if action in actions)
if len(actions) > 1 and (gestures.index(actions[0]) >
gestures.index(actions[1])):
result += 16
return result
4 changes: 2 additions & 2 deletions exercises/secret-handshake/secret_handshake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def handshake(number):
def handshake(code):
pass


def code(secret_code):
def secret_code(actions):
pass
81 changes: 56 additions & 25 deletions exercises/secret-handshake/secret_handshake_test.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,80 @@
import unittest

from secret_handshake import handshake, code
from secret_handshake import handshake, secret_code


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class HandshakeTest(unittest.TestCase):
def test_shake_int(self):
self.assertEqual(handshake(9), ['wink', 'jump'])
def test_wink_for_1(self):
self.assertEqual(handshake(1), ['wink'])

def test_double_blink_for_10(self):
self.assertEqual(handshake(2), ['double blink'])

def test_close_your_eyes_for_100(self):
self.assertEqual(handshake(4), ['close your eyes'])

def test_jump_for_1000(self):
self.assertEqual(handshake(8), ['jump'])

def test_combine_two_actions(self):
self.assertEqual(handshake(3), ['wink', 'double blink'])

def test_reverse_two_actions(self):
self.assertEqual(handshake(19), ['double blink', 'wink'])

def test_reversing_one_action_gives_the_same_action(self):
self.assertEqual(handshake(24), ['jump'])

def test_shake_bin1(self):
self.assertEqual(
handshake('10110'), ['close your eyes', 'double blink'])
def test_reversing_no_actions_still_gives_no_actions(self):
self.assertEqual(handshake(16), [])

def test_shake_bin2(self):
self.assertEqual(handshake('101'), ['wink', 'close your eyes'])
def test_all_possible_actions(self):
self.assertEqual(handshake(15), ['wink',
'double blink',
'close your eyes',
'jump'])

def test_shake_negative_int(self):
self.assertEqual(handshake(-9), [])
def test_reverse_all_possible_actions(self):
self.assertEqual(handshake(31), ['jump',
'close your eyes',
'double blink',
'wink'])

def test_shake_bin_invalid(self):
self.assertEqual(handshake('121'), [])
def test_do_nothing_for_zero(self):
self.assertEqual(handshake(0), [])

def test_unknown_action(self):
self.assertEqual(code(['wink', 'sneeze']), '0')
# Track-specific tests

@unittest.skip('extra-credit')
def test_code1(self):
self.assertEqual(code(['close your eyes', 'jump']), '1100')
self.assertEqual(secret_code(['close your eyes', 'jump']), 12)

@unittest.skip('extra-credit')
def test_code2(self):
self.assertEqual(code(['wink', 'double blink']), '11')
self.assertEqual(secret_code(['wink', 'double blink']), 3)

@unittest.skip('extra-credit')
def test_code3(self):
self.assertEqual(code(['jump', 'double blink']), '11010')
self.assertEqual(secret_code(['jump', 'double blink']), 26)

def test_composition1(self):
self.assertEqual(code(handshake(27)), '11011')
@unittest.skip('extra-credit')
def test_reversible1(self):
self.assertEqual(secret_code(handshake(27)), 27)

def test_composition2(self):
self.assertEqual(code(handshake(1)), '1')
@unittest.skip('extra-credit')
def test_reversible2(self):
self.assertEqual(secret_code(handshake(1)), 1)

def test_composition3(self):
self.assertEqual(code(handshake('111')), '111')
@unittest.skip('extra-credit')
def test_reversible3(self):
self.assertEqual(secret_code(handshake(7)), 7)

def test_composition4(self):
@unittest.skip('extra-credit')
def test_reversible4(self):
inp = ['wink', 'double blink', 'jump']
self.assertEqual(handshake(code(inp)), inp)
self.assertEqual(handshake(secret_code(inp)), inp)


if __name__ == '__main__':
Expand Down