Skip to content

Commit 028236e

Browse files
author
Ronghai Wei
committed
update ruby version code
1 parent e5bdd2b commit 028236e

8 files changed

+98
-25
lines changed

ebook/code/ruby/chapter04.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def o_index(sub)
2121
#Ruby 1.9 maintains hash order
2222
#For Ruby 1.8 find orderedhash http://rubygems.org/gems/orderedhash
2323
#find first letter who's count is count
24+
#This supports UTF-8
2425
def o_first_count_letter(count = 1)
2526
hash = Hash.new(0)
2627
each_char{|c|

ebook/code/ruby/chapter05.rb ebook/code/ruby/chapter05.findSum.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def find_sum_with_two(array, sum)
4949
print "find_03 ", array.find_03(sum), "\n"
5050

5151
Benchmark.bm(10) do |x|
52-
x.report("find_01:") { 10000.times{ array.find_01(sum) } }
53-
x.report("find_02:") { 10000.times{ array.find_02(sum) } }
54-
x.report("find_03:") { 10000.times{ array.find_03(sum) } }
52+
x.report("find_01:") { 10000.times{ array.find_01(sum) } }
53+
x.report("find_02:") { 10000.times{ array.find_02(sum) } }
54+
x.report("find_03:") { 10000.times{ array.find_03(sum) } }
5555
end
5656
end
5757

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
#!/usr/bin/env ruby
22
# -*- coding: UTF-8 -*-
33

4-
5-
6-
7-
if __FILE__ == $0
8-
max = 5000000
9-
half = (max**0.5).to_int
4+
def amicable_number max
5+
half = max / 2
106
sum = Array.new(max){1}
117
(2...half).each do|i|
128
(i + i).step(max + 1, i)do|j|
13-
sum[j] += i unless sum[j].nil?
9+
sum[j] += i unless sum[j].nil?
1410
end
1511
end
16-
17-
18-
12+
ans = []
1913
sum.each_with_index do | s, i|
20-
p "%d %d" % [i, s] if s > i && s <= max && sum[s] == i
21-
end
14+
ans.push([i, s]) if s > i && s <= max && sum[s] == i
15+
end
16+
ans
2217
end
2318

19+
20+
21+
if __FILE__ == $0
22+
amicable_number( 500000 ).each{|e|
23+
p "%d %d" % e
24+
}
25+
end

ebook/code/ruby/chapter07.findMaxSum.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ def max_sum(arr)
1414
[max, max_sub]
1515
end
1616

17-
def max(*ar)
18-
return ar.max
19-
end
20-
21-
def max_sum_2(arr)
17+
def max_sum_dp(arr)
2218
sum = Array.new(arr.length){0}
2319
sum[0] = arr[0]
2420
1.upto(arr.length - 1){|i|
25-
sum[i] = max( arr[i], sum[i-1] + arr[i])
21+
sum[i] = [ arr[i], sum[i-1] + arr[i]].max
2622
}
2723
sum.max
2824
end
2925

3026

3127
if __FILE__ == $0
32-
p max_sum([-1,-2,-3,-4])
33-
p max_sum([1, -2, 3, 10, -4, 7, 2, -5])
3428

35-
p max_sum_2([-1,-2,-3,-4])
36-
p max_sum_2([1, -2, 3, 10, -4, 7, 2, -5])
29+
[ [-1,-2,-3,-4] , [1, -2, 3, 10, -4, 7, 2, -5] ] .each{ |ar|
30+
p ar, max_sum(ar), max_sum_dp(ar)
31+
}
32+
3733
end
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: UTF-8 -*-
3+
def permutation_recursive(ar)
4+
return [ar] if ar.length == 1
5+
permutations = []
6+
ar.each_with_index{|a, i|
7+
arc = ar.clone; arc.delete_at(i)
8+
subpermutations = permutation_recursive(arc)
9+
subpermutations.each{|x|
10+
permutations.push( [a] + x)
11+
}
12+
}
13+
14+
return permutations
15+
end
16+
17+
18+
if __FILE__ == $0
19+
test_cases = [["A", "B", "C"]]
20+
test_cases.each{|tc|
21+
permutations = permutation_recursive(tc)
22+
p "permutation", permutations, permutations.sort == tc.permutation(tc.length).sort
23+
}
24+
25+
26+
27+
end

ebook/code/ruby/chapter17.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: UTF-8 -*-
3+
4+
def fibonacci_solution n
5+
return [0, 1, 2][n] if( n <= 2 )
6+
fibonacci_solution(n - 1) + fibonacci_solution( n - 2)
7+
end
8+
9+
if __FILE__ == $0
10+
steps = [1, 2, 3, 4, 5, 6]
11+
p steps.map{|x|
12+
[x, fibonacci_solution(x)]
13+
}
14+
15+
end

ebook/code/ruby/chapter18.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: UTF-8 -*-
3+
4+
def odd_even(arr)
5+
return arr.select{|x| x % 2 != 0 } + arr.select{|x| x % 2 == 0 }
6+
end
7+
8+
def reorder_odd_even(arr)
9+
si, ei = 0, arr.length - 1
10+
while si < ei
11+
if(arr[si].odd?)
12+
si += 1; next
13+
end
14+
if(arr[ei].even?)
15+
ei -= 1; next
16+
end
17+
arr[si], arr[ei] = arr[ei], arr[si]
18+
end
19+
arr
20+
end
21+
22+
if __FILE__ == $0
23+
test_cases = [ [2, 1, 3, 4, 6, 5, 7] ]
24+
test_cases.each{|tc|
25+
p odd_even(tc), reorder_odd_even(tc)
26+
}
27+
28+
end

ebook/code/ruby/chapter19.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: UTF-8 -*-
3+
# see 'chapter04.rb'
4+
# String.o_first_count_letter

0 commit comments

Comments
 (0)