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...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
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...
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...
Write a python program to read from a file the names and grades of a class...
Write a python program 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 gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
Goal: to write a Python program that will read a playlist from a CSV file and...
Goal: to write a Python program that will read a playlist from a CSV file and display it in the console in the form of a table. https://s3.eu-west-2.amazonaws.com/bb-python-modules/Coursework/CW3/playlist_text_question.html The following is a link to the question. It includes all instruction and a template file (with additional instructions) where the answer must be completed.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT