22
2
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 code-golf, 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.
2
Please allow more general input and output, requiring a specific string format is usually a bad idea.
– xnor – 2016-01-13T01:07:05.453@xnor But some answers have already been posted...? – Luis Mendo – 2016-01-13T01:09:36.933
That's tricky, edits after answers are usually frowned upon. Here, though, I think the input processing is separate enough that answers could change it easily. – xnor – 2016-01-13T01:16:08.483
1@LuisMendo I wouldn't mind editing the I/O in mine. ¯\(ツ)/¯ – Alex A. – 2016-01-13T01:16:40.390
Ok, I too will change my answer (tomorrow) if input format is made more flexible. I suggest that at least the current format should continue to be allowed – Luis Mendo – 2016-01-13T01:21:37.293
Just to check, in other words, the problem is to find the size (x-y) substring of n with the greatest numeric value? – Michael Klein – 2016-01-13T02:04:14.433
4-1 because of the strict I/O requirements, +1 for an interesting challenge. Overall, a solid sidevote. – Mego – 2016-01-13T04:39:34.290
1The input format is too strict as others have said, especially considering that
x
is kind of a useless information. – Fatalize – 2016-01-13T08:10:08.890Sorry me for that, I didn't know it would impact so much on the answers... I've updated my question – removed – 2016-01-13T08:27:49.463
1@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.) – Alex A. – 2016-01-13T16:58:38.613@WashingtonGuedes No problem. It's your first question and I think overall you've done a splendid job. :) – Alex A. – 2016-01-13T16:59:14.533