Inputs
A list (array) of numbers, or numerical strings if that makes it easier. You can assume there will always be at least two elements in the list and every element will be a natural number (integer larger than zero).
Outputs
A single number, or again, a numerical string.
Problem
The idea is to reduce the list of numbers by removing the last digit of the largest number at that current stage of the list, eventually ending with one number (only one number should be returned, even if there are multiple instances)
Example
[123,343,121,76,465,786] -- The last digit in 786 is dropped, so it becomes 78
[123,343,121,76,465,78] -- New largest number is 465, so the 5 is dropped, making it 46
[123,343,121,76,46,78] -- Repeat until left with one number
[123,34,121,76,46,78]
[12,34,121,76,46,78]
[12,34,12,76,46,78]
[12,34,12,76,46,7]
[12,34,12,7,46,7]
[12,34,12,7,4,7]
[12,3,12,7,4,7]
[1,3,1,7,4,7] -- If there are multiple max numbers, you **must** remove the last digit from all of them
[1,3,1,4]
[1,3,1]
[1,1] -- You have your answer when there is one number, or multiple numbers that are equal
1 -- Result
Loopholes
Other constraints
Your program must work for any list of random numbers (within reason of course)
Test Cases
[24,72,4]
[24,7,4]
[2,7,4]
[2,4]
[2]
2
[14, 7]
[1, 7]
[1]
1
[1278,232,98273,2334]
[1278,232,9827,2334]
[1278,232,982,2334]
[1278,232,982,233]
[127,232,982,233]
[127,232,98,233]
[127,232,98,23]
[127,23,98,23]
[12,23,98,23]
[12,23,9,23]
[12,2,9,2]
[1,2,9,2]
[1,2,2]
[1]
1
Scoring
This is code-golf, so shortest answer in every language wins!
18 Answers 18
-
2\$\begingroup\$ This can be shortened 4 bytes while keeping the same idea using
min(zip(*l)[0]). \$\endgroup\$notjagan– notjagan2017年07月06日 23:51:50 +00:00Commented Jul 6, 2017 at 23:51
Python 3, (削除) 24, 21 (削除ここまで), 18 bytes
lambda l:min(l)[0]
(削除) Three (削除ここまで) Six bytes saved thanks to @totallyhuman!
Mathematica, 29 bytes
Min[First@*IntegerDigits/@#]&
Japt, (削除) 8 (削除ここまで) (削除) 6 (削除ここまで) 5 bytes
-1 byte thanks to @Shaggy
n g g
Takes input as an array of numeric strings. Try it online!
Explanation
// implicit input: array of strings
n // sort the array
g // get the first element
g // get the first character
// implicit output
-
-
\$\begingroup\$ @Shaggy Oh duh, I totally over complicated this one. Thanks! \$\endgroup\$Justin Mariner– Justin Mariner2017年07月07日 14:22:26 +00:00Commented Jul 7, 2017 at 14:22
-
\$\begingroup\$ No problem :)
n v gwould also work for 5 bytes. Welcome to Japt, by the way. \$\endgroup\$Shaggy– Shaggy2017年07月07日 14:25:55 +00:00Commented Jul 7, 2017 at 14:25
-
\$\begingroup\$ Fails for
[12,23,12]. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:16:02 +00:00Commented Jul 7, 2017 at 13:16 -
\$\begingroup\$ @OlivierGrégoire: How? Removing the digits in the order
3,2,2,2,1results in 1 as expected. \$\endgroup\$Emigna– Emigna2017年07月07日 13:28:58 +00:00Commented Jul 7, 2017 at 13:28 -
\$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:33:14 +00:00Commented Jul 7, 2017 at 13:33
-
\$\begingroup\$ Fails for
[12,23,12]. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:14:30 +00:00Commented Jul 7, 2017 at 13:14 -
\$\begingroup\$ @OlivierGrégoire [12,23,12]->[12,2,12]->[1,2,12]->[1,2,1]->[1,1]->[1]->1 is in my opinion correct \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年07月07日 13:30:46 +00:00Commented Jul 7, 2017 at 13:30
-
\$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:32:21 +00:00Commented Jul 7, 2017 at 13:32
V, (削除) 11 (削除ここまで), 5 bytes
ÚxV}p
I was making this waaay more complicated than it actually is. This answer simply sorts every line by ASCII values, and then returns the very first character. Since this is kind or a boring answer, here is a more interesting answer that actually implements the algorithm originally described:
V, 11 bytes
òún
/äîä
Lx
-
\$\begingroup\$ I was too when I posed the question. Your original answer was what I expected most to look like. Bummer. \$\endgroup\$Henry– Henry2017年07月06日 21:51:16 +00:00Commented Jul 6, 2017 at 21:51
Jelly, (削除) 3 (削除ここまで) 2 bytes
ṂḢ
A full program which takes a list of lists of characters (strings) and prints the result.
How?
We just need to return the smallest leading digit...
ṂḢ - Main link: list of lists of characters
Ṃ - minimum (lexicographical ordering ensures this will start with the minimal digit)
Ḣ - head (get that first digit character)
-
\$\begingroup\$ No problem, it happens. \$\endgroup\$Jonathan Allan– Jonathan Allan2017年07月07日 13:38:13 +00:00Commented Jul 7, 2017 at 13:38
JavaScript (ES6), 17 bytes
Takes input as an array of strings.
a=>a.sort()[0][0]
Try it
Input a comma separated list of numbers.
o.innerText=(f=
a=>a.sort()[0][0]
)((i.value="1278,232,98273,2334").split`,`);oninput=_=>o.innerText=f(i.value.split`,`)
<input id=i><pre id=o>
,,,, 3 bytes
⫰1⊣
Explanation
⫰1⊣
⫰ pop the whole stack and push the minimum element
1 push 1
⊣ pop the minimum and 1 and push the first character of it
Braingolf, 17 bytes
VVR{Mvd<M&$_R}vvx
Explanation
VVR{Mvd<M&$_R}vvx Implicit input from commandline args
VVR Create stack2 and stack3, return to stack1
{.........} Foreach item in stack..
M ..Move item to next stack
v ..Switch to next stack
d ..Split item into digits
<M ..Move first item to next stack
&$_ ..Clear stack
R ..Return to stack1
vv Switch to stack3
x Reduce to lowest value
Implicit output of last item on stack
In other words, it constructs a stack consisting of only the first digit of each item, then outputs the lowest.
This challenge gave me a bunch of useful ideas for builtins to add to Braingolf, and now thanks to the addition of the "special" foreach loop, Braingolf can do it in 5 bytes:
Braingolf, 5 bytes [non-competing]
(d<)x
Explanation
(d<)x Implicit input from commandline args
(..) Special foreach loop, iterates over the stack, moving each item to a special
Sandboxed stack environment, and prepends the last item of the sandboxed
stack to the real stack at the end of each iteration
d< Split into digits, move first digit to end of stack
x Reduce to lowest value
Implicit output of last item on stack
I'm normally against adding builtins just to complete one challenge, but I can see a plethora of uses for the new (...) foreach loop, so I don't really consider it adding a feature just for this challenge.
-
\$\begingroup\$ Fails for
[12,23,12]. Expected output is2, you returned1. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:24:48 +00:00Commented Jul 7, 2017 at 13:24 -
\$\begingroup\$ @OlivierGrégoire The expected output of that is
1:[12,23,12] > [12,2,12] > [1,2,1] > [1,1]\$\endgroup\$Mayube– Mayube2017年07月07日 13:28:04 +00:00Commented Jul 7, 2017 at 13:28 -
\$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月07日 13:33:37 +00:00Commented Jul 7, 2017 at 13:33
Pip, 5 bytes
Takes the list of input numbers as command-line arguments.
@@SSg
Alternately:
MN@Zg
Explanations
In both programs, g is the list of command-line args.
@@SSg
SS sorts using string comparison, thus putting the numbers with smallest first digits first, regardless of their magnitudes. Unary @ gives the first element of a list or scalar. We apply it twice to get the first digit of the first number after sorting.
g [24 72 491]
SS [24 491 72]
@ 24
@ 2
Alternately:
MN@Zg
Z is zip; its unary version can be used to transpose a list. The first element of the transposed list is a list of the first digits of all the numbers. @ gets that list of digits; MN takes its minimum.
g [24 72 491]
Z [[2 7 4] [4 2 9]]
@ [2 7 4]
MN 2
Pyth, (削除) 9 (削除ここまで) 7 bytes
hSmsh`d
Explanation
This basically return the smallest leading digit.
Q # Implicit input
msh`d # For each number in Q, convert to string, take the first character, convert to integer
hS # Return the minimum
Python 3, 33 bytes
lambda l:min(str(x)[0]for x in l)
@DJMcMayhem and @totallyhuman have a better solutions but mine assumes numerical input instead of string.
Pyth, 3 bytes
hhS
Input is list of string representations of numbers.
Explanation:
hhS
# Q=input
S # Sort Q
h # First Element of sorted list
h # First element of string
# Implicitly print result
[12, 123, 124]is12, which makes every single posted answer wrong. \$\endgroup\$