diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..a637b8c 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello #{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..07e89c1 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -7,7 +7,7 @@ puts "b 是 #{b}" # ... +a, b = b, a puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..0e9dc57 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -7,5 +7,6 @@ b = gets # ..... +s = (a.to_f * b.to_f) / 2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: ____#{s}_____" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..740a16b 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -7,6 +7,8 @@ people = gets # ..... +a = pizzas.to_i / people.to_i +b = pizzas.to_i % people.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: _____#{a}____ 片" +puts "还剩下几片: ____#{b}_____ 片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..2f0972c 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -6,12 +6,22 @@ print "请输入您的体重(公斤),然后按 Enter: " weight = gets +weight = weight.to_f print "请输入您的身高(厘米),然后按 Enter: " height = gets - +height = height.to_f # ..... +bmi = weight / ( height ** 2 ) + +if bmi < 18.5 + result = "过轻" +elsif bmi >= 24 + result = "过重" +else + result = "正常" +end -puts "您的 BMI 是: _________" +puts "您的 BMI 是: ____#{bmi}_____" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 结果是: _____#{result}____(过轻或正常或过重)" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..ae804fc 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -3,8 +3,18 @@ print "请输入一个整数,然后按 Enter: " x = gets - +x = x.to_i # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x == 0 + result1 = "零" +elsif x > 0 + result1 = "正数" +else + result1 = "负数" +end + +result2 = (x % 2 == 0) ? "偶数" : "奇数" + +puts "这个数是___#{result1}__ (正数或零或负数)" +puts "这个数是__#{result2}___ (偶数或奇数)" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..c9a662f 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -10,13 +10,25 @@ print "请输入一个整数x,然后按 Enter: " x = gets +x = x.to_i print "请输入一个整数y,然后按 Enter: " y = gets +y = y.to_i print "请输入一个整数z,然后按 Enter: " z = gets +z = z.to_i # .... +if x > 0 + if y > 0 + result = (z > 0) ? "B" : "C" + else + result = (z > 0) ? "D" : "E" + end +else + result = "A" +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是____#{result}____(A或B或C或D或E)" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..c64c6db 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -8,7 +8,8 @@ print "请输入一个数字z,然后按 Enter: " z = gets - # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +result = [x, y, z].map(&:to_i).max + +puts "最大的数是 ___#{result}_____(x或y或z)" diff --git a/09-function.rb b/09-function.rb index b1f922d..d265f90 100644 --- a/09-function.rb +++ b/09-function.rb @@ -2,14 +2,17 @@ def calculate_area(a, b) # .... + (a * b) / 2 end print "请输入直角三角形的高,然后按 Enter: " a = gets +a = a.to_f print "请输入直角三角形的底边,然后按 Enter: " b = gets +b = b.to_f answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" diff --git a/10-function.rb b/10-function.rb index bb450fb..9fbcfef 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,7 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + [x, y, z].map(&:to_i).max end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +17,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" diff --git a/11-seven.rb b/11-seven.rb index 26c221d..0eb905d 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -4,6 +4,9 @@ while ( i <= 100 ) # .... + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..c25767f 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -6,8 +6,11 @@ while ( i <= 100 ) # .... + if i % 2 == 0 + total += i + end i+=1 end -puts total \ No newline at end of file +puts total diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..b153cb5 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -2,6 +2,7 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i # while ( ... ) # while ( ...) @@ -9,3 +10,12 @@ # end # end +i = 1 +while ( i <= n ) + h = 1 + while ( h <= n ) + puts "#{i} * #{h} = #{i * h}" + h += 1 + end + i += 1 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..fc8f2f1 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,19 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + if n < 2 + return false + elsif n == 2 + return true + else + for i in 2..n-1 + if n % i == 0 + return false + else + return true + end + end + end end print "请输入数字 N,然后按 Enter: " diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..869ea53 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,13 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - if n.to_i == target puts "恭喜猜中啦! " break + elsif n.to_i > target + puts "太高了,再猜一次" + else + puts "太低了,再猜一次" end -end \ No newline at end of file +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..76bcd3c 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,16 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = array[0] + for i in 1..((array.length)-1) + if array[i] > m + m = array[i] + end + end + m end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..e92cecb 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -1,6 +1,7 @@ # 使用者不断输入数字存进 Array,最后输出总和、平均、最大值、最小值 arr = [] +total = 0 while (true) print "请输入数字,结束请直接按 Enter: " @@ -12,9 +13,16 @@ end end +for i in arr + total += i +end + puts arr.to_s +average = total.to_f / arr.length +max = arr.max +min = arr.min -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 __#{total}___" +puts "平均是 __#{average}___" +puts "最大值是 ___#{max}__" +puts "最小值是 ___#{min}__" diff --git a/18-square.rb b/18-square.rb index 226e1c1..9eb4161 100644 --- a/18-square.rb +++ b/18-square.rb @@ -4,7 +4,11 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i +for i in 0..n-1 + arr << i ** 2 +end # ... -puts arr.to_s \ No newline at end of file +puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..dcd2112 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + result = [] + for i in arr + if i % 2 == 0 + result << i + end + end + result end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] \ No newline at end of file +puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..dc47359 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + result = [] + for i in arr + if i % 2 == 0 + result << i + end + end + result.sort.uniq end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +puts filter_even(arr).to_s # 应该是 [42, 46, 68, 86] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index 9cb58f8..1b4f1eb 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,19 @@ # https://zh.wikipedia.org/wiki/选择排序 def insertion_sort(arr) - #... + return arr if arr.length == 0 + (0..arr.length-1).each do |i| + min, index = arr[i], i + (i+1..arr.length-1).each do |j| + min, index = arr[j], j if arr[j] < min + arr[i], arr[index] = arr[index], arr[i] + end + end + arr end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] answer = insertion_sort(arr) -puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] \ No newline at end of file +puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] diff --git a/22-missing.rb b/22-missing.rb index 6898714..c6a624e 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,12 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + arr.uniq + result = [] + for i in 0..9 + result << i if !arr.include?(i) + end + result end answer = find_missing( [2,2,1,5,8,4] ) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..c229f7f 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,8 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + result = hash.values.max + hash::key(result) end h = { @@ -15,5 +16,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..9c5fbf4 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,16 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + result1 = [] + h = hash.values + for i in h + result1 << i if i % 2 == 0 + end + result2 = [] + for j in result1 + result2 << hash::key(j) + end + result2 end h = { @@ -17,5 +24,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..c7c631a 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -2,11 +2,10 @@ def count(arr) h = {} - - arr.each do |i| - # ... + arr.uniq.each do |i| + a = arr.count(i) + h[i] = a end - return h # 回传一个 hash end @@ -15,4 +14,3 @@ def count(arr) answer = count(arr) puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..b0c58b8 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,10 +7,23 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] +h = [] +arr.each do |a| + if a["age"] > 18 + h << a + end + h +end -# .... +(0..h.length-1).each do |i| + min, index = h[i], i + (i+1..h.length-1).each do |j| + min, index = h[j], j if h[j]["age"] < min["age"] + h[i], h[index] = h[index], h[i] + end +end -puts "所有成年人,并由小到大: _________" +puts "所有成年人,并由小到大: _____#{h}____" # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..91b9331 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,8 @@ class Person - # ... + attr_accessor :first_name, :last_name + def greet + puts "Hello, #{first_name} #{last_name}" + end end p1 = Person.new @@ -11,6 +14,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - - diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..5e400ac 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -1,5 +1,18 @@ # 请打开 wordcount.txt,计算每个单字出现的次数 doc = File.read("wordcount.txt") +word_count = Hash.new(0) +doc.each_line do |line| + line = line.gsub(/[,'".]/,'') + words = line.split + words.each do |word| + word_count[word] += 1 + end +end +puts '每个单词出现的频数为:' + +word_count.each do |key, value| + puts "#{key} #{value}" +end # ... diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..8df0922 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -14,16 +14,29 @@ while (true) print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " command = gets.chomp - if command == "add" print "请输入代办事项: " + task = gets.chomp + todos << task + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end # ... elsif command == "remove" print "请输入要删除的编号: " + todos.delete_at(gets.chomp.to_i) + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end # ... elsif command == "save" puts "存盘离开" - + File.open("todos.txt", "w+") do |f| + todos.each do |todo| + f << todo + f << "\n" + end + end # ... break; else diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..c741881 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-slate \ No newline at end of file diff --git a/todos.txt b/todos.txt index 4757e85..ea6b63f 100644 --- a/todos.txt +++ b/todos.txt @@ -2,3 +2,4 @@ Buy book Go Shopping Walk Gogo +Cake