Skip to content

Conversation

cecomfort
Copy link

Recursion Tracing

Thanks for doing some brain yoga. You are now submitting this assignment!

Comprehension Questions

Question Answer
Did you define all the recursion terms? yes
How do you feel about tracing recursive problems? (good, bad, ugly, neutral) good
Would you like help/follow up about recursion? Provided solutions to the added fun would be very helpful! I attempted the added fun for mystery2, but -123/10 and -123%10 did not give the -12 and -3 like anticipated.

@sudocrystal
Copy link

re: answers to the added fun

Here ya go:

def mystery2(n)
  if n < 0
    return -1 * mystery2(-n)
  elsif n < 10
    return n
  else
    return (n%10) + mystery2(n/10)
  end
end

def mystery5(s)
  if s.length == 0
    return ""
  elsif s[0].match(/[a-zA-Z]/)
    return "*" + mystery5(s[1..-1])
  else
    return s[0] + mystery5(s[1..-1])
  end
end

def mystery6(s)
  if s.length < 2
    return s
  else
    return mystery6(s[1..-1]) + s[0]
  end
end

@sudocrystal
Copy link

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants