Hacker News new | ask | show | jobs
by bobbyi_settv 4342 days ago
The "complete" solutions for Python in the repo don't actually work because they don't reset the state. If you add a final line of:

  print g('al')
for solution1 or:

  print m('rton')
for solution2, you'll find they don't print gal/ mrton.

Here's what I came up with before looking at those solutions that seems to work:

  def g(arg=None):
      if arg == 'al':
          return 'gal'
      def inner(arg=None):
          if arg == 'al':
              return 'g' + (inner.counter * 'o') + 'al'
          inner.counter += 1
          return inner
      inner.counter = 1
      return inner
1 comments

Resetting the state isn't one of the rules ;)

I'd make a solution which resets the state linked by the table though, so please submit one!

Rule 7 says:

   g('al') must return "gal".
If you add

   assert g('al') == 'gal'
as the last line of solution 1, the assertion will not pass. I'm not saying there is a rule that mentions "resetting state". I'm saying that their failure to reset state is the bug leading to them not following the rules.
Thanks for pointing this out, it's now fixed.

https://github.com/eatnumber1/goal/pull/18

Nick,

That helps but doesn't completely solve the state problem. Try adding these two lines to the end of the program:

    g()()()()()
    print g('al')

I think 'gal' should be printed here. Rule 7 says that's what your function should return given 'al', and the fact that we've called it in some other way before doesn't change the fact that it is required to return 'gal' given 'al'.

The solution I posted above handles this scenario (and, as a bonus, is thus threadsafe in case multiple people were doing g()()()('al') simultaneously).