Skip to content

Conversation

@J-C-L
Copy link

@J-C-L J-C-L commented Mar 28, 2017

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) It's fun! I don't always see the tough ones quickly, but I understand the process.
Would you like help/follow up about recursion? I'd like to see answers to the 'added fun' questions and the writing recursion exercises.

@sudocrystal
Copy link

Answers to added fun:

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

a = (m%10) + mystery2(m/10)
return -a
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it works. I also just posted an alternative solution that is perhaps more readable.

end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it works. I did post another solution to consider.

One thing is to figure out if you think I intended mystery5("Hi, there!") to produce "*******" or "**, *****!"

I intended the latter, but didn't specify so you+others assumed and wrote solutions for the former.

else
return mystery6B(s[1..-1]) + s[0]
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 my solution too. Though I wrote the base case differently, they are essentially the same. I didn't account for nil though.

@sudocrystal
Copy link

Nice job!

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