Ad
  • Custom User Avatar

    the kata needs clarification but I think if it meant to return "bye" and not "e" then it should also return "hi" if "hi" is the only argument. otherwise it's inconsistent. what if the arguments were ("hello", [1, "world", [2, 3]])? Should it return [1, "world", [2, 3]], [2, 3], or 3 ?

  • Custom User Avatar

    But if the function gets called like last("hi", "bye") this would return "e" and from what I understood this should return "bye" with that input.
    It seems the kata needs more tests or clarify that in the instructions.

  • Custom User Avatar

    In short: You modified functions list.

    Here is the test case in js:

    Test.assertEquals(JSON.stringify(chained(fns)(r)), 
                      JSON.stringify(_solChained(fns)(r)),
                      "Random test "+i)
    

    I suggest modify test case to:

    Test.assertEquals(JSON.stringify(_solChained(fns)(r)),
                      JSON.stringify(chained(fns)(r)), 
                      "Random test "+i)
    

    With that, we can pop as we wish......

  • Custom User Avatar

    Ah-- you should have marked it as "Suggestion" instead of "Issue". I've added it as a test, anyhow ;P

  • Custom User Avatar

    My bad, I didn't understand the tagging system. I was just making a suggestion to give a more comprehensive test case with the puzzle.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I struggled with this one. So, I have like a million test cases from troubleshooting. The thing that tripped me up here, was the language used to refer to the top/front/back/bottom/left/right of the "stack". So the lesson learned is that new items are generally placed on the top/front/right_side of the stack, old items are on bottom/back/left_side of the stack.

    The the good news is my solution ended up being super friendly to being tested. I consider these tests to be part of my solution, so I'll post those here:

    test.it("Builds a code map on init")
    test_code = "123\n" \
                "456\n" \
                "78@"
    test_interpreter_1 = BefungeInterpreter(test_code, start_compiler=False)
    test_code_map = {(0, 0): '1', (1, 0): '2', (2, 0): '3', (0, 1): '4',
                     (1, 1): '5', (2, 1): '6', (0, 2): '7', (1, 2): '8',
                     (2, 2): '@'}
    
    test.assert_equals(test_interpreter_1._code_map, test_code_map)
    
    test_code = "12a\n" \
                "4 *\n" \
                " \@"
    test_interpreter_2 = BefungeInterpreter(test_code, start_compiler=False)
    test_code_map = {(0, 0): '1', (1, 0): '2', (2, 0): 'a', (0, 1): '4',
                     (1, 1): ' ', (2, 1): '*', (0, 2): ' ', (1, 2): '\\',
                     (2, 2): '@'}
    
    test.assert_equals(test_interpreter_2._code_map, test_code_map)
    del test_code_map
    
    test.it("It finds the coordinates of the next step")
    test.assert_equals(test_interpreter_1._next_step(), (1, 0))
    test.assert_equals(test_interpreter_1._next_step(direction="<"), (0, 0))
    test.assert_equals(test_interpreter_1._next_step(direction="<"), (2, 2))
    test.assert_equals(test_interpreter_1._next_step(direction="<"), (1, 2))
    test.assert_equals(test_interpreter_1._next_step(direction="v"), (2, 0))
    test.assert_equals(test_interpreter_1._next_step(direction="v"), (2, 1))
    test.assert_equals(test_interpreter_1._next_step(direction="v"), (2, 2))
    test.assert_equals(test_interpreter_1._next_step(direction="v"), (0, 0))
    test.assert_equals(test_interpreter_1._next_step(direction="^"), (2, 2))
    test.assert_equals(test_interpreter_1._next_step(direction="^"), (2, 1))
    test.assert_equals(test_interpreter_1._next_step(direction="^"), (2, 0))
    test.assert_equals(test_interpreter_1._next_step(direction="^"), (1, 2))
    test.assert_equals(test_interpreter_1._next_step(direction="v"), (2, 0))
    test.assert_equals(test_interpreter_1._next_step(direction=">"), (0, 1))
    test.assert_equals(test_interpreter_1._next_step(direction="^"), (0, 0))
    
    test.it("It executes commands: direction")
    test_code = ">>v\n" \
                ">@?\n" \
                "^<<"
    test_interpreter_3 = BefungeInterpreter(test_code, start_compiler=False)
    test_interpreter_3.next()
    test_interpreter_3.next()
    test.assert_equals(test_interpreter_3._pointer, (2, 0))
    test_interpreter_3.next()
    test.assert_equals(test_interpreter_3._direction, "v")
    test_interpreter_3.next()
    
    del test_interpreter_1
    del test_interpreter_2
    del test_interpreter_3
    
    test.describe("Execute Commands")
    test.it("push")
    test_code = "132@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 3, 2]))
    
    test.it("math")
    test_code = "12-@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([-1]))
    
    test_code = "12+@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([3]))
    
    test_code = "46*@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([24]))
    
    test_code = "74695*@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([7, 4, 6, 45]))
    
    test_code = "10/@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "01/@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "92/@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([4]))
    
    test_code = "93/@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([3]))
    
    test_code = "93%@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "92%@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1]))
    
    test_code = "97%@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([2]))
    
    test_code = "20%@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "07%@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "32`@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1]))
    
    test_code = "59`@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "55`@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "0!@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1]))
    
    test_code = "1!@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "2!@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test.it("direction")
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._execute("<")
    test.assert_equals(test_commands._direction, "<")
    test_commands._execute("<")
    test.assert_equals(test_commands._direction, "<")
    test_commands._execute(">")
    test.assert_equals(test_commands._direction, ">")
    test_commands._execute("^")
    test.assert_equals(test_commands._direction, "^")
    test_commands._execute("v")
    test.assert_equals(test_commands._direction, "v")
    
    test.it("pop move")
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test_commands._execute("_")
    test.assert_equals(test_commands._direction, "<")
    test_commands._queue.append(0)
    test_commands._execute("_")
    test.assert_equals(test_commands._direction, ">")
    test_commands._queue.append(0)
    test_commands._execute("|")
    test.assert_equals(test_commands._direction, "v")
    test_commands._queue.append(1)
    test_commands._execute("|")
    test.assert_equals(test_commands._direction, "^")
    
    test_code = "0 v  \n" \
                "@3_4@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([4]))
    test_code = "1 v  \n" \
                "@3_4@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([3]))
    
    test_code = 'v >3@\n' \
                '>0|  \n' \
                '  >4@'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([4]))
    
    test_code = 'v >3@\n' \
                '>1|  \n' \
                '  >4@'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([3]))
    
    test.it("string mode")
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test.assert_equals(test_commands._string_mode, False)
    test_commands._execute("\"")
    test.assert_equals(test_commands._string_mode, True)
    test_commands._execute("\"")
    test.assert_equals(test_commands._string_mode, False)
    
    test_code = '"Test"@'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([84, 101, 115, 116]))
    
    test.it("duplicates value on top of the stack")
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test_commands._execute(":")
    test.assert_equals(test_commands._queue, deque([1, 1]))
    
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._execute(":")
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test_commands._queue.append(2)
    test_commands._execute(":")
    test.assert_equals(test_commands._queue, deque([1, 2, 2]))
    
    test_code = "1:@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 1]))
    
    test_code = ":@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([0]))
    
    test_code = "12:@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 2, 2]))
    
    test.it("Swaps top 2 values")
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test_commands._queue.append(2)
    test.assert_equals(test_commands._queue, deque([1, 2]))
    test_commands._execute("\\")
    test.assert_equals(test_commands._queue, deque([2, 1]))
    
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test.assert_equals(test_commands._queue, deque([1]))
    test_commands._execute("\\")
    test.assert_equals(test_commands._queue, deque([1, 0]))
    
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(1)
    test_commands._queue.append(2)
    test_commands._queue.append(3)
    test_commands._queue.append(4)
    test_commands._queue.append(5)
    test_commands._execute("\\")
    test.assert_equals(test_commands._queue, deque([1, 2, 3, 5, 4]))
    
    test_code = "12\@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([2, 1]))
    
    test_code = "1\@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 0]))
    
    test_code = "12345\@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 2, 3, 5, 4]))
    
    test.it("pops values")
    test_code = "1$@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._output, [])
    test_code = "12345$$$@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 2]))
    test.assert_equals(test_commands._output, [])
    test_code = "1.@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._output, [1])
    test_code = "12345...@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([1, 2]))
    test.assert_equals(test_commands._output, [5, 4, 3])
    
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(53)
    test_commands._execute(",")
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._output, ['5'])
    
    test_code = "@"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(49)
    test_commands._queue.append(50)
    test_commands._queue.append(51)
    test_commands._queue.append(52)
    test_commands._queue.append(53)
    test_commands._execute(",")
    test_commands._execute(",")
    test_commands._execute(",")
    test.assert_equals(test_commands._queue, deque([49, 50]))
    test.assert_equals(test_commands._output, ['5', '4', '3'])
    
    test_code = "96*5-,@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._output, ['1'])
    
    test_code = "96*5-96*4-96*3-96*2-96*1-,,,@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([49, 50]))
    test.assert_equals(test_commands._output, ['5', '4', '3'])
    
    test.it("trampoline")
    test_code = "#1@"
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([]))
    test_code = ' v\n' \
                ' #\n' \
                ' 1\n' \
                ' 2\n' \
                ' @'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([2]))
    test_code = '#@v\n' \
                ' 3#\n' \
                ' 21\n' \
                ' # \n' \
                ' ^<'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([3]))
    test_code = '#1#@ #2# #5<'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([5]))
    
    test.it("gets and puts")
    test_code = " @"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(49)
    test_commands._queue.append(0)
    test_commands._queue.append(0)
    test.assert_equals(test_commands._code_map.get((0, 0)), ' ')
    test_commands._execute("p")
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._code_map.get((0, 0)), '1')
    
    test_code = " @"
    test_commands = BefungeInterpreter(test_code, start_compiler=False)
    test_commands._queue.append(118)
    test_commands._queue.append(1)
    test_commands._queue.append(2)
    test_commands._execute("p")
    test.assert_equals(test_commands._queue, deque([]))
    test.assert_equals(test_commands._code_map.get((1, 2)), 'v')
    
    test_code = '#@96*5-12pv\n' \
                '           \n' \
                ' 2         \n' \
                ' ^        <'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._code_map.get((1, 2)), '1')
    test.assert_equals(test_commands._queue, deque([1]))
    
    test_code = '#210g@'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([50]))
    
    test_code = '34g@\n' \
                '    \n' \
                '    \n' \
                '    \n' \
                '   z'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(test_commands._queue, deque([122]))
    
    test.it("Converts output to string")
    test_code = '"!olleH",,,,,,@'
    test_commands = BefungeInterpreter(test_code)
    test.assert_equals(str(test_commands), "Hello!")
    del test_commands
    
    test.describe("Sample Programs")
    test_code = '"!olleH",,,,,,@'
    test.assert_equals(interpret(test_code), "Hello!")
    
    test_code = '>987v>.v\n' \
                'v456<  :\n' \
                '>321 ^ _@'
    test.assert_equals(interpret(test_code), '123456789')
    
    test_code = '>              v\n' \
                'v  ,,,,,"Hello"<\n' \
                '>48*,          v\n' \
                'v,,,,,,"World!"<\n' \
                '>25*,@'
    
    test.assert_equals(interpret(test_code), 'Hello World!\n', "Hello World!")
    
    test_code = '>25*"!dlrow ,olleH":v \n' \
                '                 v:,_@\n' \
                '                 >  ^ '
    test.assert_equals(interpret(test_code), 'Hello, world!\n', "Hello, world!")
    
    test_code = '0"!dlroW ,olleH">:#,_@'
    test.assert_equals(interpret(test_code), 'Hello, World!')
    
    test_code = '01->1# +# :# 0# g# ,# :# 5# 8# *# 4# +# -# _@'
    test.assert_equals(interpret(test_code), test_code)
    
    test_code = '08>:1-:v v *_$.@\n' \
                '  ^    _$>\:^'
    test.assert_equals(interpret(test_code), '40320')
    
  • Custom User Avatar

    #My TDD test cases:

    Test.it("Calculates points, given activity and user ranks")
    calculate_points = User()
    Test.assert_equals(calculate_points._calculate_points(-8, -6), 0)
    Test.assert_equals(calculate_points._calculate_points(-8, -7), 1)
    Test.assert_equals(calculate_points._calculate_points(-8, -8), 3)
    Test.assert_equals(calculate_points._calculate_points(-7, -8), 10)
    Test.assert_equals(calculate_points._calculate_points(-6, -8), 40)
    Test.assert_equals(calculate_points._calculate_points(-5, -8), 90)
    Test.assert_equals(calculate_points._calculate_points(-4, -8), 160)
    Test.assert_equals(calculate_points._calculate_points(1, -1), 10)
    Test.assert_equals(calculate_points._calculate_points(8, -8), 2250)
    Test.assert_equals(calculate_points._calculate_points(-3, -1), 0)
    Test.assert_equals(calculate_points._calculate_points(-2, 1), 0)
    Test.assert_equals(calculate_points._calculate_points(-3, 1), 0)
    Test.assert_equals(calculate_points._calculate_points(-4, 1), 0)
    
    Test.it("Has a max rank")
    rank_up = User()
    rank_up.rank = 7
    rank_up.progress = 70
    rank_up._rank_up()
    Test.assert_equals(rank_up.rank, 8)
    Test.assert_equals(rank_up.progress, 0)
    del rank_up
    
    
    rank_up = User()
    rank_up.rank = 8
    rank_up.progress = 70
    rank_up._rank_up()
    Test.assert_equals(rank_up.rank, 8)
    Test.assert_equals(rank_up.progress, 0)
    del rank_up
    
    
    rank_up = User()
    rank_up.rank = -1
    rank_up.progress = 70
    rank_up._rank_up()
    Test.assert_equals(rank_up.rank, 1)
    Test.assert_equals(rank_up.progress, 70)
    del rank_up
    
    
    user = User()
    Test.assert_equals(user.rank, -8)
    Test.assert_equals(user.progress, 0)
    
    user.inc_progress(-7)
    Test.assert_equals(user.progress, 10)
    
    user.inc_progress(-5)  # will add 90 progress
    Test.assert_equals(user.progress, 0)  # progress is now zero
    Test.assert_equals(user.rank, -7)  # rank was upgraded to -7
    
    user.inc_progress(-2)
    Test.assert_equals(user.progress, 50)
    Test.assert_equals(user.rank, -5)
    
    user.inc_progress(4)
    Test.assert_equals(user.progress, 90)
    Test.assert_equals(user.rank, 2)
    
    user.inc_progress(-1)
    Test.assert_equals(user.progress, 90)
    Test.assert_equals(user.rank, 2)
    
    user.inc_progress(7)
    Test.assert_equals(user.progress, 40)
    Test.assert_equals(user.rank, 5)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 30)
    Test.assert_equals(user.rank, 6)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 70)
    Test.assert_equals(user.rank, 6)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 10)
    Test.assert_equals(user.rank, 7)
    
    for _ in xrange(8):
        user.inc_progress(8)
    
    Test.assert_equals(user.progress, 90)
    Test.assert_equals(user.rank, 7)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 0)
    Test.assert_equals(user.rank, 8)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 0)
    Test.assert_equals(user.rank, 8)
    
    user.inc_progress(8)
    Test.assert_equals(user.progress, 0)
    Test.assert_equals(user.rank, 8)
    
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Thumbs up, This was a fun puzzle! I feel like a genious right now for solving this :D Probably not a genious, but this pushed my skill limits as a programmer. I approve.

    Nice work putting this together.

    edit: I posted my notes, test cases, and thought process with my solution. I wrote this mostly for myself to help digest the work. But I imagine it might be useful to others too.

  • Custom User Avatar

    This was a fun kata! I put together this colossal class. When I finally got to the last method that would actually solve the puzzle, that was exciting.

    So, cheers!

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I think you're inflicting some unnecessary sufferring with this line:

    str(a) == '(1,2,3)'

    The default tuple for python has spaces after the ,. So it would be (1, 2, 3).

    It's the difference of using the built in tuple() function and writing it out.

  • Loading more items...