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

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

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

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the...

    Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the sum of the integer numbers from 1 to n Please write in C++

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