diff --git a/array.txt b/array.txt new file mode 100644 index 0000000..22f8ce4 --- /dev/null +++ b/array.txt @@ -0,0 +1,6 @@ +[ 50, 9, 17, 0] +[ 342, 689 ] +[ 43, 5, 12, 99 ] +[ 8, 4, 100 ] +[ 0, 1, 2, 3, 4, 5, 6, 7 ] +[ 1 ] \ No newline at end of file diff --git a/greatest.py b/greatest.py new file mode 100644 index 0000000..12215ac --- /dev/null +++ b/greatest.py @@ -0,0 +1,54 @@ +# +# Israel O. Dilan-Pantojas +# israelodilan@gmail.com +# + + +# To run program you may pass a file through cli standard input +# with an array or a list of numbers separated by commas as input. +# You may also input an Array directly from cli standard input. +# And finally you might want to enter it manually when you run +# the program, remember to use ("") quotations marks when doing so. + +# Ex. +# python greatest.py arrays.txt +# python greatest.py "[ 50, 9, 17, 0 ]" +# python greatest.py + +import sys +from itertools import permutations + +# Parse array from all input sources +def parse(a): + array = a.replace(" ", "").strip().lstrip("[(\"").rstrip("])\"").strip("[]]()\"").split(",") + return array + +# Determine max number from possible permutations of array +def greatest(x): + lst = [] + x = parse(x) + perms = list(permutations(x, len(x))) + for i in perms: + w = "" + for h in i: + w += h + lst.append(w) + print max(lst) + +# Try different input sources +try: + arg = sys.argv[1] + + try: + with open(arg, 'r') as f: + for line in f: + greatest(line) + + except: + greatest(arg) + +except: + arr = raw_input("Please input an array of valid natural integers separated by a comma: ") + greatest(arr) + + \ No newline at end of file