Question

In: Computer Science

Write a function that takes three integers, n, a and b and a filename and writes...

Write a function that takes three integers, n, a and b and a filename and writes to the file a list with n random integers between a and b. And then write a function that can read the files as generated above and return the values.

language: python

Solutions

Expert Solution

Dear Student ,

As per requirement submitted above kindly find below solution.

Python program :

import random #import random
#Python function to write numbers to the file
def writeNumbers(n,a,b,filename):
    f=open(filename,"w") #open file for writing
    randomNumberList=[] #list to store numbers
    #using for loop
    for i in range(0,n):
        number=random.randint(a,b) #generate random number
        randomNumberList.append(number) #store number in list
    f.write(str(randomNumberList))#write list to the file
#function to read the file and return list
def readNumbers(filename):
    f=open(filename,"r")#open file for read purpose
    return f.read() #read file contents and return
#call function with value of a , b and n and file name
#function to write numbers to the file
writeNumbers(5,1,10,"numbers.txt")
#call function to read numbers and print numbers
print(readNumbers("numbers.txt"))

*****************************************
Please refer to the screenshot of the code to understand the indentation of the code :

==================================

Output :

Screen showing file numbers.txt :

Screen showing output :

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
(Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes True...
(Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes True if each one of them is less than or equal to the sum of the other two and False otherwise. This computation tests whether the three numbers could be the lengths of the sides of some triangle. $ python3 triangle.py 3 4 5 True $ python3 triangle.py 2 4 7 False
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0 and 255 (inclusive), representing the red, green, and blue intensity of a pixel and returns the luminance of this pixel as an integer. The function expects each parameter r, g, and b to be an integer in the interval [0,255]. You should use assertions to enforce this constraint.
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT