In: Computer Science
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
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:-