9
\$\begingroup\$

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

Standard loopholes apply

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 , so shortest answer in every language wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ Please let me know if I missed anything. First question. \$\endgroup\$ – Henry Jul 6 '17 at 20:45
  • 14
    \$\begingroup\$ Dunno if it's too late to change, but the question might be better if we did have to output each stage. I think the answers will be quite straightforward otherwise. \$\endgroup\$ – DLosc Jul 6 '17 at 20:59
  • 8
    \$\begingroup\$ Since no one else has mentioned it, this is the kind of thing that would frequently be caught in the sandbox. \$\endgroup\$ – DJMcMayhem Jul 6 '17 at 21:52
  • 1
    \$\begingroup\$ Can you add testcases where the answer is not the first digit of the first entry in the list? \$\endgroup\$ – JAD Jul 7 '17 at 9:54
  • 5
    \$\begingroup\$ The way I read this question the answer for [12, 123, 124] is 12, which makes every single posted answer wrong. \$\endgroup\$ – Ørjan Johansen Nov 17 '17 at 1:15

18 Answers 18

10
\$\begingroup\$

Haskell, 16 bytes

minimum.map head

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python 2, 24 bytes

-4 bytes thanks to notjagan.

lambda l:min(zip(*l)[0])

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ This can be shortened 4 bytes while keeping the same idea using min(zip(*l)[0]). \$\endgroup\$ – notjagan Jul 6 '17 at 23:51
3
\$\begingroup\$

Python 3, 24, 21, 18 bytes

lambda l:min(l)[0]

Try it online!

Three Six bytes saved thanks to @totallyhuman!

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 29 bytes

Min[First@*IntegerDigits/@#]&
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 5 bytes: Sort the array, get the first element, get the first character. \$\endgroup\$ – Shaggy Jul 7 '17 at 14:18
  • \$\begingroup\$ @Shaggy Oh duh, I totally over complicated this one. Thanks! \$\endgroup\$ – Justin Mariner Jul 7 '17 at 14:22
  • \$\begingroup\$ No problem :) n v g would also work for 5 bytes. Welcome to Japt, by the way. \$\endgroup\$ – Shaggy Jul 7 '17 at 14:25
2
\$\begingroup\$

PHP, 45 bytes

<?foreach($_GET as$v)$r[]=$v[0];echo min($r);

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Fails for [12,23,12]. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 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 Jul 7 '17 at 13:30
  • \$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 at 13:32
2
\$\begingroup\$

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>

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 3 bytes

€нW

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Fails for [12,23,12]. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 at 13:16
  • \$\begingroup\$ @OlivierGrégoire: How? Removing the digits in the order 3,2,2,2,1 results in 1 as expected. \$\endgroup\$ – Emigna Jul 7 '17 at 13:28
  • \$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 at 13:33
1
\$\begingroup\$

V, 11, 5 bytes

ÚxV}p

Try it online!

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I was too when I posed the question. Your original answer was what I expected most to look like. Bummer. \$\endgroup\$ – Henry Jul 6 '17 at 21:51
1
\$\begingroup\$

,,,, 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
\$\endgroup\$
1
\$\begingroup\$

Jelly,  3  2 bytes

ṂḢ

A full program which takes a list of lists of characters (strings) and prints the result.

Try it online!

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)
\$\endgroup\$
  • \$\begingroup\$ No problem, it happens. \$\endgroup\$ – Jonathan Allan Jul 7 '17 at 13:38
1
\$\begingroup\$

Braingolf, 17 bytes

VVR{Mvd<M&$_R}vvx

Try it online!

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

Try it online!

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.

\$\endgroup\$
  • \$\begingroup\$ Fails for [12,23,12]. Expected output is 2, you returned 1. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 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\$ – Skidsdev Jul 7 '17 at 13:28
  • \$\begingroup\$ My bad, I misread. See previous comment. \$\endgroup\$ – Olivier Grégoire Jul 7 '17 at 13:33
1
\$\begingroup\$

Funky, 18 bytes

x=>x::sort()[0][0]

Takes input as a list of strings.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pip, 5 bytes

Takes the list of input numbers as command-line arguments.

@@SSg

Try it online!

Alternately:

MN@Zg

Try it online!

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
\$\endgroup\$
0
\$\begingroup\$

PHP, 27 bytes

<?=substr(max($_GET),0,-1);

(Wow, totally misunderstood the question. This does not work. Will edit later.)

\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 7 bytes

hSmsh`d

Try it online!

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
\$\endgroup\$
0
\$\begingroup\$

Python 3, 33 bytes

lambda l:min(str(x)[0]for x in l)

Try it online!

@DJMcMayhem and @totallyhuman have a better solutions but mine assumes numerical input instead of string.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 3 bytes

hhS

Input is list of string representations of numbers.

Try It Online

Explanation:

hhS
    # Q=input
  S # Sort Q
 h  # First Element of sorted list
h   # First element of string
    # Implicitly print result
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.