[Tutor] crashed with Runtime Error: NZEC (non-zero exit code)
bob gailer
bgailer at alum.rpi.edu
Tue Feb 26 20:09:46 CET 2008
I will share my algorithm just for the heck of it and to see if you see
any problem.
Underlying theory:
Divisible by 15 == divisible by 3 and divisible by 5
If a number is divisible by 3 any rearrangement of its digits is also
divisible by 3.
Therefore to get the largest number, put the digits in descending order.
If the number is not divisible by 3 it can be made divisible by 3 by
removing one or 2 non-multiple-of-3 digits
To be divisible by 5 the rightmost digit must be 0 or 5. If it is
neither then a 5 must be moved from the interior to the end.
s = the test case input
If there are no '0's and no '5's in s then impossible.
Convert s (string) to a list, sort it, reverse it. If rightmost digit is
not '0' or '5', remove a '5' from the list
(we will append it at checkout)
On a "parallel path":
i = int(s) # make numeric
r = i % 3 # get modulo
if r == 0 # divisible by 3 (regardless of the order of the digits)
proceed to checkout
else
attempt to remove one or two digits to bring the modulo to 0
if possible remove just 1 digit (the lower the better) (the candidates
are, for r ==1-1,4,7 r==2-2,5,8)
otherwise if possible remove 2 digits (the lower the better) (the
candidates are, for r ==2-1,4,7 r==1-2,5,8)
otherwise impossible
checkout:
if we removed a '5' from the list, append it
join and print the list
--
Bob Gailer
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list