Skip to main content
Commonmark migration
Source Link

#Ruby, 247

Ruby, 247

Straightforward approach looping through all bytes of the input and all bits in each byte, summing to variable d.

d is initialized to -2 because h contains the terminating newline from the input (worth 2 bits) and we do not want to count that.

Similarly h will contain test with a trailing newline, so a newline must included in the comparison value.

d=-2
h=gets
h.bytes{|a|8.times{|b|d+=a>>b&1}}
p h=='test
'?0:d

#Ruby, 247

Straightforward approach looping through all bytes of the input and all bits in each byte, summing to variable d.

d is initialized to -2 because h contains the terminating newline from the input (worth 2 bits) and we do not want to count that.

Similarly h will contain test with a trailing newline, so a newline must included in the comparison value.

d=-2
h=gets
h.bytes{|a|8.times{|b|d+=a>>b&1}}
p h=='test
'?0:d

Ruby, 247

Straightforward approach looping through all bytes of the input and all bits in each byte, summing to variable d.

d is initialized to -2 because h contains the terminating newline from the input (worth 2 bits) and we do not want to count that.

Similarly h will contain test with a trailing newline, so a newline must included in the comparison value.

d=-2
h=gets
h.bytes{|a|8.times{|b|d+=a>>b&1}}
p h=='test
'?0:d
Source Link
Level River St
  • 26.7k
  • 4
  • 37
  • 105

#Ruby, 247

Straightforward approach looping through all bytes of the input and all bits in each byte, summing to variable d.

d is initialized to -2 because h contains the terminating newline from the input (worth 2 bits) and we do not want to count that.

Similarly h will contain test with a trailing newline, so a newline must included in the comparison value.

d=-2
h=gets
h.bytes{|a|8.times{|b|d+=a>>b&1}}
p h=='test
'?0:d