How would you write it vs Smalltalk

  • Thread starter SpringFlowers AutumnMoon
  • Start date
S

SpringFlowers AutumnMoon

i found a description of Smalltalk in

http://www.engin.umd.umich.edu/CIS/course.des/cis400/smalltalk/freq.html

and then I tried it in Ruby:


h = Hash.new(0)

print "Enter line: "

gets.downcase.each_byte {|b| c = "%c" % b; h[c] += 1 if c =~ /[a-z]/}

p h.sort


C:\rails\depot>ruby count_alpha2.rb
Enter line: This is a test of the line count
[["a", 1], ["c", 1], ["e", 3], ["f", 1], ["h", 2], ["i", 3], ["l", 1],
["n", 2],
["o", 2], ["s", 3], ["t", 5], ["u", 1]]

I just wonder how would you write it? There is no each_char it seems,
unless with ActiveSupport.
 
G

gabriele renzi

I just wonder how would you write it? There is no each_char it seems,
unless with ActiveSupport.

running away but a quick idea: you could use Integer#chr to get a char
(well, a one-byte-string) from an integer.
And, since you're dwelling with bytes anyway, you could just do a test
like
if c =~ ?a..?z
(?a being the literal to get the integer value of a character)

So something like:
gets.downcase.each_byte {|b| h[c.chr] += 1 if c =~ ?a..?z}
 
R

Rick DeNatale

So something like:
gets.downcase.each_byte {|b| h[c.chr] += 1 if c =~ ?a..?z}

Here's another way:

puts "Enter line:"
h = Hash.new(0)
gets.scan(/[a-zA-Z]/) {|ch| h[ch.downcase] += 1 }
p h.sort
 
S

Simon Kröger

Rick said:
So something like:
gets.downcase.each_byte {|b| h[c.chr] += 1 if c =~ ?a..?z}

Here's another way:

puts "Enter line:"
h = Hash.new(0)
gets.scan(/[a-zA-Z]/) {|ch| h[ch.downcase] += 1 }
p h.sort

Well, we need an inject solution, right?

p gets.downcase.scan(/[a-z]/).inject({}){|h,c| h.merge({c,h[c].to_i+1})}.sort

cheers

Simon
 
7

7stud --

Simon said:
Rick said:
Here's another way:

puts "Enter line:"
h = Hash.new(0)
gets.scan(/[a-zA-Z]/) {|ch| h[ch.downcase] += 1 }
p h.sort

Well, we need an inject solution, right?

p gets.downcase.scan(/[a-z]/).inject({}){|h,c|
h.merge({c,h[c].to_i+1})}.sort

How about:

p gets.downcase.scan(/[a-z]/).inject(Hash.new(0)){|h, c| h[c]+= 1;
h}.sort
 
7

7stud --

And this seems conceptually easier and it's slightly faster(if I
benchmarked it correctly):

input = gets
h = Hash.new(0)

input.scan(/[a-z]/){|char| h[char] += 1}
p h.sort


============

require "benchmark"

input = "123Hello+#"

h = Hash.new(0)
Benchmark.bm(20) do |br|
br.report("scan with block:") do
200_000.times do
input.downcase.scan(/[a-z]/){|char| h[char] += 1}
end
end
end

p h.sort
puts

h = Hash.new(0)
Benchmark.bm(20) do |br|
br.report("scan with inject:") do
200_000.times do
input.downcase.scan(/[a-z]/).inject(h) do |h, c|
h[c] += 1
h
end
end
end
end

p h.sort

--output:--

user system total real
scan with block: 2.690000 0.010000 2.700000 ( 2.714077)
[["e", 200000], ["h", 200000], ["l", 400000], ["o", 200000]]

user system total real
scan with inject: 2.830000 0.020000 2.850000 ( 2.856019)
[["e", 200000], ["h", 200000], ["l", 400000], ["o", 200000]]
 
7

7stud --

7stud said:
And this seems conceptually easier and it's slightly faster(if I
benchmarked it correctly):

input = gets
h = Hash.new(0)

input.scan(/[a-z]/){|char| h[char] += 1}
p h.sort


Whoops. I didn't downcase the input in that example, but I did downcase
the input in the benchmark tests. In any case, apologies to Rick
Denatale who already posted that solution.
 
B

Brian Adkins

So something like:
gets.downcase.each_byte {|b| h[c.chr] += 1 if c =~ ?a..?z}

Here's another way:

puts "Enter line:"
h = Hash.new(0)
gets.scan(/[a-zA-Z]/) {|ch| h[ch.downcase] += 1 }
p h.sort

More verbose but a teensy bit quicker by avoiding regex & downcase:

lambda do |str|
h = Hash.new(0)
str.each_byte do |b|
if (b > 96 && b < 123)
h[b.chr] += 1
elsif (b > 64 && b < 91)
h[(b+32).chr] += 1
end
end
h
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top