Question

(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).

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


def fourBitStrings(n):
    f = open('fourBits.txt', 'w')
    for i in range(n):
        number = random.randint(0, 15)
        f.write('0' * (4 - len(bin(number)[2:])) + bin(number)[2:] + "\n")
    f.close()


# Testing the function here. ignore/remove the code below if not required
fourBitStrings(5)  # This write data to file. check file for output.

generated four Bits.txt file..

1110 0111 1111 1000 0001

Add a comment
Know the answer?
Add Answer to:
(PYTHON please) Write a function that takes, as an argument, a positive integer n, and creates...
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
  • 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].

  • 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...

  • 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...

  • Write a Python function binom_product that takes integer arguments a and b and positive integer argument...

    Write a Python function binom_product that takes integer arguments a and b and positive integer argument n and returns the product of the coefficients in the expansion of (ax + by)”. Example: Let a = 2, b = -1, and n = 3. Then (2x – y)3 = 8x3 – 12x²y + 6xy2 – 43 The product of the expansion coefficients is 8 x -12 x 6 x -1 = 576 Notes: There are two visible test cases and three...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • In python programming. Write a recursive function that accepts an integer argument, n. The function should...

    In python programming. Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks.

  • How to Define a function named calculatePI that takes a positive integer n as its argument...

    How to Define a function named calculatePI that takes a positive integer n as its argument and calculates π using the formula: (Assume n is odd, which is the precondition of the function. And, this function should return double type value.) π = 4*(1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + ... 1/n)

  • In python 3 please Write a function file_motif(filename, dim, char1, char2) that creates a new file,...

    In python 3 please Write a function file_motif(filename, dim, char1, char2) that creates a new file, named filename, containing the pattern that is shown in the example below. You are not allowed to use the with statement. Parameters: filename is a string, dim is an integer that defines the number of rows, char1 and char2 are single-character strings. xamples: file_motif("output.txt", 4, "s", "q")         -> qsssssss                                                            qqssssss                                                            qqqqssss                                                            qqqqqqqq file_motif("pattern.dat", 5, "o", "+")        ->   +ooooooooooooooo                                                            ++oooooooooooooo                                                            ++++oooooooooooo                                                            ++++++++oooooooo                                                            ++++++++++++++++

  • python In a program, write a function named roll that accepts an integer argument number_of_throws. The...

    python In a program, write a function named roll that accepts an integer argument number_of_throws. The function should generate and return a sorted list of number_of_throws random numbers between 1 and 6. The program should prompt the user to enter a positive integer that is sent to the function, and then print the returned list.

  • python: Write a function named "write_values" that takes a key-value store mapping strings to strings as...

    python: Write a function named "write_values" that takes a key-value store mapping strings to strings as a parameter and writes the values of the input to a file named "persuade.txt" with one element per line. If a file named "persuade.txt" already exists it must be overwritten. The function should not return any value

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