1- """py_sorter.sorter: provides argument parsing and entry point main()."""
2-
3- __version__ = "0.2.1"
4-
1+ """py_sorter.sorter: provides argument parsing and entry point main().
2+ Any calls to the sorts methods are called from here based on the -s/--sort
3+ argument specified by the user.
4+ """
5+ __version__ = "0.3.0"
56
67import argparse
78import os
@@ -17,16 +18,16 @@ def parse_args(args):
1718 """
1819 parser = argparse .ArgumentParser (description = 'sort some integers.' )
1920
20- # Parser group created to take in either one argument.
21+ # Parser group created to take in either argument. (-i or -g) .
2122 # Integers manually specified or generated on the fly.
2223 integers_group = parser .add_mutually_exclusive_group (required = True )
2324 integers_group .add_argument ('-i' , '--integers' , type = int , nargs = '+' ,
2425 help = 'integer(s) being sorted.' )
2526 integers_group .add_argument ('-g' , '--generate' , type = int ,
2627 help = 'generate a random list of integers to sort.' )
2728
29+ # Specific sort algorithm or use all sort algorithms.
2830 sorting_group = parser .add_mutually_exclusive_group (required = True )
29- # Specify a specific sorting algorithm.
3031 sorting_group .add_argument ('-s' , '--sort' , type = str ,
3132 choices = ['bubble' , 'bogo' , 'merge' , 'selection' , 'quick' , 'radix' , 'insertion' ,
3233 'insertion_recursive' , 'heap' ],
@@ -38,19 +39,23 @@ def parse_args(args):
3839 parser .add_argument ('-l' , '--list' , action = 'store_true' ,
3940 help = 'displays the original and unsorted lists if present.' )
4041
42+ # Compare argument to display the time difference compared to the default python sorted() function.
43+ parser .add_argument ('-c' , '--compare' , action = 'store_true' ,
44+ help = 'display the difference in time compared to pythons default \' sorted()\' function.' )
45+
4146 return parser .parse_args (args )
4247
4348
4449def print_error (args , error ):
4550 """Print an error to the screen when a sort fails."""
46- print ('\t Algorithm: [%s]' % str . upper (args .sort ))
51+ print ('\t Algorithm: [%s]' % str (args .sort ))
4752 print ("\t error occurred while sorting: %s" % error )
4853 print ("\t " )
4954
5055
5156def print_results (args , sorted_list , time ):
5257 """Print an original list, sorted list and time required to sort the original list."""
53- print ('\t Algorithm: [%s]' % str . upper (args .sort ))
58+ print ('\t Algorithm: [%s]' % str (args .sort ))
5459
5560 # Determine whether or not the original and sorted lists will be printed
5661 # as part of the results output.
@@ -62,11 +67,31 @@ def print_results(args, sorted_list, time):
6267 for sorted_chunk in print_list_chunks (sorted_list , 20 ):
6368 print ('\t ' + str (sorted_chunk )[1 :- 1 ])
6469
65- # Print time required to sort list.
66- print ('\t Time(seconds): %s' % time )
70+ # Check for the --compare flag being present, and print out the difference
71+ # between this results time and the default sorted() function time.
72+ if args .compare :
73+ print ('\t Time(seconds) %s: %s' % (args .sort , time ))
74+ print ('\t Time(seconds) sorted(): %s' % args .compare_time )
75+ print ('\t %s' % calculate_compare_time_difference (time , args .compare_time , args .sort ))
76+ else :
77+ print ('\t Time(seconds): %s' % time )
78+
6779 print ('\t ' )
6880
6981
82+ def calculate_compare_time_difference (algorithm_time , default_time , algorithm ):
83+ """Calculate the difference between a custom algorithms time taken to sort, and
84+ pythons sorted() function time taken to sort the same list, returns a readable string
85+ detailing which was faster.
86+ """
87+ difference = algorithm_time - default_time
88+
89+ if difference > 0 :
90+ return '%s was %s seconds slower.' % (algorithm , difference )
91+ else :
92+ return '%s was %s seconds faster.' % (algorithm , - difference )
93+
94+
7095def print_list_chunks (integer_list , n ):
7196 """Simple helper method to print out a list in chunks. This allows
7297 the console output to never surpass a specific width. Giving a cleaner
@@ -130,6 +155,14 @@ def sort(args):
130155
131156 # Final default_timer() method call to grab time after sort is completed.
132157 final = timeit .default_timer ()
158+
159+ # Check for compare flag and get the time taken for the default sorted() call on list.
160+ if args .compare :
161+ default_initial = timeit .default_timer ()
162+ default_list = sorted (original_list )
163+ default_final = timeit .default_timer ()
164+ args .compare_time = default_final - default_initial
165+
133166 print_results (args , sorted_list , final - initial )
134167
135168
0 commit comments