22
\$\begingroup\$

Introduction

This challenge consists in finding the greatest number removing y digits from the original number n which has x digits.

Assuming y=2 n=5263 x=4, the possible numbers removing y=2 digits are:

[52, 56, 53, 26, 23, 63]

So, the greatest number is 63 which must be the output for this example.


Another logic would be: for each y, search from left to right the digit which right next digit is greater, then remove it, else when no match, remove the last y-digits.

Using y=3 n=76751432 x=8 to explain:

y=3
76751432
-^------ remove 6 because right next 7 is greater

y=2
7751432
---^--- remove 1 because right next 4 is greater

y=1
775432
-----^ the search failed, then remove last y digits

result = 77543

Both methods explained above works.. of course, you can use another method too :)

Challenge

The number n won't have more than 8 digits, and y will always be greater than zero and lower than x.

To avoid strict input format, you can use the values: y n x the way you prefer: as parameters in function, raw input, or any other valid way. Just don't forget to say how you did that in your answer.

The output should be the result number.

This is , the shortest answer in bytes wins.

Example Input and Output

Again: you do not need to be too strict :)

4 1789823 7 -> 983
1 54132 5   -> 5432
3 69314 5   -> 94
2 51794 5   -> 794

Edit

I changed the input order to reflect the fact that some of you may not need the x value to solve the problem. x is now an optional value.

\$\endgroup\$
  • 2
    \$\begingroup\$ Please allow more general input and output, requiring a specific string format is usually a bad idea. \$\endgroup\$ – xnor Jan 13 '16 at 1:07
  • 1
    \$\begingroup\$ @LuisMendo I wouldn't mind editing the I/O in mine. ¯\_(ツ)_/¯ \$\endgroup\$ – Alex A. Jan 13 '16 at 1:16
  • 4
    \$\begingroup\$ -1 because of the strict I/O requirements, +1 for an interesting challenge. Overall, a solid sidevote. \$\endgroup\$ – Mego Jan 13 '16 at 4:39
  • 1
    \$\begingroup\$ The input format is too strict as others have said, especially considering that x is kind of a useless information. \$\endgroup\$ – Fatalize Jan 13 '16 at 8:10
  • 1
    \$\begingroup\$ @Fatalize Actually, I think that depending on the approach you take, having x as input can shorten the code. (Case in point: my Julia answer.) \$\endgroup\$ – Alex A. Jan 13 '16 at 16:58

13 Answers 13

3
\$\begingroup\$

A-Ray, 9 7 bytes

My new language! According to meta, this is allowed, but if this is not accepted, then I will remove it.

pM:i-II

Explanation:

  :i-II       Gets all permutations possible for the given number converted to an array,
                      with the length of y-x, which is the -II part
 M            Gets the maximum of the result above
p             Prints the resulting array above, with no separators

Example input (number, x, y):

1736413 7 4

Output:

764

You can test this with the .jar file given in the github link.

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

MATL, 10 bytes

-jowXncUX>

This uses version (9.2.1) of the language/compiler, which is earlier than this challenge.

It takes three inputs from stdin in this order: string length, number of removed characters, string.

Example

>> matl
 > -jowXncUX>
 > 
> 7
> 4
> 1789823
983

EDIT: Try it online! (the code in the link has XN instead of Xn to conform to changes in the language after this challenge; also, o is not needed anymore)

Explanation

(This still costs 2 bytes more than it should due to Octave's and Matlab's nchoosek function behaving differently. Fixed in the next release of the compiler.)

-        % implicitly input two numbers, and subtract them
jo       % input string, and convert to ASCII codes
wXn      % swap inputs. Generate all combinations, each in a row
c        % convert to char array
U        % convert each row to a number
X>       % get maximum. Implicitly display

Answer to original challenge (stricter input requirements): 16 bytes

jYbZ)b-wowXncUX>

Uses current version (9.2.1) of the language/compiler.

Example

>> matl jYbZ)b-wowXncUX>
> 4 1789823 7
983

Explanation

(This should have been 4 bytes less, but I need that wow...c because Octave's nchoosek function, unlike Matlab's, doesn't work with character input. Will be fixed for next release of the compiler.)

j              % input string
YbZ)           % split at spaces into strings
b-             % subtract first and third (1-digit) strings
wow            % convert middle string into ASCII codes
Xn             % get all combinations, each in a row
c              % convert to char array
U              % convert each row to a number
X>             % get maximum. Implicitly display
\$\endgroup\$
  • 3
    \$\begingroup\$ wow Your code is amazed at its own shortness ;) \$\endgroup\$ – ETHproductions Jan 13 '16 at 2:22
  • 3
    \$\begingroup\$ @ETHproductions Haha. Well, with the new input requirements it lost 6 bytes and got... speechless \$\endgroup\$ – Luis Mendo Jan 13 '16 at 10:44
3
\$\begingroup\$

Pyth - 11 9 8 bytes

eS.cz-QE

Test Suite.

\$\endgroup\$
  • \$\begingroup\$ Nice golf, but doesn't adhere to the input formatting? \$\endgroup\$ – Lui Jan 13 '16 at 2:52
  • \$\begingroup\$ @Lui oh, didn't see that it was that strict, fixing. \$\endgroup\$ – Maltysen Jan 13 '16 at 2:56
  • \$\begingroup\$ Fair enough, it looks like there is some discussions about it in the comments on the question itself, but it isn't resolved. \$\endgroup\$ – Lui Jan 13 '16 at 2:58
  • \$\begingroup\$ @L fixed. SPACE FILLER. \$\endgroup\$ – Maltysen Jan 13 '16 at 3:04
  • \$\begingroup\$ Looks better, but I think the input also has x on the same line, where x is the number of digits in the main integer? ie: 2 5263 4. \$\endgroup\$ – Lui Jan 13 '16 at 3:06
1
\$\begingroup\$

Japt, 19 bytes

Vs ¬àW-U m¬mn n!- g

Try it online!

How it works

        // Implicit: U = y, V = n, W = x
Vs ¬    // Convert V into a string, then split into an array of chars.
àW-U    // Generate all combinations of length W-U.
m¬mn    // Join each pair back into a string, then convert each string to a number.
n!-     // Sort by backwards subtraction (b-a instead of a-b; highest move to the front).
g       // Get the first item in this list.
        // Implicit: output last expression
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 30 bytes

,{,[N:Y].hs?lL,Nl-Y=L}?:1forh.

Since OP has relaxed the constraints on IO, this expects [Number, NumberOfDigitsRemoved] as input and returns the answer as output, e.g. brachylog_main([1789823,4], Z)..

Explanation

,{                   }?:1f     § Declare sub-predicate 1 and find all inputs which satisfy
                               § this sub-predicate with output = Input of the main predicate
                               § (i.e. [Number, Number of digits to remove])

                               § -- Sub-predicate 1 --
  ,[N:Y].                      § Output = [N, Y]
         hs?                   § Input = a subset of N
            lL,Nl-Y=L          § Length of Input = Length of N - Y

                          orh. § Order the list of answers, reverse it an return the first
                               § element (i.e. the biggest number of the list)
\$\endgroup\$
1
\$\begingroup\$

Python 3, 69 bytes

This defines an anonymous function accepting all three arguments. Taking full advantage of the rule that "you can use the values: y n x the way you prefer", I have chosen to accept y and x as integers and n as a string. The return value is a string.

from itertools import*
lambda y,n,x:''.join(max(combinations(n,x-y)))

Just in case anyone feels that this is stretching the rules too far, this version takes all inputs as integers and is 74 bytes.

from itertools import*
lambda y,n,x:''.join(max(combinations(str(n),x-y)))

And just for kicks, I also wrote a two-argument version, taking y and n from the command line and printing the result to STDOUT. It's 92 bytes.

import sys,itertools as i
_,y,n=sys.argv
print(*max(i.combinations(n,len(n)-int(y))),sep='')
\$\endgroup\$
1
\$\begingroup\$

ES6, 70 bytes

r=(y,n)=>y?r(y-1,Math.max(...`${n}`.replace(/./g," $`$'").split` `)):n

Returns a numeric result unless y is falsy and n is a string. I've convinced myself that doing the recursion the wrong way around still works (my solution isn't applicable to doing the correct recursion).

Also my first code golf where I use all three quote signs (although not all as quotes), which prevented me from trivially calculating the length.

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

Julia, 128 95 bytes

f(y,n,x)=maximum(i->parse(join(i)),filter(k->endof(k)==x-y,reduce(vcat,partitions(["$n"...]))))

This is a function that accepts the three values as parameters and returns an integer.

Ungolfed:

function f{T<:Integer}(y::T, n::T, x::T)
    # Get all ordered partitions of the digits of n
    p = reduce(vcat, partitions(["$n"...]))

    # Filter to groups of size x-y
    g = filter(k -> endof(k) == x - y, p)

    # Get the maximum resulting number
    return maximum(i -> parse(join(i)), g)
end
\$\endgroup\$
1
\$\begingroup\$

Haskell, 64 bytes

import Data.List
y#x=maximum.filter((==x-y).length).subsequences

Usage example: (4#7)"1789823" -> "983".

The original number n is takes as a string. (Not sure if I'm overstressing the "no strict input format" rule, but string input was required(!) in the first version).

How it works: make a list of all subsequences of n, keep those with length x-y and pick the maximum.

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

Ruby, 40 bytes

->y,n,x{n.chars.combination(x-y).max*''}

This is an anonymous function that takes y and x as integers and n as a string, and returns a string. You may call it for instance like this

->y,n,x{n.chars.combination(x-y).max*''}[2,"5263",4]

and it will return "63".

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

MATLAB 40 bytes

@(n,y)max(str2num(nchoosek(n,nnz(n)-y)))

Test:

ans('76751432',3)
ans = 77543
\$\endgroup\$
0
\$\begingroup\$

Pyth, 45 bytes

=cz)Fk.P@z1-l@z1v@z0I:@z1j".*"k1I>vkZ=Zvk)))Z

try it here

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

JavaScript (ES6), 78

A recursive function with 2 arguments y and d. y can be numeric or string, d must be a string.

r=(y,d)=>y?Math.max(...[...d].map((x,i)=>r(y-1,d.slice(0,i)+d.slice(i+1)))):+d

Before the challenge changed it was 107 - ... with all input/output oddities ...

x=>(r=(n,d)=>n?Math.max(...[...d].map((x,i)=> r(n-1,d.slice(0,i)+d.slice(i+1)))):+d)(...x.match(/\d+/g))+'\n'

Test

r=(y,d)=>y?Math.max(...[...d].map((x,i)=>r(y-1,d.slice(0,i)+d.slice(i+1)))):+d

function test() {
  [a,b]=I.value.match(/\d+/g)
  O.textContent=r(a,b)
}

test()
y,n: <input id=I value='4 1789823' oninput="test()">
<pre id=O></pre>

\$\endgroup\$
  • \$\begingroup\$ Typo: n-1 should be y-1. \$\endgroup\$ – Neil Jan 13 '16 at 16:18

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.