Question

In: Computer Science

Python DESCRIPTION Write a program that will read an array of integers from a file and...

Python

DESCRIPTION

Write a program that will read an array of integers from a file and do the following:

Task 1: Revert the array in N/2 complexity time

(i.e., number of steps)

.

Task 2: Find the maximum and minimum element of the array.

INPUT OUTPUT

Read the array of integers from a file named “

inputHW1.txt

”. To do this, you can use code snippet

from the “

file.py

” file. This file is provided in

Canvas. Copy the “

read

” function from this file and

paste it to your program file. Then, if you call the read function like the following:

numbers = read()

# The following "read" function reads values from a file named "inputHW1.txt" and

returns the values in an array.

def read():

file = open("inputHW1.txt", "r")

line = file.readline()

values = []

for value in line.split(' '):

values.append(int(value))

return values

the integers will be read from file and loaded in the numbers array.

Now, print the reverted arra

y, minimum element and maximum element of the array.

For example, if the input array is: 1 4 9 11 8 3 2 5 10

the following will be printed on the screen:

Reverted array: 10 5 2 3 8 11 9 4 1

Maximum element: 11

Minimum element: 1

Solutions

Expert Solution

def read():
   file = open("inputHW1.txt", "r")
   line = file.readline()
   values = []
   for value in line.split(' '):
       values.append(int(value))
   return values

numbers = read()
#task 1, reverting the array
n = len(numbers)
mid = int(n/2)
#swap ith number with (n-1-i)th number
for i in range(mid):
   numbers[i], numbers[n-1-i] = numbers[n-1-i], numbers[i]

#task 2, finding maximum and minimum
maximum = numbers[0]
minimum = numbers[0]
for i in range(n):
   maximum = max(maximum, numbers[i])
   minimum = min(minimum, numbers[i])

print("Reverted array:", end=" ")
for i in range(n):
   if(i<n-1):
       print(numbers[i], end=" ")
   else:
       print(numbers[i])

print("Maximum element:", maximum)
print("Minimum element:", minimum)


Related Solutions

Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT