15
\$\begingroup\$

A fairly simple challenge: You will receive two inputs, a string and a number (the number may be taken as a string, ie "123" instead of 123)

If the string does not end in a number (ie, it does not match the regex \d$), just append the number to the end of the string.

If the string does end in a number (ie, it matches the regex \d+$), you should first delete that and then append the number.

Neither of the inputs will ever be invalid or empty (invalid is defined by the numerical input not containing only digits)

The number will never contain a - or a ..

The string will never contain a newline, or unprintable non-whitespace characters.

Test Cases:

abc123 + 345 -> abc345
123 + 1 -> 1
hello + 33 -> hello33
123abc123 + 0 -> 123abc0
ab3d5 + 55 -> ab3d55
onetwo3 + 3 -> onetwo3
99ninenine + 9999 -> 99ninenine9999
\$\endgroup\$

24 Answers 24

10
\$\begingroup\$

Retina, 5 bytes

\d*¶

Takes two strings as input, separated by a newline.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ ninja'd. Though I'm not sure if the space is a good separator choice. \$\endgroup\$ – John Dvorak Jun 6 '17 at 6:40
  • \$\begingroup\$ Fair enough, changed to a tabulator. \$\endgroup\$ – Dennis Jun 6 '17 at 6:48
  • \$\begingroup\$ How about a semicolon? That one doesn't need to be escaped either. \$\endgroup\$ – John Dvorak Jun 6 '17 at 6:49
  • \$\begingroup\$ Oh, I just read the OP's clarification. Newline it is. \$\endgroup\$ – Dennis Jun 6 '17 at 6:50
7
\$\begingroup\$

Python 2, 30 bytes

lambda a,b:a.rstrip(`56**7`)+b

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice trick to create a number with all the digits! \$\endgroup\$ – TheLethalCoder Jun 6 '17 at 12:02
  • \$\begingroup\$ Not sure what is going on, but for me (v2.7.11 on Windows), this fails when a ends with "L" because 56**7 evaluates to 1727094849536L. Input of a="abcdL"; b="59" outputs "abcd59". Your TIO link does not evaluate 56**7 to a long so I don't know what is happening \$\endgroup\$ – wnnmaw Jun 6 '17 at 16:49
6
\$\begingroup\$

PHP, 37 36 35 33 bytes

<?=chop($argv[1],3**39),$argv[2];

Saved 1 byte thanks to Jörg Hülsermann.

\$\endgroup\$
  • 1
    \$\begingroup\$ chop as alias saves 1 Bytes \$\endgroup\$ – Jörg Hülsermann Jun 6 '17 at 8:56
5
\$\begingroup\$

Perl 5, 12 bytes

11 bytes code + 1 for -p.

s/\d*$/<>/e

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Hehe, came up with exactly the same code! Glab to see you back around :) \$\endgroup\$ – Dada Jun 7 '17 at 11:22
5
\$\begingroup\$

Java 8, 32 bytes

a->b->a.replaceAll("\\d*$","")+b

Takes input a as a String, and for b it doesn't matter whether it's a String or integer (although I use Integer in the TIO-link below).

Try it here.

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

Python 2, 32 bytes

import re
re.compile("\d*$").sub

Try it online!

Takes the inputs in reverse order, both as string.

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

05AB1E, 9 bytes

DvTFNÜ}}«

Try it online! Probably a bad solution but it's the best I could come up with

Explanation

DvTFNÜ}}«
Dv     }  # For each character in input1
  TF  }   # 10 times
    NÜ    # Get 0-9 and trim it from input1
        « # Concatenate with input2
\$\endgroup\$
  • \$\begingroup\$ Nevermind, I was wrong. \$\endgroup\$ – Magic Octopus Urn Jun 6 '17 at 13:45
4
\$\begingroup\$

Japt, 10 bytes

r"%d*$" +V

Try it online!

 r"%d*$" +V
Ur"%d*$" +V # Implicit input (U and V)
 r          # Remove
  "%d*$"    #   any trailing digits
U           #   from the first input
         +V # And append the second input
            # Implicit output
\$\endgroup\$
  • \$\begingroup\$ Doesn't work if U doesn't end in a number. Try using * in the RegEx, instead of +. TIO \$\endgroup\$ – Shaggy Jun 6 '17 at 8:11
  • \$\begingroup\$ Now it doesn't work if U does end in a number. I think you'll have to do r"%d+$" +V \$\endgroup\$ – ETHproductions Jun 6 '17 at 15:54
  • \$\begingroup\$ Yeah, I just realised that. Should be fixed now \$\endgroup\$ – Luke Jun 6 '17 at 16:11
4
\$\begingroup\$

Python 2, 44 41 39 bytes

EDIT: -4 bytes thanks to @Dom Hastings. I don't use regular expressions much.

EDIT 2: -2 bytes thanks to @totallyhuman pointing out that the number could be taken as a string

Had to be expected...

lambda x,y:re.sub("\d*$",y,x)
import re

Just removes the digits at the end of the string and appends the number

Try it online!

\$\endgroup\$
  • \$\begingroup\$ If you replace your regex with \d*$, can you replace the "" with `` y `` to save some bytes? \$\endgroup\$ – Dom Hastings Jun 6 '17 at 8:04
  • \$\begingroup\$ @DomHastings: Nice! I don't use regular expressions much so thanks! \$\endgroup\$ – Neil A. Jun 6 '17 at 8:06
  • 1
    \$\begingroup\$ You can take the y parameter as a string too. \$\endgroup\$ – totallyhuman Jun 6 '17 at 11:11
  • \$\begingroup\$ @totallyhuman: Glazed over that detail. Thanks! \$\endgroup\$ – Neil A. Jun 6 '17 at 18:55
4
\$\begingroup\$

Pip, 9 7 bytes

a<|XD.b

@DLosc saved me 2 bytes!

Try it online!

Explanation

a<|         Strip all matches off the end of 'a' (the first cmd line arg)
   XD         of the pattern \d (ordinarily, a regex would be entered as '\d', but digits 
              have a pre-existing constant XD)
     .b     Then concatenate 'b' (the second cmd line arg)
            PIP implicitly prints the results of the last operation.
\$\endgroup\$
3
\$\begingroup\$

Jelly, 5 bytes

œrØD;

Try it online!

How it works

œrØD;  Main link. Left argument: s. Right argument: t

  ØD   Digits; yield "0123456789".
œr     Trim these characters from the right of s.
    ;  Append t.
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 28 26 25 bytes

x=>y=>x.replace(/\d*$/,y)
  • 1 byte saved thanks to Neil reminding me why I shouldn't golf early in the morning!
\$\endgroup\$
  • 1
    \$\begingroup\$ Is the ? required? \$\endgroup\$ – Neil Jun 6 '17 at 7:48
3
\$\begingroup\$

Ruby, 21 bytes

