Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
    
@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. –  Sebastian Lamerichs Aug 19 at 7:58
    
@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. –  Sebastian Lamerichs Aug 19 at 7:59
    
so, there's no number in 257.24ft? Now that's interesting... –  Jan Dvorak Aug 19 at 8:00
    
@SebastianLamerichs note that 253.47 is not a sequence of digits... –  Jan Dvorak Aug 19 at 8:03
    
@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. –  Sebastian Lamerichs Aug 19 at 8:04

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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)
share|improve this answer
    
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). –  Sebastian Lamerichs Aug 19 at 10:33
    
@SebastianLamerichs Guess I have to print the output in the program, forgot that. –  IazertyuiopI Aug 19 at 10:40
    
@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. –  Sebastian Lamerichs Aug 19 at 10:43
    
Are you sure it works? If I input "2 40 2", I get "02 40 2", not "02 40 02". –  Ingo Bürk Aug 22 at 15:15
    
@IngoBürk I just inputted 2 40 2 and got 02 40 02 using Chrome 36 -- test here –  IazertyuiopI Aug 23 at 4:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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