5/5 – (10 votes)
ParityParty
Write a Python function ParityParty() that takes in a nonnegative integer dd and returns a list:
- the first element of the output list is a 0 if the number dd is even, and 1 if dd is odd,
- the second element of the output list is d/2d/2 if dd is even, and (d1)/2(d1)/2 if dd is odd.
A potentially useful reminder: When you divide or multiply an integer by a floating point number in Python, the result will be a floating point number. If the test cases require an integer as output for d/2d/2 or (d1)/2(d1)/2, you may need to account for this
For example:
Test | Result |
---|---|
print(ParityParty(0)) |
[0, 0] |
print(ParityParty(10)) |
[0, 5] |
print(ParityParty(33)) |
[1, 16] |
BinToDec
Suppose you were working on some Python code to convert numbers from binary to decimal form. Suppose your mischievous cat walked across your keyboard while you were getting a cup of coffee, and as a result several important pieces of code have gone missing. For each missing piece of code, select the answer that will enable the function BinToDecBinToDec to convert positive integers from binary to decimal form, without modifying any of the other pieces of code.
DecToBin
Write a Python function DecToBin() that takes in a nonnegative integer dd and returns a Python list of 00s and 11s corresponding to the binary representation of dd.
For example:
Test | Result |
---|---|
print(DecToBin(0)) |
[0] |
print(DecToBin(10)) |
[1, 0, 1, 0] |
print(DecToBin(241)) |
[1, 1, 1, 1, 0, 0, 0, 1] |
Note: binary_inbinary_in is a Python list, representing the number in binary form.
Example: The number 1010 in binary would be input as [1, 0, 1, 0].
def BinToDec(binary_in):def BinToDec(binary_in):
# initialize# initializedecimal_out = 0decimal_out = 0
# add up the binary expression of the decimal number# add up the binary expression of the decimal numberfor position in for position in Chooserange(0, len(binary_in))range(1, len(binary_in))range(0, len(binary_in)-1)range(1, len(binary_in)+1) :
decimal_out = decimal_out + binary_in[len(binary_in)-position-1]*(2**decimal_out = decimal_out + binary_in[len(binary_in)-position-1]*(2**Choosepositionposition-1position+1 ))
Reviews
There are no reviews yet.