šŸŽBACK-TO-SCHOOL DEAL. Subscribe Now to get 40% OFF at only 8.49 USD/month, only valid until Sep 30th, 2024

Question

Question
Problem 2 - Recursive Binary Create a recursive function in a file called binary.py: This recursive function should return the binaly representation of a number. Here is how to think about this problem. Writing a function that calculates the binary representation should always ask whether or not the number in question is even or odd. For instance, if you were to convert 5 into binary, 5 is odd so you know that the first digit should be a 1 . After this, then you should think about it this way, if you (integer) divide the number by 2 , then you'll get , and the binary for 2 is 10. If you put 10 together with 1 at the end, then you'll reach 101 which is the binary for 5 . How can you make this into a recursive process? When you think about the base case, you can use it to add the Ob to the binary number to match what the built in bin produces. When you use this driver code to call your function you should see that the binary expression matches. int (input ('Tell me number: ')) while : print ('Ob' + binary (x), bin(x)) int (input ('Tell me number: ')) linux5[109]\& python binary.py Tell me number: 5 Ob101 0b101 Tell me number: 21 Ob10101 0b10101 Tell me number: 57 Ob111001 Ob111001 Tell me number: 214 Ob11010110 Ob11010110 Tell me number: 12345 0b11000000111001 0b11000000111001 Tell me number: 3 Ob11 0b11 Tell me number: 2 Ob10 Ob10 Tell me number: 17 0b10001 0b10001 Tell me number: -1

Asked By SereneSunset64 at

Answered By Expert

Ted

Expert Ā· 4.8k answers Ā· 4k people helped

Step 1/2

To create a recursive function in a file called binary.py that returns the binary representation of a number, you can follow the logic you provided. Here's a Python implementation of the binary function:

Explanation:

This code defines the binary function using recursion. It handles the base case when the number is 0 and recursively calculates the binary representation for both even and odd numbers by dividing the number by 2 and adding '0' or '1', respectively. The driver code takes user input, calculates and prints both the recursive binary result and the built-in binary representation for the input number until -1 is entered.

Step 2/2

Output for the code above:

Final Answer

The recursive function converts an integer to its binary representation by dividing it by 2 and appending the remainder as binary digits. The base case handles 0. The function uses recursion to build the binary representation, matching the result obtained from bin(x) .

šŸ§‘ā€šŸ« More Questions

A calica cat is a multicolored domestic cat whose fur that is orange, white, black, cream, and gray. My adorable callco Kitten Cate the Great, is shown below. It turns out that nearly all calico cats are female. One source daims that exactly 99.9% of all call cats are female. We are interested in testing this claim. Let p be the true proportion of all calico cats that are female. A sample of 104 randomly selected callco cats found that 100 were female. Round all answers in this problem to three decimal places. (a) (4 points) What are the appropriate null and alternative hypotheses? (a) (4 points) What are the appropriate null and alternative hypotheses? hypothesis OHD O HD999 O H D >0.900 Alternative hypothesis: OHP - 0.999 O HD99.9 OH 0.999 OMD99.9 OHID0.999 (b) (2 points) What kind of hypothesis test is described? TWO-tailed test Left-tailed test Right-tailed test One-tailed test (c) (2 points) What is the sample proportion of female calice cats in the study (d) (4 points) What is the appropriate test statistic for the hypothesis testin (b)? 0.05, what is the appropriate conclusion? (a) (2 points) at Foto Palto reject Red Refect (4 points) using the sample data, computer an upper bound for a lower 99% confidence interval for p. This upper bound is (a) (2 points) Based on the confidence interval in (). Is 99.9% a plausible value for the true percentage of calice cats that are female? 99 is plausible O s implausible (Bonus) What one word best describe my click the pictured The best n e wer from each section will receive one bonuspoint.