Problem 1: From binary to decimal and back!
20 points; pair-optional
This is one of only two problems from this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
This problem asks you to write functions related to the binary represention of integers. We will use strings consisting of ‘0’s and ‘1’s for the binary numbers. For example, the binary representation of the integer 5 would be the string ‘101’.
In Spyder, use the File->New File menu option to open a new editor window for your code, and save it using the name ps4pr1.py.
The guidelines that we gave you for Problem Set 2, Problem 3 also apply here. In addition, you must use recursion in your solutions, and you may not use loops or list comprehensions.
Write a function dec_to_bin(n) that takes a non-negative integer n and
uses recursion
to convert it from decimal to binary – constructing and
returning
a string version of the binary representation of that number. For example:
>>> dec_to_bin(5)
result: ‘101’
>>> dec_to_bin(12)
result: ‘1100’
>>> dec_to_bin(0)
result: ‘0’
Notes/hints:
The function must use the recursive, right-to-left approach that we discussed in the lecture on binary numbers.
You will need two base cases.
Make sure that all return statements return a string and not an integer.
In lecture, we gave you an example of how the function should recursively process a number. You should use that example and other concrete cases to determine the appropriate logic for the recursive case.
In addition to the test cases provided above, make sure to try other test cases to ensure that your function works correctly in all cases!
Write a function bin_to_dec(b) that takes a string b that represents a binary number and
uses recursion
to convert the number from binary to decimal,
returning
the resulting integer. For example:
>>> bin_to_dec(‘101’)
result: 5
>>> bin_to_dec(‘1100’)
result: 12
>>> bin_to_dec(‘0’)
result: 0
Notes/hints:
The function must use the recursive, right-to-left approach that we discussed in the lecture on binary numbers.
You will again need two base cases. You may assume that the string passed in for b will never be empty.
Make sure that all return statements return an integer and not a string.
In lecture, we gave you an example of how the function should recursively process a string. You should use that example and other concrete cases to determine the appropriate logic for your recursive case.
In addition to the test cases provided above, make sure to try other test cases to ensure that your function works correctly in all cases!
Problem 2: Using your conversion functions
20 points; pair-optional
This is one of only two problems from this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
In Spyder, use the File->New File menu option to open a new editor window for your code, and save it using the name ps4pr2.py.
The guidelines that we gave you for Problem Set 2, Problem 3 also apply here. You should not use recursion in these functions.
Important: Your should include the following line at the top of the file, after your initial comments:
from ps4pr1 import *
Doing so will allow you to use the dec_to_bin() and bin_to_dec() functions that you wrote for Problem 1, provided that your ps4pr1.py file is in the same folder as your ps4pr2.py file.
Write a function add(b1, b2) that takes as inputs two strings b1 and b2 that represent binary numbers. The function should compute the sum of the numbers, and return that sum in the form of a string that represents a binary number. For example:
>>> add(’11’, ‘1’)
result: ‘100’
>>> add(‘11100’, ‘11110’)
result: ‘111010’
In this function, you should not need to use recursion or to perform any binary arithmetic. Rather, you must make use of the conversion functions that you wrote for Problem 1. If you included the line mentioned above to import your code from pr4pr1.py, you should be able to call your dec_to_bin and bin_to_dec functions from within this function. Convert both b1 and b2 to decimal, add the resulting decimal numbers, and then convert the resulting sum back to binary! Here is the pseudocode:
n1 = the decimal value of the input string b1 (use one of your conversion functions!)
n2 = the decimal value of the input string b2 (use that function again!)
b_sum = the binary value of (n1 + n2) (use your other function!)
return b_sum
Write a function increment(b) that takes an 8-character string representation of a binary number and returns the next larger binary number as an 8-character string. For example:
>>> increment(‘00000000’)
result: ‘00000001’
>>> increment(‘00000111’)
result: ‘00001000’
>>> increment(‘11111111’)
result: ‘00000000’
Notes/hints:
As shown in the last example above, you will need a special case for when the input is ‘11111111’. This is the largest binary number that can be represented using 8 bits, so incrementing it causes the value to “wrap around” to give you ‘00000000’. Your function should start by testing for this special input, and it should handle it by simply returning the appropriate value.
Here again, you should not use recursion or perform any binary arithmetic. Rather, you must make use of the conversion functions that you wrote for Problem 1.
To ensure that your result has the correct length, we encourage you to do the following:
Use your conversion functions to determine the binary representation of the result, and store that result in a variable.
Determine the length of the result and store that length in a variable.
If the length of the result is less than 8 bits, precede the result with the correct number of leading ‘0’s. You can use the multiplication operator to produce the necessary string of ‘0’s.
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.