6
\$\begingroup\$

Write a program in the shortest number of bytes possible that will parse any string given to it by input, and output that string with any and all numbers padded with leading zeroes to match the largest number's length.

For example:

Input:

This 104 is an -8 example of 4.518 a string 50.

The generated output should become:

This 104 is an -008 example of 004.518 a string 050.

Note that any digits after the period are not considered part of the "length" of a number. Numbers are considered any sequence of digits with either 0 or 1 periods in the sequence. Numbers will be delimited with either the string boundary, spaces, commas, or newlines. They can also be followed by a period, but only if the period is then followed by a delimiting character. They can also be preceded with a '-' to indicated negatives. So something like this:

The strings 20.d, 5.3ft and &450^ are not numbers, but 450.2 is.

Should output the following:

The strings 20.d, 5.3ft and &450^ are not numbers, but 450.2 is.

That is to say, no modifications.

String input will be no more than 200 characters, if your program has an upper bound for some reason.

The winning answer will be the answer in the shortest number of bytes in seven days from the posting of this question.

Test cases

Input:

2 40 2

Output:

02 40 02

Explanation: both substrings 2 are bounded on one side by a string boundary and on the other side by .


Input:

E.g. 2,2.,2.2,.2,.2., 2 2. 2.2 .2 .2. 2d 2.d 2.2d .2d .2.d 40

Output:

E.g. 02,02.,02.2,00.2,00.2., 02 02. 02.2 00.2 00.2. 2d 2.d 2.2d .2d .2.d 40

Explanation: in the first two groups the first four numbers are followed by a delimiter (, or ) and the final one is followed by a period then a delimiter; in the third group, each sequence is followed by the non-delimiter d.

\$\endgroup\$
25
  • \$\begingroup\$ @MartinBüttner yeah, that's a bit confusing, but basically it means that in "253.47", " 253" isn't a number even though it's followed by a period - it's part of a number. Will reword it when I'm back home. \$\endgroup\$
    – Sellyme
    Commented Aug 19, 2014 at 7:58
  • \$\begingroup\$ @JanDvorak I feel like doing so would kind of defeat the point of the challenge - that's an exercise left to the reader. Happy to provide more examples and clarification though. \$\endgroup\$
    – Sellyme
    Commented Aug 19, 2014 at 7:59
  • \$\begingroup\$ so, there's no number in 257.24ft? Now that's interesting... \$\endgroup\$ Commented Aug 19, 2014 at 8:00
  • \$\begingroup\$ @SebastianLamerichs note that 253.47 is not a sequence of digits... \$\endgroup\$ Commented Aug 19, 2014 at 8:03
  • \$\begingroup\$ @MartinBüttner I padded "4.518", not "4". Functionally they're the same. Basically, don't treat "4.518" as two separate numbers, despite them being delimited by a period. \$\endgroup\$
    – Sellyme
    Commented Aug 19, 2014 at 8:04

3 Answers 3

4
\$\begingroup\$

Ruby, 111 bytes

I hope this catches all the details of the spec

puts $*[0].gsub(r=/(?<![^,\s])(-?)(\d+)(?=\.?\d*(?![^,\s]))/){$1+$2.rjust(i.scan(r).map{|m|m[1].size}.max,'0')}

It reads the input as the first command line argument and prints the result to STDOUT.

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

Cobra - 282

class P
    def main
        r,s,l,n=RegularExpressions.Regex(r'^[\n,]?-?(\d+).?\d*[\n,]?$'),Console.readLine.split(' '),0,0
        for m in 2,for y,x in s.numbered,if r.isMatch(x),while (n-=n-r.match(x).groups[1].length)<if(n>l,l-=l-n,l),x=s[y]=if(x[0]=='-','-0'+x[1:],'0'+x)
        print s.join(' ')

For those of you wishing to test this without installing Cobra, it's rather easily converted into equivalent C# code.

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

JavaScript, 201 chars

s=prompt();j=k=l=0,t={}
for(i in s){s[i]==+s[i]&&s[i]!=" "?j++:(t[i-j]=j,j=0);if(j>k)k=j;}if(j!=0)t[+i+1-j]=j
for(i in s){if(t[i])while(k-t[i]){s=s.slice(0,+i+l)+"0"+s.slice(+i+l);t[i]++;l++;}}alert(s)
\$\endgroup\$
7
  • \$\begingroup\$ I'm confused as to how input is meant to be handled here - without spaces (comma delimiting, for example) it seems to just print "0", and with spaces I can't even work out what it's doing (I got an output of "7" for "45 200" but am somehow unable to replicate that). \$\endgroup\$
    – Sellyme
    Commented Aug 19, 2014 at 10:33
  • \$\begingroup\$ @SebastianLamerichs Guess I have to print the output in the program, forgot that. \$\endgroup\$ Commented Aug 19, 2014 at 10:40
  • \$\begingroup\$ @lazertyuiopl Yeah, there has to be output in some form, I was getting tripped up by JavaScript's insistence on returning a value even when not explicitly told to. Not sure where that 7 came from, though. I can confirm that it works fine for comma-delimited input now. \$\endgroup\$
    – Sellyme
    Commented Aug 19, 2014 at 10:43
  • \$\begingroup\$ Are you sure it works? If I input "2 40 2", I get "02 40 2", not "02 40 02". \$\endgroup\$
    – Ingo Bürk
    Commented Aug 22, 2014 at 15:15
  • \$\begingroup\$ @IngoBürk I just inputted 2 40 2 and got 02 40 02 using Chrome 36 -- test here \$\endgroup\$ Commented Aug 23, 2014 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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