Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions array.txt
Original file line number Diff line number Diff line change
@@ -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 ]
54 changes: 54 additions & 0 deletions greatest.py
Original file line number Diff line number Diff line change
@@ -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)