1

I am trying to up my literate coding chops and solving some programming challenges with it.

Here is the full writeup of the exercise I'm playing with

Mostly everything works and I understand everything...except that bit all the way at the bottom.

I have


    #+BEGIN_SRC python :session dp-string-gcd :var examples=dp-gcd-examples :results output :exports both 
      def get_string_gcd(shorter, longer):
          if not shorter:
              return None
          if len(shorter) > len(longer):
              return get_string_gcd(longer, shorter)
          current_cycle = []
          for ((s, current_cycle), l) in zip(tracked_cycle(shorter), longer):
              if s != l:
                  return None
          return ''.join(current_cycle)

      for (longer, shorter, desired) in examples:
          desired = desired or None
          gcd = get_string_gcd(shorter, longer)
          print(f'gcd({shorter}, {longer}) = {gcd}')
          if gcd != desired:
              print(f'FAILBOY! it should be {desired}\n')
    #+END_SRC

    #+RESULTS:
    : gcd(ABC, ABCABC) = ABC
    : gcd(ABAB, ABABAB) = AB
    : gcd(ABAB, ABABABB) = None
    : gcd(CODE, LEET) = None

but that's not really what I want - I would like the function definition to be in a separate src block

The issue is when I do this

    #+BEGIN_SRC python :session dp-string-gcd :results none
      def get_string_gcd(shorter, longer):
          if not shorter:
              return None
          if len(shorter) > len(longer):
              return get_string_gcd(longer, shorter)
          current_cycle = []
          for ((s, current_cycle), l) in zip(tracked_cycle(shorter), longer):
              if s != l:
                  return None
          return ''.join(current_cycle)
    #+END_SRC

    #+BEGIN_SRC python :session dp-string-gcd :var examples=dp-gcd-examples :results output :exports both 
      for (longer, shorter, desired) in examples:
          desired = desired or None
          gcd = get_string_gcd(shorter, longer)
          print(f'gcd({shorter}, {longer}) = {gcd}')
          if gcd != desired:
              print(f'FAILBOY! it should be {desired}\n')
    #+END_SRC

    #+RESULTS:
    : gcd(ABC, ABCABC) = None
    : FAILBOY! it should be ABC
    : 
    : gcd(ABAB, ABABAB) = None
    : FAILBOY! it should be AB
    : 
    : gcd(ABAB, ABABABB) = None
    : gcd(CODE, LEET) = None

I for some reason start getting the function returning None! in fact it seems like that happens any time that the first block doesn't have :results output. I'm not outputting anything there, I just want the stuff to run in the session.

What is going on that simply changing the results type on the src block can make a python function return something different?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.