Rise, sequence, rise
Pyth, 31 30 29 bytes
e+0f.x!sgM.:iVczdT2ZosN^STlcz
1 byte thanks to @Jakube.
Demonstration. Test Harness.
Input is given on STDIN, space separated. If newline separated input is allowed, I can shorten the program by 2 bytes.
Explanation:
e+0f.x!sgM.:iVczdT2ZosN^STlcz
Implicit: z = input(), T = 10, Z = 0, d = ' '
ST [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lcz len(z.split())
^ All combinations w/replacement of that length.
osN Order by increasing sum.
f Filter on
czd z.split(' ')
iV T Vectorize the "Convert to base" operation over
the integers as strings and the base sequence.
.: 2 Take length 2 subsequences.
gM Map the >= operation over them.
!s Sum and logically negate.
.x Z If that throws an error, returns 0 (e.g. reject)
+0 Prepend a 0, in case no sequences are found.
e Take the end of the list.
Including 1
in the list of possible bases is safe because i
, which uses Python's int
builtin, doesn't allow 1
as a base, and therefore always throws an error, which is caught and filtered out.
CJam, 43 bytes
0B,2>ea,m*{:+~}${ea::~_2$.b__Q|$=*@.b=}=p];
Reads command-line arguments and prints an array.
Try it online in the CJam interpreter.
Examples
$ cjam rise.cjam 12 11 10
[6 8 10]
$ cjam rise.cjam 19 18 17
0
How it works
0 e# Push a 0 (default return value).
B,2> e# Push [0 ... 10] and remove the first two elements.
ea, e# Push the number of command-line arguments (n).
m* e# Cartesian power. Pushes all vectors of {2 ... 10}^n.
{:+~}$ e# Sort by the negated sums.
{ e# Find; for each vector V in {2 ... 10}^n:
ea::~ e# Evaluate each character of each command-line argument.
_2$ e# Copy the results and V.
.b e# Vectorized base conversion (list to integer).
__ e# Push two copies.
Q|$ e# Deduplicate and sort the last copy.
= e# Compare it to the first. Pushes 1/0 if equal/unequal.
* e# Repeat the original result of .b that many times.
@.b e# Vectorized base conversion (integer to list).
= e# Compare the result to the modified command-line arguments.
e# Equality makes sure that the base was greater than all digits.
}= e# If pushed 1, push V and break.
p e# Print. Either prints the last V or 0 if none matched.
]; e# Clear the stack to avoid implicitly printing the 0 (if still present).
Julia, 176 156 145 118 109 99 97 bytes
A->try p=NaN;flipud(map(i->(k=11;t=p;while t<=(p=parseint("$i",k-=1))end;k),flipud(A)))catch;0end
Ungolfed:
function anonfunc(i)
# Start with k=11 so that it evaluates to 10 on first while iteration
k=11
# set t to the previous value of p
# Note: p here gets held over between iterations within the map
t=p
# Iterate through, dropping k by 1 and evaluating the integer in
# base k and stopping if the value drops below t
# Note: "p=" expression inside conditional to ensure k-=1 is evaluated
# at least once (to make NaN work as desired)
while t<=(p=parseint("$i",k-=1))
end
# if it dropped below t, return the base, k to be the corresponding
# element in the map
return k
end
function f(A)
# Using try/catch to return 0 if no acceptable base found
try
# This is a trick to make sure the comparison in the while loop
# evaluates to false on the first use of it (last value in A)
p=NaN
# Apply anonfunc to each element of A, starting with the last element
# and store the result in S
S=map(anonfunc,flipud(A))
# S is backwards, so flip it and return it
return flipud(S)
catch
# Will throw to here if parseint fails with the base due to having
# a digit not acceptable in the base
return 0
end
end
Used with a 1d array input. If the function is assigned to c
, then you would call c([12,11,10])
and it would output [6,8,10]
.
Note: I had used dec(i)
inside the parseint command, but because i
is a single-character variable name, and I don't need to access a component, I used "$i"
to get the same result.