Question

In: Computer Science

Please solve using simple python programming language and make it easy to understand explain your code...

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.

  • There are two files with numbers in them
  • Each line in the files can contain one or more numbers, if there are more than one number on a line, they are separated by spaces
  • Both files will contain both odd and even numbers
  • You need to create a filter that can pick all odd numbers from one file and all even numbers from the other file
  • You then need to combine all the filtered out odd and even numbers (from the two files), and sort them in a reversed order using something called Bubble Sort.

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.  

Solutions

Expert Solution

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.


Related Solutions

*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Please code the following, using the language java! Build a simple calculator that ignores order of...
Please code the following, using the language java! Build a simple calculator that ignores order of operations. This “infix” calculator will read in a String from the user and calculate the results of that String from left to right. Consider the following left-to-right calculations: "4 + 4 / 2" "Answer is 4" //not 6, since the addition occurs first when reading from left to right “1 * -3 + 6 / 3” “Answer is 1” //and not -1Start by copying...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing on your experience from program 1, read an integer, n from keyboard (standard input). This integer represents the number of integers under consideration. After reading that initial integer in, read n integers in, and print the minimum and maximum of all the integers that are read in. Example: Input Output 7 1 5 3 6 9 22 2 Min: 1 Max: 22 C++ preferred
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT