In: Computer Science
Please solve using simple python programming language and make it easy to understand explain your code as I am a beginner, use appropriate variable names which make the code easy to understand and edit if needed.
A subsystem responsible for delivering priority numbers to an automated irrigation system has stopped working and you need to deliver a quick fix that will work until the actual subsystem is fixed by senior developer.
As you are the newest addition to the development team you have not fully grasped the complete picture of how all the systems work yet but you are confident you can solve this as you got a few hints from a senior developer.
Here is what the senior developer told you as he was running out the door.
He did not say it explicitly but you also understood that when you are done with the above steps, you need to display the sorted list of numbers on the screen.
When it comes to the design of the script there are a few requirements, there needs to be a main function that parses two command line arguments to file paths. From the first file (first argument) all odd numbers are read, and from the second file (second argument) all even numbers are read. The two lists of numbers are then combined and reverse sorted. The result from the sort is displayed on screen. Besides the main function the following functions must be present, and used.
read_file(filename)
Reads all the numbers in the specified file and adds them to a list
as integers. The list is returned from the function.
filter_odd_or_even(numbers, odd)
The first parameter is a list of numbers and the second parameter
is a Boolean value specifying if the filter function shall keep the
odd numbers (True) or the even numbers (False). The function shall
create a new list that is filled with either the odd or even
numbers from the parameter list depending on the odd parameter. The
new, filtered, list shall be returned from the function.
reversed_bubble_sort(numbers)
Takes a list of integer numbers as parameter and sorts it in place.
Sorting it in place means there is no need to return anything from
the function, the calling function will already have access to the
sorted list. The sorting shall be done using Bubble Sort (Links to
an external site.), but in reverse order. Reverse order means that
the biggest number shall be first and the smallest last. The
implementation of Bubble Sort shall not be optimized using the
optimization described in the link above.
SOLUTION: CODE FOR PYTHON PROGRAM.
import sys
L1=[]
L2=[]
L3=[]
L4=[]
LL=[]
LL1=[]
LL2=[]
def readfile(filename):
L1.clear()
with open(filename,'r') as file:
for line in file:
for N in line.split():
L1.append(int(N))
if filename==sys.argv[1]:
LL=list(filter_odd_or_even(L1, 1))
if filename==sys.argv[2]:
LL=list(filter_odd_or_even(L1, 0))
return LL
def filter_odd_or_even(numbers, odd):
L2.clear()
if odd==1:
for I in numbers:
if I%2!=0:
L2.append(I)
if odd==0:
for I in numbers:
if I%2==0:
L2.append(I)
return L2
def reversed_bubble_sort(numbers):
n = len(numbers)
# Traverse through all array elements
for i in range(n-1):
# Last i elements are already in place
for j in range(0, n-i-1):
if numbers[j] < numbers[j+1] :
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
return numbers
def main():
LL1=readfile(sys.argv[1])
#printing odd numbers read from first file
print("First File Odd Numbers",LL1)
LL2=readfile(sys.argv[2])
#printing even numbers read from second file
print("Second File Even Numbers",LL2)
#combining two lists in L3
L3=LL1+LL2
#sorting L3 in descending order
L4=list(reversed_bubble_sort(L3))
print(L4)
if __name__ == "__main__":
main()
CODING SCREENSHOT:
OUTPUT:
CONSIDERING TWO FILES NUMBERS1.TXT AND NUMBERS2.TXT EACH CONTAINING NUMBERS AS 1 2 3 4 5 6 7 8 9, I HAVE EXECUTED THE PROGRAM AND GOT THE OUTPUT CORRECT.