From 86bfb881e13e12a6ca152754c9e7cf9e60dc3450 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Thu, 5 Oct 2017 23:50:41 +0800 Subject: [PATCH 01/30] 01 hello --- 01-hello.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..29a8257 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello, #{your_name}" From ae539c38bf9c8439cc876c3fcedfe2be5b2ab7fe Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Thu, 5 Oct 2017 23:52:44 +0800 Subject: [PATCH 02/30] 02 variable --- 02-variable.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..2821710 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,5 @@ # ... -puts "a 应该是 2,现在是 #{a}" -puts "b 应该是 1,现在是 #{b}" - +puts "a 应该是 2,现在是 #{b}" +puts "b 应该是 1,现在是 #{a}" From e01bfd02f6f1c6de1335c53bfa179d1d66a2c3c0 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:21:49 +0800 Subject: [PATCH 03/30] 03 triangle --- 03-triangle.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..3775704 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -1,11 +1,11 @@ # 题目: 使用者输入直角三角形的宽和高,输出三角形的面积 print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = gets.to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets +b = gets.to_i # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{(b * a)/2}" From d55a16a4499674fb4ae54d3eec6ffd8e3093e6bb Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:24:24 +0800 Subject: [PATCH 04/30] 04 pizzas --- 04-pizzas.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..ee7257e 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -1,12 +1,15 @@ # 题目: 输入有多少片比萨饼和多少人,输出每人可以分到几片,以及剩下几片 print "请输入有多少片比萨饼,然后按 Enter: " -pizzas = gets +pizzas = gets.to_i print "请输入有多少人要吃,然后按 Enter: " -people = gets +people = gets.to_i # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +a = pizzas +b = people + +puts "每人可分得几片: #{ a/b } 片" +puts "还剩下几片: #{ a%b } 片" From 2a7294ccfc77e7b166a51dd0c58365db56d2877d Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:28:34 +0800 Subject: [PATCH 05/30] 05 bmi --- 05-bmi.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..8b9ea51 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -5,13 +5,26 @@ # 如果 BMI 介于 18.5 ~ 24,显示正常 print "请输入您的体重(公斤),然后按 Enter: " -weight = gets +weight = gets.to_f print "请输入您的身高(厘米),然后按 Enter: " -height = gets +height = gets.to_f # ..... -puts "您的 BMI 是: _________" +a = weight +b = height +BMI = (a/(b*b)) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 是: #{(a/(b*b))}" + +print "您的 BMI 结果是: " +"#{ +if BMI < 18.5 + puts "过轻" +elsif BMI >= 24 + puts "过重" +else + puts "正常" +end +}" From 0877000913d80c44fc5662db0e581b5033f779c9 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:37:18 +0800 Subject: [PATCH 06/30] 06 interger-positive --- 06-interger-positive.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..ae3edce 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -2,9 +2,26 @@ print "请输入一个整数,然后按 Enter: " -x = gets +x = gets.to_i # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +print "这个数是: " +"#{ +if x > 0 + puts "正数" +elsif x == 0 + puts "零" +else x < 0 + puts "负数" +end +}" + +print "这个数是: " +"#{ +if x%2 == 0 + puts "偶数" +else + puts "奇数" +end +}" From 837d3035bd490df37aaa7d1e7ac860c0035a5308 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:39:35 +0800 Subject: [PATCH 07/30] 07 abcde --- 07-abcde.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..9c0862d 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,33 @@ # 当 z < 0 输出 "E" print "请输入一个整数x,然后按 Enter: " -x = gets +x = gets.to_i print "请输入一个整数y,然后按 Enter: " -y = gets +y = gets.to_i print "请输入一个整数z,然后按 Enter: " -z = gets +z = gets.to_i # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +print "结果是:" +"#{ +if x < 0 + puts "A" +else x < 0 + if y > 0 + if z > 0 + puts "B" + else z < 0 + puts "C" + end + else y < 0 + if z > 0 + puts "D" + else z < 0 + puts "E" + end + end +end +}" From b91011d93345b7775117a2054d0d4a0f2e5198eb Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:41:45 +0800 Subject: [PATCH 08/30] 08 find-max --- 08-find-max.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..4957caa 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -1,14 +1,24 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 print "请输入一个数字x,然后按 Enter: " -x = gets +x = gets.to_i print "请输入一个数字y,然后按 Enter: " -y = gets +y = gets.to_i print "请输入一个数字z,然后按 Enter: " -z = gets +z = gets.to_i # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +print "最大的数是: " +"#{ +if (x > y) && (y > z) + puts "#{x}" +elsif (y > x) && (x > z) + puts "#{y}" +else (z > x) && (x > y) + puts "#{z}" +end +} +" From 68562ddb5b75cb0d76a2e9ad2c23bad9c8442325 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:43:18 +0800 Subject: [PATCH 09/30] 09 function --- 09-function.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..d1e26af 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + (b * a)/2 end print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = gets.to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets +b = gets.to_i answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From 1684da5564f91ab482adb42fa95ed7ad2a39b969 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:44:27 +0800 Subject: [PATCH 10/30] 10 function --- 10-function.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..a558ceb 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,26 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y && y > z + return x + elsif y > x && x > z + return y + else z > x && x > y + return z + end end print "请输入一个数字x,然后按 Enter: " -x = gets +x = gets.to_i print "请输入一个数字y,然后按 Enter: " -y = gets +y = gets.to_i print "请输入一个数字z,然后按 Enter: " -z = gets +z = gets.to_i # .... answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From de69968a17495b6208c25e54f8fb21dfb738e078 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:46:52 +0800 Subject: [PATCH 11/30] 11 seven --- 11-seven.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..fb8f319 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,9 @@ i = 1 while ( i <= 100 ) - - # .... + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end From 8aef53b162e05744958168b13943fda5ad591f77 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 21:51:30 +0800 Subject: [PATCH 12/30] 12 sum-even --- 12-sum-even.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..820b1c5 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,10 +4,11 @@ total = 0 while ( i <= 100 ) - - # .... + if i % 2 == 0 + total = total + i + end i+=1 end -puts total \ No newline at end of file +puts total From 96b612a145619c7a643ff2b263b9fdd1cbe517a8 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:00:19 +0800 Subject: [PATCH 13/30] 13 nn --- 13-nn.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..a5242a9 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,14 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end +n = gets.to_i +i = 1 +while ( i <= n ) + h = 1 + while ( h <= n ) + puts "#{i} * #{h} = #{i*h}" + h += 1 + end + i += 1 +end From ae84f29eeef59d07180fb70c271353da2d086956 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:03:16 +0800 Subject: [PATCH 14/30] =?UTF-8?q?13=20nn=20=E7=AC=AC=E4=BA=8C=E7=A7=8D?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 13-nn.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index a5242a9..36e2ffa 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -4,11 +4,12 @@ n = gets.to_i i = 1 -while ( i <= n ) - h = 1 - while ( h <= n ) - puts "#{i} * #{h} = #{i*h}" - h += 1 +x = 0 +while x <= n + y = 0 + while y <= n + puts "#{x} x #{y} = #{x*y}" + y += 1 end - i += 1 + x += 1 end From d0b684fe949b1fe149ce1bccfbc181f4b6927514 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:04:32 +0800 Subject: [PATCH 15/30] 14 prime --- 14-prime.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..ea5c97a 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,16 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + if n > 2 + i = 2 + while i <= n / 2 + if n % i == 0 + return false + end + i+=1 + end + return true + end end print "请输入数字 N,然后按 Enter: " From e9340c94906876fcc8d48304542053688b38a3f3 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:06:35 +0800 Subject: [PATCH 16/30] 15 guess-number --- 15-guess-number.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..8096bf7 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -4,14 +4,18 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " - n = gets + n = gets.to_i #puts "太低了,再猜一次" #puts "太高了,再猜一次" - if n.to_i == target + if n == target puts "恭喜猜中啦! " break + elsif n > target + puts "太高了,再猜一次" + else n < target + puts "太低了,再猜一次" end - -end \ No newline at end of file + +end From b12c0b69526d338d6d85fec1e1ae0e20605b813d Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:09:05 +0800 Subject: [PATCH 17/30] 16 array-sum --- 16-array-sum.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..efbccff 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,18 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = 0 + + array.each do |i| + if m < i + m = i + end + end + + return m end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - From 1555d6206d0ce5629dcfb4d6e6fe111de3b7375a Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:11:38 +0800 Subject: [PATCH 18/30] 17 array-stats --- 17-array-stats.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..56b4a6d 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -14,7 +14,18 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +def find_sum(array) + sum = 0 + array.each do |i| + sum = sum + i + end + + return sum +end + +sum = find_sum(arr) + +puts "总和是 #{sum} " +puts "平均是 #{sum / arr.size} " +puts "最大值是 #{arr.max} " +puts "最小值是 #{arr.min} " From 04ecc80e61ef2900e1058b4336aa05da4aa4672f Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:13:22 +0800 Subject: [PATCH 19/30] 18 square --- 18-square.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..a4c1eb5 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,15 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i # ... -puts arr.to_s \ No newline at end of file +x = 0 +while (x <= n-1) + y = x * x + arr << y + x += 1 +end + +puts arr.to_s From 3d7101f68e92940589c6314040d34919e3aca35d Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:14:59 +0800 Subject: [PATCH 20/30] 19 filter --- 19-filter.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..6f20711 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,17 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + arr_even = [] + + arr.each do |i| + if i % 2 == 0 + arr_even.push(i) + end + end + + arr_even 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] From 3f1fdaf1fdaae0c6ab79fd708b2b37f3babe01d0 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:16:07 +0800 Subject: [PATCH 21/30] 20 sorting --- 20-sorting.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..fb986b6 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,18 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + arr_even = [] + + arr.each do |i| + if i % 2 == 0 + arr_even.push(i) + end + end + + arr_even 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).uniq.sort.to_s # 应该是 [42, 46, 68, 86] From 06fdee30800cb732d1d6521b4f9583228a67c2f0 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:17:33 +0800 Subject: [PATCH 22/30] 21 selection-sort --- 21-selection-sort.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..506a706 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,19 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_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 = selection_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] From 9728def543aae91b3bf5adf2ab1e8cb8f564c869 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:18:29 +0800 Subject: [PATCH 23/30] 22 missing --- 22-missing.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..90b3ec6 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,13 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + missing_number = [] + + (0..9).each do |i| + missing_number << i unless arr.include?(i) + end + + missing_number end answer = find_missing( [2,2,1,5,8,4] ) From 30fa6286c84a01cbf79cb16156ffa9c08b2f35d3 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:20:01 +0800 Subject: [PATCH 24/30] 23 has-max --- 23-hash-max.rb | 6 +++--- 24-hash-even.rb | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..3755a0e 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,9 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + hash.each do |key, value| + return key if value == hash.values.max + end end h = { @@ -15,5 +17,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..b79f953 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -17,5 +17,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From c6ec563c90f740ea0b224578a94e283258d4fdbe Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:21:28 +0800 Subject: [PATCH 25/30] 24 hash-even --- 24-hash-even.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index b79f953..31a9cd4 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,11 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + even = [] + hash.each do |key, value| + even << key if value % 2 == 0 + end + even end h = { From 61733fa535c8c1b384e4baa04d0df3cfca6587aa Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:22:49 +0800 Subject: [PATCH 26/30] 25 hash-coubt --- 25-hash-count.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..2b4e6bc 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,7 +4,8 @@ def count(arr) h = {} arr.each do |i| - # ... + a = arr.count(i) + h[i] = a end return h # 回传一个 hash @@ -15,4 +16,3 @@ def count(arr) answer = count(arr) puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - From b4412df9a22fc39605dfd1029a504a357aefe157 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:25:23 +0800 Subject: [PATCH 27/30] 26 hash-filter --- 26-hash-filter.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..8bf8530 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -10,7 +10,23 @@ # .... -puts "所有成年人,并由小到大: _________" +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 "所有成年人,并由小到大: #{h}" # 答案应该是 #[ From 81a189e4a133b8188e3c8e95bf8829f6868369b0 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:26:13 +0800 Subject: [PATCH 28/30] 27 class --- 27-class.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..607b10a 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,9 @@ class Person - # ... + attr_accessor :first_name, :last_name + + def greet + puts "Hi, #{first_name} #{last_name}" + end end p1 = Person.new @@ -11,6 +15,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - - From d253ea694f1d813eb2c00f4140b4ecd3461134f3 Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:27:25 +0800 Subject: [PATCH 29/30] 28 word-count --- 28-word-count.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..bd96762 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -3,3 +3,18 @@ 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 From c1d383dfbb19c2b29145121f0ed52d29744d163c Mon Sep 17 00:00:00 2001 From: Planck1043 Date: Fri, 6 Oct 2017 22:29:09 +0800 Subject: [PATCH 30/30] 29 todos --- 29-todos.rb | 20 +++++++++++++++----- todos.txt | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..875ae10 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -17,17 +17,27 @@ if command == "add" print "请输入代办事项: " - # ... + n = gets.chomp + todos << n + 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 puts "看不懂,请再输入一次" end end - diff --git a/todos.txt b/todos.txt index 4757e85..ebb88ea 100644 --- a/todos.txt +++ b/todos.txt @@ -2,3 +2,4 @@ Buy book Go Shopping Walk Gogo +Car