Question
(I need help programming this code. If you can do it in Scala, that would be great. If not, Python works too. Thank you.) INSTRUCTIONS:
(I need help programming this code. If you can do it in Scala, that would be great. If not, Python works too. Thank you.)
INSTRUCTIONS:
Now, write a tail recursive version of the convertToBinary function, called convertToBinaryTail.
Your function should be tail recursive and takes in a single integer argument and returns a String.
Hint You can have more arguments to your tail recursive function but make sure to give them a default value. Alternatively, you can make an explicit helper function.
Restrictions
Use of var and loops forbidden. Inbuilt functions that directly perform conversion are also forbidden. Must be tail recursive.
###CODE
import scala.annotation.tailrec @tailrec // YOUR CODE HERE ???
//BEGIN TEST assert(convertToBinaryTail(1) == "1", "(1)decimal must be 1 binary") assert(convertToBinaryTail(5) == "101", "5 must be 101 binary") assert(convertToBinaryTail(11) == "1011", "(11) must be 1011") assert(convertToBinaryTail(4100) == "1000000000100", "(4100) must be 1000000000100") assert(convertToBinaryTail(30108100) == "1110010110110100111000100", "(30108100) must be 1110010110110100111000100") passed(10) //END TEST
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started