Skip to content

Instantly share code, notes, and snippets.

@marklit
Created April 22, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marklit/07b1692bf1cafcd7a1af to your computer and use it in GitHub Desktop.
Save marklit/07b1692bf1cafcd7a1af to your computer and use it in GitHub Desktop.
from random import randint
LIVING_SPACE_CELL_SIZE = 24
class Alive(object):
pass
class Dead(object):
pass
def get_next_state(position, current_generation):
"""
:param list combo: 3 objects that are either Dead or Alive
:return: if a middle cell should live or not based on neighbours
:rtype: object (either Dead or Alive)
Outcomes:
111 Dead
110 Dead
101 Dead
100 Alive
011 Alive
011 Dead
010 Dead
001 Dead
000 Dead
"""
# Edge case 1 (left side)
if position < 1:
combo = [
type(current_generation[len(current_generation) - 1]),
type(current_generation[position]),
type(current_generation[position + 1])
]
# Edge case 2 (right side)
elif position == len(current_generation) - 1:
combo = [
type(current_generation[position - 1]),
type(current_generation[position]),
type(current_generation[0])
]
else:
combo = [
type(current_generation[position - 1]),
type(current_generation[position]),
type(current_generation[position + 1])
]
print 'combo', combo
should_live = False
if combo == [type(Alive), type(Dead), type(Dead)]:
should_live = True
if combo == [type(Dead), type(Alive), type(Alive)]:
should_live = True
return Alive() if should_live else Dead()
if __name__ == "__main__":
current_generation = [Dead() if randint(0, 1) else Alive()
for _ in range(0, LIVING_SPACE_CELL_SIZE)]
next_generation = [get_next_state(position, current_generation)
for position, value in enumerate(current_generation)]
$ nose2 test
combo [<class 'main.Alive'>, <class 'main.Alive'>, <class 'main.Alive'>]
combo [<class 'main.Alive'>, <class 'main.Alive'>, <class 'main.Dead'>]
combo [<class 'main.Alive'>, <class 'main.Dead'>, <class 'main.Alive'>]
combo [<class 'main.Alive'>, <class 'main.Dead'>, <class 'main.Dead'>]
F
======================================================================
FAIL: test_get_next_state (test.TestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mark/testing/test.py", line 28, in test_get_next_state
self.assertEqual(type(next_state), type(expected_state), '%s should be %s instead of %s' % (combo, type(expected_state), type(next_state)))
AssertionError: [<main.Alive object at 0x7f5a9df00450>, <main.Dead object at 0x7f5a9df002d0>, <main.Dead object at 0x7f5a9df00290>] should be <class 'main.Alive'> instead of <class 'main.Dead'>
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
import unittest
from main import Alive, Dead, get_next_state
class TestCases(unittest.TestCase):
def test_get_next_state(self):
outcomes = """
111 Dead
110 Dead
101 Dead
100 Alive
011 Alive
011 Dead
010 Dead
001 Dead
000 Dead
""".strip()
for combo, state in [outcome.strip().split(' ')
for outcome in outcomes.split('\n')]:
expected_state = Alive() if state == 'Alive' else Dead()
combo = [Dead() if val == '0' else Alive()
for val in combo]
next_state = get_next_state(1, combo)
self.assertEqual(type(next_state),
type(expected_state),
'%s should be %s instead of %s' % (combo,
type(expected_state),
type(next_state)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment