Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This project is done in python. The assignment requires that I write one method call OrderCurrencies. Just this method and nothing else is to be

This project is done in python. The assignment requires that I write one method call OrderCurrencies. Just this method and nothing else is to be changed. Skeleton code was provided as well as some test cases. You are to replace the 'pass' from the method OrderCurrencies with the algorithm necessary. I will copy over the skeleton code and test cases after the assignment description. What I need is just the algorithm/code used in OrderCurrencies.

This is the assignment descriptionotes:

image text in transcribed

image text in transcribed

SKELETON CODE THAT WAS PROVIDED (with comments):

import sys

############### # author: # # project #1 # # description: ###############

def ChooseOrdering(ascending, descending, fp_out=sys.stdout): ''' @pre: non empty array of integers that holds the countsof inversions for ascending each array non empty array of the same length that holds the count of inversions for descending each array file object to write to

@post: returns nothing, writes the output to a file.

function decides which sorting arrangement would be more efficent and prints the output to file ''' # DO NOT MODIFY - WILL DEDUCT POINTS if abs(sum(ascending) - sum(descending))

# directing output to file print("Total Number of portfolio's assessed: ", len(ascending), file=fp_out) print(" " * 12, "{:^12s} vs {:^12s}".format("Ascending", "Descending"), file=fp_out) for pairs in range(len(ascending)): print("Portfolio #{:02d} {:^12d} vs {:^12d}" \ .format(pairs + 1, ascending[pairs], descending[pairs]), file=fp_out) print(" Total Inversion Count", file=fp_out) print("Ascending: ", sum(ascending), " vs. Descending: ", sum(descending), file=fp_out)

print(" Best Form of Ordering: ", outcome, file=fp_out)

def OrderCurriences(fp, fp_out=sys.stdout): ''' @pre:

@post: ''' pass

def main(): fp = open(input("Insert Filename (with extension) for input: ").strip())

# comment back in for writing output to the file, manadatory for grading purposes. # fp_out = open(input("Insert Filename (with extension) for output: ").strip(), 'w')

# OrderCurriences(fp, fp_out) # fp_out.close()

# writes to standard output OrderCurriences(fp)

fp.close()

main()

END OF SKELETON CODE

Also here are some test cases that I was provided with (these were given as 6 seperate text files:

Test case 1: 1,2,30,0,02,3,2,1,55,4,3,2,1

Test case 2: -33,82,-967,-490 -500,461,-822,-515,-530,158,-631,270-973,827,-438,-969,804,248,-861,-492,799,-198,-485-558,-130,-226,-81,-566,181,22,-737,720,-274,572,-283,-86,-662,-509-322,646,44,276,653,288,996,-622,-238,26

Test case 3:-365,-172,508,-434,328,-744,87-792,-299,-524,705,-910708,-904,788,937,-918,829,231,0,-601,-335,886887,37,84,-62,-578,-702813,680,-400,745,-426,18,-396,-526,-363,910,817,785,-648,189,-194-262,-263,-857,223,-378,355,986,-833,173,-190,882

Test case 4: -654,655,-945,-623,133,-667-939,-615,-308,54,-23,-863,-567880,-101,-873,-608,49,-237-1000,734,600,-593,50,-126,-538,884,704,-30,228,-822,-797368,-366,-830,167,269,-788,809,-375,-910,389,546,-368,410,512,692-404,-15,-510,-35,933606,-546,487,951,932,300,391,120,690,-771,-26,-43,458-682,229,-75,620,-770,-934,799,-342,-413,812,-80-796,-841,-644,663,-492,126,-233,23,172,-790,809,-963 660,314,727-149,-871,975,-287,938,517,77-589,369,-887,-691,162,-46,665,-427,-319,579,642,539,-380-963,-857,-401,365,492,998,403,-172,-892,150,804

Test case 5: 611,-956,-150,358,81,663,847,-160,-379,25,287791,1,-920,359,169,536,-496,-133

Test case 6: -870,-305,-535,-65,-874,397-893,441,359,611,-457755,-206,980,857,-582-613,353,880,816,-997,43,347,-716,-516,322,283,428,237-886,-750,413,-806,367,128,505,-889,-415153,-185,-962,-874,-752,-168,575,-909,355,-626,69,849,-200,-329,63,-438-902,-205,165-929,-889,62,849,-419,-282-310,875,997,973,-543,999,850,-74,38,-370,170,900,560,-762,-618,-987-241,968,533,349,-887,-351,850,278,278,206-663,-135,-997,-145,586,-511,313,982,-443,623,-447

Assigniment Overview You are a hedge fund portfolio manager. Your client wants to invest in a mutual fund made up of all different cryptocurrencies. Given an array denoting the value of each cryptocurrency, you must sort them ascending and descending. But your boss doesn't like to waste time and resources. So, for each ascending sort and descending sort, count the number of inversions it took to sort it both ways. Inversions can be defined, https://en wikipedia.org/wiki/Inversion (discrete mathematics) At the end of all portfolios (arrays), return which ordering was least expensive throughout all the Portfolios. Here I will be defining expensive as the exceeding value of inversions per array Assigniment Deliverables CryptoSort.py Be sure to use the specified file name(s) and to submit your files for grading viaDropbox before the project deadline Assignment Specifications There is an obvious solution that can be done using an O(n) algorithm. Though it may be a good approach for initial problem solving, you are required to implement an algorithm that's faster, guaranteed O( nlog(n)) Your task will be to complete the methods listed below OrderCurriences The interaction of the program should read in a file containing arrays of numbers separated by commas. Each line will have a new array that should be assessed. I have provided a print function, "ChooseOrdering", which will be handling the output for final submission. You should pass a single array denoting the count of inversions for all the arrays sorted ascending as the first parameter. The second parameter should be a single array denoting the count of inversions for all the arrays sorted descending. Finally, the last parameter should be a file object that should get written to by the predefined function Assigniment Overview You are a hedge fund portfolio manager. Your client wants to invest in a mutual fund made up of all different cryptocurrencies. Given an array denoting the value of each cryptocurrency, you must sort them ascending and descending. But your boss doesn't like to waste time and resources. So, for each ascending sort and descending sort, count the number of inversions it took to sort it both ways. Inversions can be defined, https://en wikipedia.org/wiki/Inversion (discrete mathematics) At the end of all portfolios (arrays), return which ordering was least expensive throughout all the Portfolios. Here I will be defining expensive as the exceeding value of inversions per array Assigniment Deliverables CryptoSort.py Be sure to use the specified file name(s) and to submit your files for grading viaDropbox before the project deadline Assignment Specifications There is an obvious solution that can be done using an O(n) algorithm. Though it may be a good approach for initial problem solving, you are required to implement an algorithm that's faster, guaranteed O( nlog(n)) Your task will be to complete the methods listed below OrderCurriences The interaction of the program should read in a file containing arrays of numbers separated by commas. Each line will have a new array that should be assessed. I have provided a print function, "ChooseOrdering", which will be handling the output for final submission. You should pass a single array denoting the count of inversions for all the arrays sorted ascending as the first parameter. The second parameter should be a single array denoting the count of inversions for all the arrays sorted descending. Finally, the last parameter should be a file object that should get written to by the predefined function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions