From da80646c3be7dc8e33beba7a71d0cc043f2eb84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 20:57:12 +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..fe34e8d 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 aff2ba19b404c1940615d1a76151198242677fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 21:02:49 +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..dd37146 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -1,7 +1,7 @@ # 题目: 交换 a, b 变数的值 -a = 1 -b = 2 +a = 2 +b = 1 puts "a 是 #{a}" puts "b 是 #{b}" @@ -10,4 +10,3 @@ puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - From edb8e82fd6a960db2e0fab944c94a314ce6c231d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 21:07:53 +0800 Subject: [PATCH 03/30] 03-triangle --- 03-triangle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..d65d5c6 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,6 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets -# ..... +area = (a.to_i * b.to_i) / 2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{area}" From c6a1e753e9dd648b9cf2210e347f8d41cd5b36c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 21:19:44 +0800 Subject: [PATCH 04/30] ruby 04-pizzas --- 04-pizzas.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..b3f89aa 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -6,7 +6,9 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -# ..... +every_one_eat = (pizzas.to_i / people.to_i).floor -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +remainder = pizzas.to_i % people.to_i + +puts "每人可分得几片: #{every_one_eat} 片" +puts "还剩下几片: #{remainder} 片" From 4e17e39ce95fc374cab1549805616a5b533b3260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 21:41:00 +0800 Subject: [PATCH 05/30] 05-bmi --- 05-bmi.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..e8ef07c 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -7,11 +7,19 @@ print "请输入您的体重(公斤),然后按 Enter: " weight = gets -print "请输入您的身高(厘米),然后按 Enter: " +print "请输入您的身高(米),然后按 Enter: " height = gets -# ..... +bmi = weight.to_f / (height.to_f) ** 2 -puts "您的 BMI 是: _________" +if bmi < 18.5 + result = "过轻" +elsif bmi >= 24 + result = "过重" +else + result = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 是: #{bmi}" + +puts "您的 BMI 结果是: #{result}" From b027e7e4eb4dcdc756ce47532ebbd2790824cbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 21:54:22 +0800 Subject: [PATCH 06/30] 06-interger-positive --- 06-interger-positive.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..89bb21f 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -4,7 +4,23 @@ print "请输入一个整数,然后按 Enter: " x = gets -# .... +y = x.to_i +z = y % 2 -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if y == 0 + result_1 = "零" +elsif y < 0 + result_1 = "负数" +elsif y > 0 + result_1 = "正数" +end + +if z == 0 + result_2 = "偶数" +else + result_2 = "奇数" +end + + +puts "这个数是#{result_1}" +puts "这个数是#{result_2}" From 0b49875cd50f4cb0db58d087d36133df0e8b7463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 22:09:18 +0800 Subject: [PATCH 07/30] 07-abcde --- 07-abcde.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..ea6e1c8 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,6 +17,20 @@ print "请输入一个整数z,然后按 Enter: " z = gets -# .... +x_i = x.to_i +y_i = y.to_i +z_i = z.to_i -puts "结果是________(A或B或C或D或E)" \ No newline at end of file + if x_i < 0 + result = "A" + elsif x_i > 0 && y_i > 0 && z_i > 0 + result = "B" + elsif x_i > 0 && y_i > 0 && z_i < 0 + result = "C" + elsif x_i > 0 && y_i < 0 && z_i > 0 + result = "D" + elsif x_i > 0 && y_i < 0 && z_i < 0 + result = "E" + end + +puts "#{result}" From 1d81e6329aafded4f7613eea36b9493d76266bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Tue, 17 Oct 2017 22:26:11 +0800 Subject: [PATCH 08/30] 08-find-max --- 08-find-max.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..b7dafd5 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,16 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +x_f = x.to_f +y_f = y.to_f +z_f = z.to_f -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +if x_f > y_f && x_f > z_f + result = x +elsif y_f > x_f && y_f > z_f + result = y +elsif z_f > x_f && z_f > y_f + result = z +end + +puts "#{result}" From 8ced33ee5eecd50b4c7a2386aa7f3f37e29c77ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 18:32:21 +0800 Subject: [PATCH 09/30] 09-function --- 09-function.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..acc468e 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,7 +1,7 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + a.to_f * b.to_f / 2 end print "请输入直角三角形的高,然后按 Enter: " @@ -12,4 +12,4 @@ def calculate_area(a, b) answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From 1d603a988588c3bc0f99ff6c09727d3f9b033427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 18:35:41 +0800 Subject: [PATCH 10/30] 10-function --- 10-function.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..637862f 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,18 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + + x_f = x.to_f + y_f = y.to_f + z_f = z.to_f + + if x_f > y_f && x_f > z_f + result = x + elsif y_f > x_f && y_f > z_f + result = y + elsif z_f > x_f && z_f > y_f + result = z + end end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +28,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From 7948e1d1c2729af253e4edbda5dbe9efc8e43108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 18:52:10 +0800 Subject: [PATCH 11/30] 11-seven --- 11-seven.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..afc26f9 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,9 @@ i = 1 while ( i <= 100 ) - # .... + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end From ac05223e6edcb0722b23f75b0e0b973183d347cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 18:58:28 +0800 Subject: [PATCH 12/30] 12-sum-even --- 12-sum-even.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..5a74ad9 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,11 @@ while ( i <= 100 ) - # .... + if i % 2 == 0 + total += i + end i+=1 end -puts total \ No newline at end of file +puts total From 38953e3f894185ca9c43a1d96fb4b61617d3fc1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 20:30:30 +0800 Subject: [PATCH 13/30] 13-nn --- 13-nn.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..e4704d6 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,13 @@ print "请输入数字 N,然后按 Enter: " n = gets -# while ( ... ) -# while ( ...) -# -# end -# end +a = 0 +while (a <= n.to_i) + b = 0 + while (b <= n.to_i) + puts "#{a} x #{b} = #{a * b}" + b += 1 + end + a += 1 +end From 03974c7b6112942a0e311f42ae0d574a98d9aaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 21:27:43 +0800 Subject: [PATCH 14/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..736454a 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,16 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + a = 2 + while a <= n + if n % a == 0 + break + return false + else + return true + end + a += 1 + end end print "请输入数字 N,然后按 Enter: " From 2873f79dcf213d2a49d65ee57e9f668c9026b857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 21:39:40 +0800 Subject: [PATCH 15/30] 15-guess-number --- 15-guess-number.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..994b5a1 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,18 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" + if n.to_i >= target + puts "太高了,再猜一次" + end + + if n.to_i <= target + puts "太低了,再猜一次" + end + if n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end From 4624b1d7f2523eb3ab6aacc4fb42ffc082e8b3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 21:47:27 +0800 Subject: [PATCH 16/30] 16-array-sum --- 16-array-sum.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..de3e8cb 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,10 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + array.max end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - From 87f277833e14adc04ef58c58dfd5cae5b0e7a53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Wed, 18 Oct 2017 22:16:30 +0800 Subject: [PATCH 17/30] 17-array-stats --- 17-array-stats.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..514b382 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,16 @@ end end +sum = 0 +arr.each do |i| + sum += i +end + +average = sum / arr.size + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 #{sum}" +puts "平均是 #{average}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" From 1183cafa1363c806d547abefdf42bc560013d95c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 19:23:45 +0800 Subject: [PATCH 18/30] 18-square --- 18-square.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..6602934 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,13 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i -# ... +x = 0 -puts arr.to_s \ No newline at end of file +while x <= n + arr << x ** 2 + x += 1 +end + +puts arr.to_s From c668bc45e4476207d986e2d57777690ac0c14be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 19:35:45 +0800 Subject: [PATCH 19/30] 18-square-2 --- 18-square-2.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 18-square-2.rb diff --git a/18-square-2.rb b/18-square-2.rb new file mode 100644 index 0000000..499eb37 --- /dev/null +++ b/18-square-2.rb @@ -0,0 +1,12 @@ +# 建构一个阵列有一百个的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方 + +arr = [] + +print "请输入数字 N,然后按 Enter: " +n = gets.to_i + +(0..n).each_with_index do |i,j| + arr << j ** 2 +end + +puts arr.to_s From 7335e03c799b7c1ebb709739d581401d99e76957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 19:44:44 +0800 Subject: [PATCH 20/30] 19-filter --- 19-filter.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..a52c52b 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + a = [] + arr.each do |i| + if i % 2 == 0 + a << i + end + end + return a 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 f73cb078f0dc90afe1facd8f621dca77245568be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 19:53:00 +0800 Subject: [PATCH 21/30] 20-sorting --- 20-sorting.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..3363e52 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + a = [] + arr.each do |i| + if i % 2 == 0 + a << i + end + end + return a 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}" # 应该是 [42, 46, 68, 86] From 859a70c9558ef539117c499f42ec1cbc45d4011a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 20:51:48 +0800 Subject: [PATCH 22/30] 21-selection-sort --- 21-selection-sort.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..80bf167 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,16 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + ans = [] + while arr.size > 0 + ans << arr.min + arr.delete(arr.min) + end + ans 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 e7fe46dd99534e2b29b6ce5e497731c677352233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 21:24:54 +0800 Subject: [PATCH 23/30] 22-missing --- 22-missing.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..ec0368c 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,8 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + a = (0..9).to_a + a - arr.uniq end answer = find_missing( [2,2,1,5,8,4] ) From e3c65e1e0626c68edb0952a170fbfd68dfeb5466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 21:37:25 +0800 Subject: [PATCH 24/30] 23-hash-max --- 23-hash-max.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..73132cc 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,7 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + hash.key(hash.values.max) end h = { @@ -15,5 +15,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 77ceb6923a3c69b53d0a6d1db816563d1381de52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 21:46:51 +0800 Subject: [PATCH 25/30] 24-hash-even --- 24-hash-even.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..16c8732 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,13 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + ans = [] + hash.values.each do |i| + if i % 2 == 0 + ans << hash.key(i) + end + end + ans end h = { @@ -17,5 +21,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 8e7f577333a44c7d3602e82875bd81f4873042ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Thu, 19 Oct 2017 22:20:11 +0800 Subject: [PATCH 26/30] 25-hash-count --- 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..478d561 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,7 +4,8 @@ def count(arr) h = {} arr.each do |i| - # ... + c = arr.count(i) + h[i] = c 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 c523908f443f765c4306a98a6e0361382d8ade7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Fri, 20 Oct 2017 19:55:28 +0800 Subject: [PATCH 27/30] 26-hash-filter --- 26-hash-filter.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..65bcbaf 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,10 +7,17 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] +person = [] -# .... +arr.each do |i| + if i["age"] >= 18 + person << i + end +end -puts "所有成年人,并由小到大: _________" +result = person.sort_by {|a| a["age"]} + +puts "所有成年人,并由小到大: #{result}" # 答案应该是 #[ From 76610d2b3d6d72a6e644aac8dc3c0fec2b35c1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Fri, 20 Oct 2017 20:49:32 +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..5882a91 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 734d92d6975b5e17f429213e9cf39bce73dc8d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Fri, 20 Oct 2017 22:09:35 +0800 Subject: [PATCH 29/30] 28-word-count --- 28-word-count.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..6ae41ba 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,15 @@ doc = File.read("wordcount.txt") -# ... +cut_doc = doc.downcase.scan(/\w+/) + +words = cut_doc.uniq + +result = {} + +words.each do |i| + a = cut_doc.count(i) + result[i] = a +end + + puts result From 6ca46e040bf9406c73ee1371bff8c7f5735e52b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BD=A6=E9=94=8B?= Date: Sun, 22 Oct 2017 09:57:18 +0800 Subject: [PATCH 30/30] 29-todos --- 29-todos.rb | 22 +++++++++++++++------- todos.tex | 5 +++++ todos.txt | 2 ++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 todos.tex diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..32b92cb 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -15,19 +15,27 @@ print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " command = gets.chomp - if command == "add" + if command == "1" print "请输入代办事项: " - # ... - elsif command == "remove" + todos << gets.chomp + elsif command == "2" print "请输入要删除的编号: " - # ... - elsif command == "save" + ind = gets.to_i + todos.delete_at(ind) + elsif command == "3" puts "存盘离开" + File.open("todos.txt","w+") do |f| + todos.each do |i| + f << i + f << "\n" + end + end - # ... + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end break; else puts "看不懂,请再输入一次" end end - diff --git a/todos.tex b/todos.tex new file mode 100644 index 0000000..faa982d --- /dev/null +++ b/todos.tex @@ -0,0 +1,5 @@ +Buy book +Go Shopping +Walk +Gogo +add01 diff --git a/todos.txt b/todos.txt index 4757e85..4bd72ec 100644 --- a/todos.txt +++ b/todos.txt @@ -2,3 +2,5 @@ Buy book Go Shopping Walk Gogo +add01 +add02