Question

In pyhton 3.7 please 2. Write a function that takes, as an argument, either a 12-digit...

In pyhton 3.7 please

2. Write a function that takes, as an argument, either a 12-digit positive integer n or a string consisting of 12 digits, and calculates the 13th digit (the check digit) for a 13-digit ISBN. Name this function calculateCheckDigit(n).

For example, >>>calculateCheckDigit(452678904653) should return 5.

As an additional example, >>>calculateCheckDigit(546654945486) should return 8.

3. Write a function that takes, as an argument, an eight-bit binary string and does the following, in this order: • Verifies that it consists of only the digits 0 and 1 (if not, return “not a binary string”)

• Verifies that it has length 8 (if not, return “wrong length”)

• It returns the eight-bit binary string that results from flipping bits: 0 becomes 1 and 1 becomes 0 Name this function flipBits(myString).

For example, >>>flipBits(“123”) should return the string “not a binary string”.

As another example, >>>flipBits(“1100”) should return the string “wrong length”.

As a final example, >>>flipBits(“11001010”) should return the string “00110101”

4. Write a function that takes, as an argument, two four-bit binary strings, string1 and string2, and does the following, in this order: • Verifies that both strings consist of only the digits 0 and 1 (if not, return “not binary strings”)

• Verifies that both strings have length 4 (if not, return “wrong length”)

• It returns the result of string1 XOR string2 Name this function xor(string1, string2).

For example, >>>xor(“1”, “abc”) should return the string “not binary strings”.

As another example, >>>xor(“1100”, “1”) should return the string “wrong length”.

As a final example, >>>xor(“1100”, “1010”) should return the string “0110”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

3)

CODE

def flipBits(myString):

if len(myString) != 8:

return "Wrong length"

for bit in myString:

if bit != '0' and bit != '1':

return "not a binary string"

flipped = ""

for bit in myString:

if bit == "0":

flipped += "1"

else:

flipped += "0"

return flipped

print(flipBits("123"))

print(flipBits("1100"))

print(flipBits("11001010"))

Wrong length Wrong length 00110101 main.py auto-format 1 def flipBits(myString): 2 if len(myString) != 8: return Wrong lengt

NOTE: As per HOMEWORKLIB POLICY I am allowed to answer specific number of questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
In pyhton 3.7 please 2. Write a function that takes, as an argument, either a 12-digit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT