From 7db8a785fc5a88d048ac6a92370d700ffd4341a7 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 20:52:24 +0800 Subject: [PATCH 01/29] 01-hello --- 01-hello.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..2a27733 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,5 @@ # 题目: 输入名字,输出 "Hello, 名字" print "请输入你的名字,然后按 Enter: " -your_name = gets - -# ... - -puts "(请替换成最后的答案)" \ No newline at end of file +your_name = gets # 从键盘拿到输入的值 +puts "Hello, #{your_name}" # 输出值,并且有换行 From f91744dab8e50c19a73fe64d291b662160c41888 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 21:02:24 +0800 Subject: [PATCH 02/29] 02-variable --- 02-variable.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..53b4c95 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -6,8 +6,9 @@ puts "a 是 #{a}" puts "b 是 #{b}" -# ... +c = a +a = b +b = c puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - From 0ac723d9131d7fd5eab2bfbc1e066cd821b31175 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 21:08:39 +0800 Subject: [PATCH 03/29] 03-triangle --- 03-triangle.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..bc6588f 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -2,10 +2,12 @@ print "请输入直角三角形的高,然后按 Enter: " a = gets +a = a.to_f print "请输入直角三角形的底边,然后按 Enter: " b = gets +b = b.to_f -# ..... +area = a * b -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{area}" From ae6dac4b855140a81d7e7878c172a3072cfc0f86 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 21:14:56 +0800 Subject: [PATCH 04/29] 04-pizzas --- 04-pizzas.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..77cd48c 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -2,11 +2,14 @@ print "请输入有多少片比萨饼,然后按 Enter: " pizzas = gets +pizzas = pizzas.to_i print "请输入有多少人要吃,然后按 Enter: " people = gets +people = people.to_i -# ..... +per = pizzas / people +remain = pizzas % people -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得 #{per} 片" +puts "还剩下 #{remain} 片" From 78759289883fc9201a64d19b6568a8a047fb9847 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 21:31:18 +0800 Subject: [PATCH 05/29] 05-bmi --- 05-bmi.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..9a50a95 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 * height) -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 2a035c436a30bd811785a456ae69f7b3b9c77355 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 21:47:38 +0800 Subject: [PATCH 06/29] 06-interger-positive --- 06-interger-positive.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..fd8aed5 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -3,8 +3,21 @@ print "请输入一个整数,然后按 Enter: " x = gets +x = x.to_i -# .... +if x > 0 + result1 = "正数" +elsif x < 0 + result1 = "负数" +else + result1 = "零" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x % 2 == 0 + result2 = "偶数" +else + result2 = "奇数" +end + +puts "这个数是 #{result1}" +puts "这个数是 #{result2}" From 82f355ec3368f742659d15ce1c39d5a89b050e45 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:03:33 +0800 Subject: [PATCH 07/29] 07-abcde --- 07-abcde.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..891d0fa 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -10,13 +10,32 @@ 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 + result = "A" +elsif x > 0 + if y > 0 + if z > 0 + result = "B" + elsif z < 0 + result = "C" + end + elsif y < 0 + if z > 0 + result = "D" + elsif z < 0 + result = "E" + end + end +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是 #{result}" From c8d7159af66ccb15b272b68f5489d9fd5e1c0465 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:11:42 +0800 Subject: [PATCH 08/29] 08-find-max --- 08-find-max.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..3104308 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -2,13 +2,16 @@ print "请输入一个数字x,然后按 Enter: " x = gets +x = x.to_f print "请输入一个数字y,然后按 Enter: " y = gets +y = y.to_f print "请输入一个数字z,然后按 Enter: " z = gets +z = z.to_f -# .... +max = [x, y, z].max -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 #{max}" From 4b1a1b4771b70eddd8bb201bbf147904113a3856 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:14:27 +0800 Subject: [PATCH 09/29] 09-function --- 09-function.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..3591a7f 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,17 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + a * b 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}" From 091adb8321e2baab19d51da9f65b57e59f0125d3 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:17:57 +0800 Subject: [PATCH 10/29] 10-function --- 10-function.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..b4a2f73 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,21 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + [x, y, z].max end print "请输入一个数字x,然后按 Enter: " x = gets +x = x.to_f print "请输入一个数字y,然后按 Enter: " y = gets +y = y.to_f print "请输入一个数字z,然后按 Enter: " z = gets - -# .... +z = z.to_f answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From 6407390a848a4f3fcc015446874ece62b3f60197 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:24:41 +0800 Subject: [PATCH 11/29] 11-seven --- 11-seven.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..5b01f64 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,8 @@ i = 1 while ( i <= 100 ) - - # .... - + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end From 86f9f02c4b96b7afd22b7888dc5c8a27bd162ca7 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Sun, 1 Oct 2017 22:27:24 +0800 Subject: [PATCH 12/29] 12-sum-even --- 12-sum-even.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..cb6aa4f 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,10 +4,10 @@ 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 38b838fd461e88fbf85f2286297c4e26cac2ead2 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 17:52:38 +0800 Subject: [PATCH 13/29] 13-nn --- 13-nn.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..5458e3c 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -2,10 +2,14 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i -# while ( ... ) -# while ( ...) -# -# end -# end - +x = 0 +while x <= n + y = 0 + while y <= n + puts "#{x} x #{y} = #{x * y}" + y+=1 + end + x+=1 +end From fbaa79421efbda4474221e51a1306149270d6ba7 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 19:42:35 +0800 Subject: [PATCH 14/29] 14-prime --- 03-triangle.rb | 2 +- 14-prime.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index bc6588f..806b962 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -8,6 +8,6 @@ b = gets b = b.to_f -area = a * b +area = a * b / 2 puts "直角三角形的面积是: #{area}" diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..78e413b 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,9 +1,27 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + if n < 2 + return false + end + + if n == 2 + return true + end + + 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: " n = gets From a5080161e3070865f76e1d2d14de76fe8aa67d3c Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 19:47:14 +0800 Subject: [PATCH 15/29] 15-guess-number --- 15-guess-number.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..f9e5456 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,17 @@ 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 6cc4cd0c3ce9adcda61eb301ff196a535162752f Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 20:39:15 +0800 Subject: [PATCH 16/29] 16-array-sum --- 16-array-sum.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..983e7b6 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] +arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88, 102] max = find_max(arr) -puts "Max is #{max}" # 应该是 88 - +puts "Max is #{max}" From 19b4c32408de8c13b14ec334dab523ac8671d461 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 20:59:20 +0800 Subject: [PATCH 17/29] 17-array-stats --- 17-array-stats.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..0f77155 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,14 @@ end end +sum = 0 +arr.each do |i| + sum = sum + i +end + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 #{sum}" +puts "平均是 #{sum / arr.length}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" From 9e8a97c42148812b523b1e9833d8a03d2796af8a Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 21:15:39 +0800 Subject: [PATCH 18/29] 18-square --- 18-square.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..6b2595f 100644 --- a/18-square.rb +++ b/18-square.rb @@ -1,10 +1,12 @@ -# 建构一个阵列有一百个的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方 +# 建构一个阵列有N的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方 arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets().to_i -# ... +(0..n-1).each_with_index do |i| + arr << i ** 2 +end -puts arr.to_s \ No newline at end of file +puts arr.to_s From cb703b44d6709da254f91d88454f2107856cdc75 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 21:40:17 +0800 Subject: [PATCH 19/29] 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..0e397ed 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + filter_even = [] + arr.each do |i| + if i % 2 == 0 + filter_even << i + end + end + filter_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 2b5d575a0619382eb7c18b451b6141648986863d Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Mon, 2 Oct 2017 21:44:39 +0800 Subject: [PATCH 20/29] 20-sorting --- 20-sorting.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..71c5d4a 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,15 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + filter_even = [] + arr.each do |i| + if i % 2 == 0 + filter_even << i + end + end + filter_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).sort.uniq.to_s # 应该是 [42, 46, 68, 86] From 42eaa69f233dd20047f2f848a777251aae7d4563 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 11:49:58 +0800 Subject: [PATCH 21/29] 21-selection-sort --- 21-selection-sort.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..8eea8bf 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,17 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + (0...arr.length).each do |i| + min, index = arr[i], i + (i...arr.length).each { |j| min, index = arr[j], j if arr[j] < min } + arr[i], arr[index] = arr[index], arr[i] + 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 0c63232a70986eab557cd347dda81355fd86a6cf Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 13:24:24 +0800 Subject: [PATCH 22/29] 22-missing --- 22-missing.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..06cba38 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,11 @@ # 给定一阵列内含数字,请输出 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 70ca46947dc5692f7bbffb327e275c275c9f8817 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 13:40:17 +0800 Subject: [PATCH 23/29] 23-hash-max --- 23-hash-max.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..0978898 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,19 +1,19 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + hash.each do |key, value| + return key if value == hash.values.max + end end h = { "a" => 71, "b" => 38, - "c" => 21, + "c" => 121, "d" => 80, "e" => 10 } answer = find_max(h) -puts "有最大 value 的是 #{answer}" # 应该是 d - - +puts "有最大 value 的是 #{answer}" From ff28ec21733d7de61b08ed8b700271bed003b442 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 13:59:41 +0800 Subject: [PATCH 24/29] 24-hash-even --- 24-hash-even.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..efb04df 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,11 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + answer = [] + hash.each do |key, value| + answer << key if value % 2 == 0 + end + answer end h = { @@ -17,5 +19,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 4355e83e3228dcc5df98453f156eb6e7d5de8e6c Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 14:05:50 +0800 Subject: [PATCH 25/29] 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..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 e5907d190d5344be6c0b98466772a9e676bb7073 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 15:57:11 +0800 Subject: [PATCH 26/29] 26-hash-filter --- 26-hash-filter.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..27699c7 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,18 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... +def filter(arr) + result = [] + arr.each do |h| + if h["age"] >= 18 + result << h + end + end -puts "所有成年人,并由小到大: _________" + result.sort_by { |i| i["age"] } +end + +puts "所有成年人,并由小到大: #{filter(arr)}" # 答案应该是 #[ From 85d65f7626fcdc7b7bde6e14a212b0a5e16357eb Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 16:04:43 +0800 Subject: [PATCH 27/29] 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..965dc58 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,9 @@ class Person - # ... + attr_accessor :first_name, :last_name + + def greet + puts "Hello, #{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 7188277225ce07e287dfd9509cc3b22a6f66ab14 Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 16:11:06 +0800 Subject: [PATCH 28/29] 28-word-count --- 28-word-count.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..9af0119 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,12 @@ doc = File.read("wordcount.txt") -# ... +words = doc.downcase.scan(/\w+/) + +result = {} + +words.each do |i| + result[i] = words.count(i) +end + +puts result From fbb2d2ca49d8a53924a6849f272a555664bf3a5b Mon Sep 17 00:00:00 2001 From: tutuwuhan Date: Tue, 3 Oct 2017 16:19:30 +0800 Subject: [PATCH 29/29] 29-todos --- 29-todos.rb | 19 +++++++++++++++---- todos.txt | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..ec5f78b 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -1,5 +1,7 @@ # 简易 Todo 代办事项应用 +require 'byebug' + text = File.read("todos.txt") todos = [] @@ -17,17 +19,26 @@ if command == "add" print "请输入代办事项: " - # ... + todos << gets elsif command == "remove" print "请输入要删除的编号: " - # ... + choose = gets().to_i + todos.delete_at(choose) elsif command == "save" puts "存盘离开" - # ... + File.open("todos.txt", "w+") do |f| + for a in todos + f << a + "\n" + end + end + + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end + break; else puts "看不懂,请再输入一次" end end - diff --git a/todos.txt b/todos.txt index 4757e85..f7988fa 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1,5 @@ Buy book Go Shopping Walk -Gogo +Have dinner +