Python program question Here are all the codes I need to use for this assignment below: n1 = int(input("Enter your first

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Python program question Here are all the codes I need to use for this assignment below: n1 = int(input("Enter your first

Post by answerhappygod »

Python program question
Here are all the codes I need to use for this assignmentbelow:
n1 = int(input("Enter your first positive integer: "))
n2 = int(input("Enter your second positive integer: "))
s = n1 + n2
lst1 = decimalToBinary(n1)
print("The binary representation of", n1, "is", lst1)
lst2 = decimalToBinary(n2)
print("The binary representation of", n2, "is", lst2)
lst3 = binaryAddition(lst1, lst2)
print("The binary addition of", n1, "and", n2, "is", lst3)
n3 = binaryToDecimal(lst3)
print("Converting the binary to decimal gives", n3)
if s == n3:
print("Since", s, "==", n3, ", it seems we did good job.")
else: print("Since", s, "!=", n3, ", something went wrong.")
print("----------------------")
n = int(input("Enter a positive integer: "))
lst = decimalToBinary(n)
print("The binary representation of", n, "is", lst)
result = algo(lst)
print(lst, "became palindrome: ", result[0], "after ",result[1], "iterations")
So far, I have done def decimalTobinary(n1) anddecimalTobinary(n2) by myself.
I attached the picture of my code ao farbelow
Python Program Question Here Are All The Codes I Need To Use For This Assignment Below N1 Int Input Enter Your First 1
Python Program Question Here Are All The Codes I Need To Use For This Assignment Below N1 Int Input Enter Your First 1 (50.58 KiB) Viewed 14 times
Using my code, could anyone give me code for defbinaryAddtion(lst1, lst2) and binarytodecimal(lst), and defalgo(lst), please??
The picture below I attached is structure for moreinformation!
It would be great if you provide screenshot of runningthe code and output as well!
Also, I have to use list function, so you can start withthe function:
def binaryAddition(lst1, lst2):
lst3 = []
Python Program Question Here Are All The Codes I Need To Use For This Assignment Below N1 Int Input Enter Your First 2
Python Program Question Here Are All The Codes I Need To Use For This Assignment Below N1 Int Input Enter Your First 2 (106.9 KiB) Viewed 14 times
n1= int(input("Enter n2= int(input("Enter def reverse(alist): rev = [] your first positive integer:"); your second positive integer:")) for i in range(len(alist)): rev.append(alist [len(alist)-1-i]) return rev def decimal ToBinary(n1): Ist1 = [] while n1 > 0: digit n1 % 2 Ist1.append(digit) n1 = n1 // 2 return reverse(Ist1) def decimal ToBinary(n2): = while n2 > 0: digit = n2 % 2 Ist2.append(digit) n2 = n2 // 2 return reverse(Ist2) Ist1 decimal ToBinary (1) print("The binary representation of", nl, "is", Ist1) Ist2 decimal ToBinary(n2) print("The binary representation of", n2, "is", Ist2)
o def binaryAddition (1st1, 1st2): • This function takes two list arguments 1st1 and 1st2 representing two binary numbers and returns a list of integers whose elements are obtained by the addition of the two binary numbers in binary addition format. How to add two binary numbers? Binary addition is like decimal addition, except that it carries on a value of 2 instead of a value of 10. 0 + 0 = 0 1 + 0 = 1 0 + 1 = 1 1 + 1 = 10 (this is 0 carry 1) 1 + 1 + 1 = 11 (this is 1 carry 1) To add two binary numbers, we simply add together the corresponding digits in each binary number from right to left and if the sum is greater than what fits in a single digit, we carry a 1 into the next column. For example, + 1111 1 110101 11101 1010010 carries We start by the first column from the right: 1) First column: 1 + 1 = 0 (with carry 1) 2) Second column: 0 + 0 + 1 (carried) = 1 (no carry) 3) Third column: 1 + 1 + no carry = 0 (carry 1) 4) Fourth column: 0 + 1 + 1 (carried) = 0 (carry 1) 5) Fifth column: 1 + 1 + 1 (carried) = 1 (carry 1) 6) Sixth column: 1 + 1 (carried) = 0 (carry 1) 7) Seventh column: 1 (carried) binaryAddition ([1,1,0,1,0,1],[1,1,1,0,1]) → [1,0,1,0,0,1,0] binaryAddition ([1,1,0,1,0,1],[1,1,1,0,1]) [1,0,1,0,0,1,0]
o def binaryToDecimal(1st): • This function takes a list 1st representing a binary number and converts it to decimal and returns the decimal number. For example, binaryToDecimal([1,0]) → 2 binaryToDecimal([1,0,1,0,0]) → 20 binaryToDecimal([1,0,1,1,1,0,1,1,0,0,1,0])→ 2994 binaryToDecimal([1,1,1,1,1,0,1,1,1,0,1,1,0,0,0,0]) → 64432 o def algo (1st): • Below is an algorithm that produces palindromic numbers for most binary numbers: Let n be a binary number: [STEP 1] If the number is palindromic STOP. If not, go to step 2 [STEP 2] Reverse the digits [STEP 3] Add the reverse to the original number (binary addition), the sum is your new number, go to step 1 For example, 1011 11011 1. 1011 is NOT palindromic 2. 1011 reversed is 1101 1101 - 11000 3. 1011 4. 11000 is NOT palindromic 5. 11000 reversed is 00011 6. 11000 + 00011 - 11011 is palindromic after 2 iterations This function takes a list 1st representing a binary number and returns a nested lists with two elements: First element: the resulting palindromic binary number, Second element: number of iterations required to get to a palindromic number by the procedure mentioned above. Throughout this procedure, if after 10 iterations the number is still NOT palindromic your function must return [None, -1]. For example, algo([1,0]) [[1,1], 1] algo([1,0,1]) → [[1, 0, 1], 0] algo([1,0,1,1]) → [[1, 1, 0, 1, 1], 2] algo ([1,0,1,0,0]) → [[1, 1, 0, 0, 0, 1, 1], 5] algo([1,0,1,1,0]) → [None, -1]
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply