Perhaps CheckiO should not have Fox, goose and bag of beans puzzle mission yet.
How about implementing the solution in Python?
specification:
wolf_goat_cabbage(purchases, ng_list) == [go1, return1, go2, return2,...]
example:
wolf_goat_cabbage({'wolf', 'goat', 'cabbage'}, [{'wolf', 'goat'}, {'goat', 'cabbage'}])
== ['goat', '', 'cabbage', 'goat', 'wolf', '', 'goat']
initial code:
def wolf_goat_cabbage(purchases, ng_list):
return []
if __name__ == '__main__':
def checker(solution, purchases, ng_list, min_len):
answer = solution(purchases, ng_list)
if not isinstance(answer, list):
print('wrong data type :', answer)
return False
dif = set(answer) - (purchases | {''})
if dif:
print('wrong cargo :', dif)
return False
if len(answer) > min_len:
print(f'too much steps : {len(answer)} > {min_len}')
return False
if not (answer or min_len):
return True
bank_a, bank_b = purchases | {''}, set()
for i, p in enumerate(answer):
p = {p, ''}
bank_a = bank_a | p if i % 2 else bank_a - p
bank_b = bank_b - p if i % 2 else bank_b | p
if bank_a in ng_list:
print(f'you failed : {bank_a}')
return False
if bank_b in ng_list:
print(f'you failed : {bank_b}')
return False
return not bank_a and bank_b == purchases | {''}
assert checker(wolf_goat_cabbage, {'wolf', 'goat', 'cabbage'}, [{'wolf', 'goat'}, {'goat', 'cabbage'}], 7), 'two restriction'
assert checker(wolf_goat_cabbage, {'wolf', 'goat', 'cabbage'}, [], 5), 'no restriction'
assert checker(wolf_goat_cabbage, {'wolf', 'goat', 'cabbage'}, [{'wolf', 'goat'}], 5), 'one restriction'
assert checker(wolf_goat_cabbage, {'wolf', 'goat', 'cabbage'},
[{'wolf', 'goat'}, {'goat', 'cabbage'}, {'wolf', 'cabbage'}], 0), 'three restriction'
assert checker(wolf_goat_cabbage, {'fox', 'goose', 'bean'}, [{'fox', 'goose'}, {'goose', 'bean'}], 7), 'fox goose bean'
Created at: 2017/05/14 12:19; Updated at: 2017/05/14 12:24