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

EXTRA CREDIT - 30%

Find the trend change points of the array. You will get

30% extra credit

for doing this task.

Trend

change points are those points where the array

changes its direction

and

goes from

increasing to decreasing order or decreasing to increasing order.

Note that, you have to find the trend change points of the input array, not the reverted 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:

Trend change points: 11 8 5

Solutions

Expert Solution

Note:-Here trend points are [11,2] but you are taking trend points [11,8,5] from 1->4->9->11 it's increasing but after 11 it's decreasing 11->8 and 11->8->3->2 it's decreasing but after 2 it's increasing 2->5->10.....so here 11 and 2 are change points so I Write the code like this

Code:-

def read(): #function to take input array from file
file=open("inputHW1.txt","r") #open input file in read mode
line=file.readline() #read line from file
values=[] #declaring list
for value in line.split(' '): #splitting the elements separated by space
values.append(int(value)) #append values to list
return values #returning list

numbers=read() #calling function
revertedarray=[] #reverted array to store elements copy of numbers
for i in numbers: #for loop to store copy elements from numbers to reverted array
revertedarray.append(i)
  
##here the concept to reversing the array is swapping first&last second &last but one...........
for i in range(int(len(revertedarray)/2)): #for loop in Time Complexity O(n/2)
#swapping process
temp=revertedarray[i]
revertedarray[i]=revertedarray[len(revertedarray)-i-1]
revertedarray[len(revertedarray)-i-1]=temp
  
max=revertedarray[0] #take first element in list as max value initially
min=revertedarray[0] #take first element in list as min value initially
for i in revertedarray:
if(i>max): #to find max
max=i
if(i<min): #to find min
min=i

print("Reverted array: ",end='') #printing reverted array
print(revertedarray)
print("Maximum Element: ",end='') #printing max element
print(max)
print("Minimum Element: ",end='') #printing min element
print(min)

diff=[] #diff list to store difference values between adjacent elements like (numbers[1] and numbers[0])
for i in range(1,len(numbers)):
diff.append(numbers[i]-numbers[i-1]) #appending each difference to diff list
trend=[] #trend list to store trend points
for i in range(1,len(numbers)-1):
if (diff[i]*diff[i-1] <=0): #if this value is negative means the sequence is changing to increasing to decreasing or decreasing to increasing
trend.append(numbers[i]) #appending trend values to list
print("Trend Values: ",end='')
print(trend) #printing trend points

Code Screenshot:-

File:-

Output:-


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