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
  • (PYTHON please) Write a function that takes, as an argument, a positive integer n, and creates...

    (PYTHON please) Write a function that takes, as an argument, a positive integer n, and creates a file, named fourBits.txt, containing n randomly generated four-bit binary strings, each on its own line. Examples of four-bit binary strings include “1111” and “0011”. Name this function fourBitStrings(n).

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

  • Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

    Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8

  • PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and...

    PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and an integer n between -750 and 750 (inclusive). Your program should verify that n is an integer in the correct range. If it is not, it should return the string “Your integer is out of range.” If it is in the correct range, your program should open (and read through) the file specified, and return the number of values in the file that are...

  • Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of...

    Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of characters and the corresponding Huffman code for those characters. It decodes the binary string using the Huffman code, and returns the resulting decoded string. Name this function decodeStringHuffman(binaryString, myCharacters, myCode). For example, >>>decodeStringHuffman("1001100010111010101010111", ["A", "B", "C"], ["1", "00", "01"]) should return the string 'ABAABCCAACCCCCAA'

  • Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...

    Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • Using C, Write a function reverse which takes a string as an argument, reverses the string...

    Using C, Write a function reverse which takes a string as an argument, reverses the string and returns the reversed string. Note; you should not return a string that you created inside the reverse function! For example: Test char str[]-"hello" printf("%s", reverse (str)); Result olleh

  • Write a function check palindrome, which takes a string x as argument and which returns True...

    Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....

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