please code in python the below code has alot of errors .. please make it error free import numpy as np import random import sys import os from datetime import datetime import string import subprocess from subprocess import call from subprocess import check_output if len(sys.argv) < 3: print('Usage: maintest.py Nm Ns R \n') exit() Ns = sys.argv[1] #sort size R = sys.argv[2] #number of repetitions w = open('BubbleSortResult.txt', 'w') #BubbleSort1: No Split Input = np.random.rand(Ns) Itmp = list (Input) def sort (numbers): sorted = False while not sorted: sorted = True for i in range (len (numbers)-1): if numbers [1] > numbers [i + 1]: sorted = False numbers [i + 1], numbers [i] = numbers[i], numbers [ i + 1] Te = datetime. now () for i in range (R): #Run R-time Itmp = list (Input) # Get fresh Input #print (Itmp) sort (Itmp) # This is in-place sort. Input will be altered. Te = datetime. now () - Te Totalus = float ((Te.seconds * 1000000 + Te.microseconds)/R) w.write("Full BubbleSort Results: \n {N} {T} \n". format (N=NS, T=Totalus)) w.close() w = open("SortResult-1.txt", "W") for i in range (len (Itmp)): w.write (str (Itmp[i])+"\n") w.close() w = open ("BubbleSortResult.txt", "a") ##open BubbleSort log file # BubbleSort2: Split input in half. sort then merge. def merge (list1i, list2): output = [] i = j = k = 0 while k< (Ns): print ("{ } { } { } {} \n".format(k, (Ns//2), i, j,)) if list1[i] < list2[j] : output.append (list1[i]) i = i + 1 if i > len (list1)-1: output.extend (list2[j:]) break else: output.append (list2[j]) j = j + 1 if j > len (list2) -1: output.extend (list1[i:]) break k=k + 1 return output Te = datetime.now () for i in range (R): Itmp = list (Input) # Use the same Input 78 List1 = Itmp[O:Ns//2] print (List1) List2 = Itmp[ (Ns//2):Ns] print (List2) sort (List1) sort (List2) output = merge (List1, List2) Te = datetime. now () - Te Totalus = float(Te.seconds * 1000000 + Te.microseconds) /R w.write("Split BubbleSort Results: \n Size={N} ElapsedTime={T} \n". format(N=Ns, T=Totalus)) w.close() w = open("SortResult-2.txt", "w") for i in range (len (output)): w .write (str (output[i])+"\n") w.close() out = subprocess.run(["diff","SortResult-1.txt","SortResult-2.txt"]) if len (out) != 0: print ("Sort Difference found: "+" | "+out+" | ") exit () else: print ("Sort No difference found ") subprocess.run ( ["rm", "SortResult-1.txt"]) subprocess.run(["rm", "SortResult2.txt"]) print ("All done!!")
please code in python
the below code has alot of errors .. please make it error free
import numpy as np
import random
import sys
import os
from datetime import datetime
import string
import subprocess
from subprocess import call
from subprocess import check_output
if len(sys.argv) < 3:
print('Usage: maintest.py Nm Ns R \n')
exit()
Ns = sys.argv[1] #sort size
R = sys.argv[2] #number of repetitions
w = open('BubbleSortResult.txt', 'w')
#BubbleSort1: No Split
Input = np.random.rand(Ns)
Itmp = list (Input)
def sort (numbers):
sorted = False
while not sorted:
sorted = True
for i in range (len (numbers)-1):
if numbers [1] > numbers [i + 1]:
sorted = False
numbers [i + 1], numbers [i] = numbers[i], numbers [ i + 1]
Te = datetime. now ()
for i in range (R): #Run R-time
Itmp = list (Input) # Get fresh Input
#print (Itmp)
sort (Itmp) # This is in-place sort. Input will be altered.
Te = datetime. now () - Te
Totalus = float ((Te.seconds * 1000000 + Te.microseconds)/R)
w.write("Full BubbleSort Results: \n {N} {T} \n". format (N=NS, T=Totalus))
w.close()
w = open("SortResult-1.txt", "W")
for i in range (len (Itmp)):
w.write (str (Itmp[i])+"\n")
w.close()
w = open ("BubbleSortResult.txt", "a") ##open BubbleSort log file
# BubbleSort2: Split input in half. sort then merge.
def merge (list1i, list2):
output = []
i = j = k = 0
while k< (Ns):
print ("{ } { } { } {} \n".format(k, (Ns//2), i, j,))
if list1[i] < list2[j] :
output.append (list1[i])
i = i + 1
if i > len (list1)-1:
output.extend (list2[j:])
break
else:
output.append (list2[j])
j = j + 1
if j > len (list2) -1:
output.extend (list1[i:])
break
k=k + 1
return output
Te = datetime.now ()
for i in range (R):
Itmp = list (Input) # Use the same Input 78
List1 = Itmp[O:Ns//2]
print (List1)
List2 = Itmp[ (Ns//2):Ns]
print (List2)
sort (List1)
sort (List2)
output = merge (List1, List2)
Te = datetime. now () - Te
Totalus = float(Te.seconds * 1000000 + Te.microseconds) /R
w.write("Split BubbleSort Results: \n Size={N} ElapsedTime={T} \n". format(N=Ns, T=Totalus))
w.close()
w = open("SortResult-2.txt", "w")
for i in range (len (output)):
w .write (str (output[i])+"\n")
w.close()
out = subprocess.run(["diff","SortResult-1.txt","SortResult-2.txt"])
if len (out) != 0:
print ("Sort Difference found: "+" | "+out+" | ")
exit ()
else:
print ("Sort No difference found ")
subprocess.run ( ["rm", "SortResult-1.txt"])
subprocess.run(["rm", "SortResult2.txt"])
print ("All done!!")
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images