views:

586

answers:

7

Given an IP address and mask like 192.168.10.30/24 on STDIN, return the matching range of addresses.

i.e. input : 192.168.10.30/24 output: 192.168.10.0-192.168.10.255

input : 127.0.0.0/31 output: 127.0.0.0-127.0.0.1

libraries/packages/... are NOT allowed

+3  A: 

Ruby 1.8.7 - 166 162 142 characters

now works on 1.8.6 also

def s x;[24,16,8,0].map{|i|255&x>>i}*"."end
l=gets.split /\D/
r=1<<32-l.pop.to_i
i=0;l.map{|x|i=(i<<8)+x.to_i}
puts"#{s r*i/=r}-#{s r*i+r-1}"
sepp2k
How about `puts(s r*i/=r)+?-+(s r*i+r-1)`
Adam
@Adam: That would execute `puts(s r*i/=r)` and then try to add `?-` to the result of `puts` (i.e. `nil`). You'd need another pair of parens. Also in ruby 1.8 `?-` is an Integer and you can't add an integer to a string.
sepp2k
However, you could save 1 char with `puts"#{s i*r}-"+s(r*i+r-1)`
AShelly
+2  A: 

C - 176 characters

updated due to gwell's suggestions:

f(unsigned v,int c){v>>8?f(v>>8,46):0;printf("%d%c",v&255,c);}main(){int a,b,c,d,e;scanf("%d.%d.%d.%d/%d",&a,&b,&c,&d,&e);a=a<<24|b<<16|c<<8|d;b=~0<<32-e;f(a&b,45);f(a|~b,10);}    
brone
The `typedef` takes more characters than it saves. Save 8 characters by removing it. Recursion for the print function can save 18 characters: `f(int v,int c){if(v>256)f(v>>8,46);printf("%d%c",v}`
gwell
Bah... you're right. It WAS 2 chars shorter when I first put it in, promise!
brone
Added recursion, hopefully most concisely. `v` has to be unsigned now, in case >> does arithmetic shift.
brone
+2  A: 

J, 97 110 characters

' .'charsub deb({.,'-',{:)":(4$256)#:#.(({:a){.(32$2)#:256#.}:a),"1]0 1$"0~{:32-a=:".'. /'charsub

Usage:

    ' .'charsub deb({.,'-',{:)":(4$256)#:#.(({:a){.(32$2)#:256#.}:a),"1]0 1$"0~{:32-a=:".'. /'charsub'127.0.0.0/31'
127.0.0.0-127.0.0.1

    ' .'charsub deb({.,'-',{:)":(4$256)#:#.(({:a){.(32$2)#:256#.}:a),"1]0 1$"0~{:32-a=:".'. /'charsub'192.168.10.30/24'
192.168.10.0-192.168.10.255
David
+2  A: 

Python, 190 175 172 169 165 chars

l,m=raw_input().split('/')
m=2**(32-int(m))-1
i=0
for k in l.split('.'):i=256*i+int(k)
i&=~m
print'-'.join('.'.join(str(k>>j&255)for j in(24,16,8,0))for k in(i,i|m))
Nas Banov
M42
@M42: well uh what version of python are you using? the code above is for 2.x (tested on 2.5). If you are using Python 3, some changes are in order, say use `input` instead of `raw_input` and `print` is a function and not statement, so add parenthesis `print('-'.join(....))`. I figure that will decrease total length by 2 chars
Nas Banov
itried it with python 2.3.4; i'm just trying on another machine, it's ok with python 2.4.3. Sorry.
M42
A: 

Lua, 181 165 characters

k=256 w=io.write
i=io.read():gmatch("%d+")a=((i()*k+i())*k+i())*k+i()c=2^(32-i())a=a-a%c
function p(n)if n>k then p((n-n%k)/k)w"."end w(n%k)end
p(a)w"-"p(a+c-1)w"\n"
gwell
+1  A: 

Ruby 1.9, 135 134 characters

r=->i{[i].pack(?N).unpack('C*')*?.}
n=gets.split(/\D/).map &:to_i
b=(1<<32-n.pop)-1
h=n.pack('C*').unpack(?N)[0]|b
puts r[h^b]+?-+r[h]
Lowjacker
A: 

Perl 134 chars

linefeeds for "readability" may be omitted

sub _{join'.',unpack'C4',pack'B*',@_}
split '[./]',<>;
$m=1x($n=pop@_).'0'x(32-$n);
$b=$m&unpack'B*',pack'C4',@_;
print _($b),'-',_$b|=~$m
M42