->a,b{a=~/\d*$/;$`+b}

Takes two strings in input.

Try it online!

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

C#, 45 bytes

s=>n=>s.TrimEnd("0123456789".ToCharArray())+n

Explanation:

s=>n=>                                        // Take input
      s.TrimEnd(                              // Trim the end of the string with the specified chars
                "0123456789"                  // Create a string of the digits
                            .ToCharArray())   // Turn the string into a char array
                                           +n // Append the integer onto the end
\$\endgroup\$
3
\$\begingroup\$

V, 7 4 bytes

óä*î

Try it online!

This uses the same regex as the Retina answer above:

s:/\d*\n//
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 20 bytes

->$_,\n{S/\d*$/{n}/}
\$\endgroup\$
2
\$\begingroup\$

Tcl 32 bytes

proc s a\ b {regsub \\d*$ $a $b}

I'm not sure about the expected interface. This is done as a procedure that accepts the two inputs as call arguments. To turn it to a standalone script that reads input from stdin and outputs the result to stdout, one would need the extra line:

puts [s [gets stdin] [gets stdin]]

or would do it all "inline":

puts [regsub \\d*$ [gets stdin] [gets stdin]]

regsub takes an RE, the original string and a string to replace the matching part with.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 48 bytes

There is already a Mathematica solution (84 bytes).

StringDrop[StringTrim["."<>#,_?DigitQ..]<>#2,1]&
\$\endgroup\$
2
\$\begingroup\$

Carrot, 16 21 bytes

$^//^.*?(?=\d*$)/S0^#

Try it online! (input is linefeed separated)

Explanation

$^                Set the stack-string to be equal to the first line in the input
/                 Set the stack-array to be equal to the matches of this regex:
 /^.*?(?=\d*$)/   The beginning of the string followed by non-digit characters at the end that are not included in the match.
S0                Convert to a string with 0 as the delimiter
^#                Append the rest of the input to the stack-string

I had to increase the bytecount by 5 because the code did not work for testcases like a5b3 with multiple digits.

\$\endgroup\$
2
\$\begingroup\$

Haskell, 75 bytes 95 bytes 91 79 61 bytes

b=(`notElem`['0'..'9'])
r=reverse
d#e=r(snd$break b$r d)++e

I tried doing this without regex so maybe that would be a dramatically improved answer. Also there are a couple ways I could go about this so I am unsure if I can shave a few bytes with a different approach.

UPDATE: I went up in bytes because I realized I was failing the test case where numbers exist in the string that are not the suffix. Now I am sure that regex would provide a much better answer.

UPDATE2: After some great feedback, more bytes were golfed!

\$\endgroup\$
  • 2
    \$\begingroup\$ b=(`elem`['0'..'9']) is shorter than isDigit+ import. dropWhile can be replaced with snd.span, i.e. =r(snd$span b$r d)++e. If you reverse the test b=(`notElem`...) you can go with d#e|b$last$d=d++e|2>1=r(snd$break b$r d)++e. \$\endgroup\$ – nimi Jun 6 '17 at 17:55
  • \$\begingroup\$ @nimi Thanks for the suggestions! I keep forgetting about span and break and how useful those can be. \$\endgroup\$ – maple_shaft Jun 7 '17 at 11:53
  • 1
    \$\begingroup\$ Can't the |b$last$d=d++e|2>1 part be simply dropped? All the test cases seem to work fine. Try it online! \$\endgroup\$ – Laikoni Jun 7 '17 at 15:42
  • \$\begingroup\$ @Laikoni Great idea! you just golfed me 18 bytes! \$\endgroup\$ – maple_shaft Jun 7 '17 at 16:53
  • 2
    \$\begingroup\$ Don't be! Learning new tricks and things about Haskell I didn't know before are some of my favourite parts of golfing. \$\endgroup\$ – Laikoni Jun 7 '17 at 19:20
1
\$\begingroup\$

Mathematica, 84 bytes

(k=#;T=ToCharacterCode;L=T@k;While[47<Last@L<58,w=k~StringDrop~-1;k=w;L=T@w];w<>#2)&

input 2 strings

["ab3d5", "55"]

output

ab3d55

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

C (gcc), 99 98 96 95 bytes

Should be able to golf this down a bit...

main(c,v)char**v;{for(c=strlen(v[1]);~c*isdigit(v[1][--c]);v[1][c]=0);printf(v[1]),puts(v[2]);}

Try it online!

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

Noether, 11 bytes

I"\d+$"-I+P

Try it here!

When inputting a string, enclose it in quotation marks

Explanation:

I       - Push input to stack
"\d+$"  - Push string to stack
-       - Remove characters from first string which match the regex
I       - Push input to stack
+       - Append to the first string
P       - Print top item of stack
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 8 bytes

DþRvyÜ}«

Try it online!

Explanation:

DþRvyÜ}«
D        Duplicate input.
 þ       Get the digits of the input.
  R      Reverse the digits of the input.
   v  }  For each of the digits in the input:
    y        Push the current digit
     Ü       Trim that from the right of the input.
       « Concatenate to 2nd input.
\$\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.