From db0d7cdad4f969e67c08582c7a7527f8c75964b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Sun, 27 Aug 2017 21:23:18 +0800 Subject: [PATCH 01/29] 1 --- 01-hello.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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}" From ac673c65717042443b0aa0d4c0840827058a6ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 16:43:07 +0800 Subject: [PATCH 02/29] 2 --- 02-variable.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..677c809 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -6,8 +6,11 @@ puts "a 是 #{a}" puts "b 是 #{b}" -# ... +c = a + +a = b + +b = c puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - From ac44ab548a9a5d85a4661b7ebeb429c0e64ea30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 16:55:28 +0800 Subject: [PATCH 03/29] 3 --- 03-triangle.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..8d6b42a 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,9 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets +c = a.to_f * b.to_f + +c = c / 2 # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: __#{c}___" From a18f3dff32cc87c14b01daf24b7c190283e13346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 17:32:44 +0800 Subject: [PATCH 04/29] 4 --- 04-pizzas.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..62da0e1 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -8,5 +8,9 @@ # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +a=pizzas.to_i/people.to_i + +b=pizzas.to_i%people.to_i + +puts "每人可分得几片: ____#{a}_____ 片" +puts "还剩下几片: _____#{b}____ 片" From 9b267148ad123a0db6ae7ce37226a38555cd5a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 17:52:36 +0800 Subject: [PATCH 05/29] 5 --- 05-bmi.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..6b1605d 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,20 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets +w = weight.to_f +h = height.to_f/100 +b = w/h +b = b/h + +if b < 18.5 + a = "过轻" +elsif b >= 24 + a = "过重" +else + a = "正常" +end # ..... -puts "您的 BMI 是: _________" +puts "您的 BMI 是: ____#{b}_____" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 结果是: ____#{a}_____(过轻或正常或过重)" From 0b5a20d58b9c76b57297545e32d4f344586cfeab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 18:02:52 +0800 Subject: [PATCH 06/29] 06 --- 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..61b1d7f 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 + a = "正数" +elsif x < 0 + a = "负数" +else + a = "0" +end + +if x%2 != 0 + b = "奇数" +else + b = "偶数" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +puts "这个数是__#{a}___ (正数或零或负数)" +puts "这个数是___#{b}__ (偶数或奇数)" From 8e035e43d85a669a76f81e5befa2dbc38698cc7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 18:08:53 +0800 Subject: [PATCH 07/29] 7 --- 07-abcde.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..58155d5 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,6 +17,27 @@ print "请输入一个整数z,然后按 Enter: " z = gets +x = x.to_i +y = y.to_i +z = z.to_i + +if x < 0 + a = "A" +else + if y > 0 + if z > 0 + a = "B" + else + a = "C" + end + else + if z > 0 + a = "D" + else + a = "E" + end + end +end # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是____#{a}____(A或B或C或D或E)" From 5e99d67e36be36dd52ed20845be6c3d700b2ed09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Mon, 28 Aug 2017 19:18:35 +0800 Subject: [PATCH 08/29] 8 --- 08-find-max.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..1008b62 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,18 @@ print "请输入一个数字z,然后按 Enter: " z = gets +x = x.to_f +y = y.to_f +z = z.to_f + +if x >= y && x >= z + a = "x" +elsif y >= x && y >= z + a = "y" +else + a = "z" +end + # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 ___#{a}_____(x或y或z)" From 64ac748339eac7812038c496fbf6c1c71a6641f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:13:09 +0800 Subject: [PATCH 09/29] 11 --- 11-seven.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..ec2771a 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,11 @@ i = 1 while ( i <= 100 ) - # .... + if i%7 == 0 + + puts i + + end i+=1 -end \ No newline at end of file +end From 6469dee027f5454fc913bf002fc1545f8c22928f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:14:14 +0800 Subject: [PATCH 10/29] 12 --- 12-sum-even.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..703fb0e 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,13 @@ 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 e463dd709124c796c8470d04d5623a645e40ab6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:21:35 +0800 Subject: [PATCH 11/29] 13 --- 13-nn.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..f7a4601 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,17 @@ print "请输入数字 N,然后按 Enter: " n = gets -# while ( ... ) -# while ( ...) -# -# end -# end +n = n.to_i +a=1 +b=1 + +while (a<=n) + while (b<=n) + print "#{a*b} " + b=b+1 + end + b=1 + a=a+1 + puts +end From 022490432a27c3379fe19694b5d74d06afda44c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:23:59 +0800 Subject: [PATCH 12/29] 9 --- 09-function.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..f44dc1d 100644 --- a/09-function.rb +++ b/09-function.rb @@ -2,6 +2,7 @@ def calculate_area(a, b) # .... + c =(a.to_f*b.to_f)/2 end print "请输入直角三角形的高,然后按 Enter: " @@ -12,4 +13,4 @@ def calculate_area(a, b) answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From f34cefbe83b313baba75ca1544f98aa1b2c3b710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:31:31 +0800 Subject: [PATCH 13/29] 10 --- 10-function.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..475eab9 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,17 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + x=x.to_f + y=y.to_f + z=z.to_f + + if x>y && y>z + x + elsif y>z + y + else + z + end end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +27,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From f3d86797d149c0237becbd77f9f8f06f827f95e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 19:35:55 +0800 Subject: [PATCH 14/29] 14 --- 14-prime.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..4a3f4a5 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,6 +1,16 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) + n=n.to_i + i=2 + while i<=n/2 + if n % i ==0 + return false + else + i = i + 1 + end + end + true # .... end From b778cdc692df55b62d604525b7dceafcd62a95cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 20:13:55 +0800 Subject: [PATCH 15/29] 15 --- 15-guess-number.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..1fd8b7f 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,16 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - + n=n.to_i + if ntarget + puts "太高了,再猜一次" + end if n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end From 5ed4200b1ebb8579b4ede37a150e360c52160bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 20:20:38 +0800 Subject: [PATCH 16/29] 16 --- 16-array-sum.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..4980b2d 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -2,10 +2,18 @@ def find_max(array) #.... + i=0 + m=0 + while i <(array.length) + if m Date: Wed, 30 Aug 2017 20:46:17 +0800 Subject: [PATCH 17/29] 17 18 --- 17-array-stats.rb | 13 +++++++++---- 18-square.rb | 9 +++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..64a4e03 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -11,10 +11,15 @@ arr << user_input.to_i end end +total = 0 +arr.each{|a| total += a} +ave = total.to_f/arr.length +max = arr.max +min = arr.min puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 __#{total}___" +puts "平均是 ___#{ave}__" +puts "最大值是 ___#{max}__" +puts "最小值是 ___#{min}__" diff --git a/18-square.rb b/18-square.rb index 226e1c1..f372b8d 100644 --- a/18-square.rb +++ b/18-square.rb @@ -4,7 +4,12 @@ print "请输入数字 N,然后按 Enter: " n = gets - +n=n.to_i +i = 0 +while i<=n + arr[i]=i ** 2 + i=i+1 +end # ... -puts arr.to_s \ No newline at end of file +puts arr.to_s From 1bda998ecc8309997981ca643244a66d418a8158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 21:19:43 +0800 Subject: [PATCH 18/29] 19 --- 19-filter.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..945edfb 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + newarr = [] + for a in arr + if a%2==0 + newarr.push(a) + end + end + newarr 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 c3eaefd98d4da367e333d352b328b0d3d12444c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 21:26:55 +0800 Subject: [PATCH 19/29] 20 --- 20-sorting.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..c99d5f0 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -3,9 +3,17 @@ def filter_even(arr) #... + newarr = [] + for a in arr + if a%2==0 + newarr.push(a) + end + end + newarr.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)}" # 应该是 [42, 46, 68, 86] From 1e8ad0ede6074cb13fdae881fc7d91f5196c99f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 21:32:36 +0800 Subject: [PATCH 20/29] 21 --- 21-selection-sort.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..174d371 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,6 +2,20 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) + for i in 0..arr.length-1 + min = i + for j in i+1..arr.length-1 + if arr[min]>arr[j] + temp = arr[min] + arr[min] = arr[j] + arr[j] = temp + end + j += 1 + end + i += 1 + end + arr + #... end @@ -9,4 +23,4 @@ def selection_sort(arr) 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 f32e4c545c4fda02d4c0ae2c919370f5855173cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 21:59:30 +0800 Subject: [PATCH 21/29] 22 --- 22-missing.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..f745f9b 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,21 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + # ... missingArr = [] + missingArr = [] + for i in 0..9 + missing = true + for a in arr + if a == i + missing = false + break + end + end + if missing + missingArr.push(i) + end + end + missingArr end answer = find_missing( [2,2,1,5,8,4] ) From 5830dd7c54e8d570c3c63f5f65831142aab46cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:02:37 +0800 Subject: [PATCH 22/29] 23 --- 23-hash-max.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..de11cbe 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -2,6 +2,15 @@ def find_max(hash) # ... + max_value = hash.values.max + max_key = "" + hash.each do |key,value| + if value == max_value + max_key = key + break + end + end + max_key end h = { @@ -15,5 +24,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 8300a5c78e33d1bd73b9c33e20a80ee967f2a96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:09:03 +0800 Subject: [PATCH 23/29] 24 --- 24-hash-even.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..5a3d0c3 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -4,6 +4,15 @@ def find_even_keys(hash) # ... (请回传一个数组) + even_keys = [] + hash.each do |key,value| + if value % 2 == 0 + even_keys.push(key) + end + end + even_keys + + end h = { @@ -17,5 +26,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 8173bff336494f8338e2262e336b38ac45b1951d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:09:49 +0800 Subject: [PATCH 24/29] 25 --- 25-hash-count.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..2cb3b20 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,6 +4,12 @@ def count(arr) h = {} arr.each do |i| + if h[i] + h[i] += 1 + else + h[i] = 1 + end + # ... end @@ -15,4 +21,3 @@ def count(arr) answer = count(arr) puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - From 67efec7cc95e2930bb37f0cc44e863768c601e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:11:19 +0800 Subject: [PATCH 25/29] 26 --- 26-hash-filter.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..c2f117e 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,7 +7,20 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] - + ages = [] + arr.each do |h| + ages.push(h["age"]) + end + ages = ages.sort + newArr = [] + for age in ages + arr.each do |h| + if age == h["age"] + newArr.push(h) + break + end + end + end # .... puts "所有成年人,并由小到大: _________" From 6caf39204d18f8fd64a3f54ce568584a7b205077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:12:17 +0800 Subject: [PATCH 26/29] 26debug --- 26-hash-filter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index c2f117e..ac0528e 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -23,7 +23,7 @@ end # .... -puts "所有成年人,并由小到大: _________" +puts "所有成年人,并由小到大: ___#{newArr}______" # 答案应该是 #[ From d98bcfd7563bf76669f78a075dc60a27dd8b0065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:15:32 +0800 Subject: [PATCH 27/29] 27 --- 27-class.rb | 9 +++++---- 28-word-count.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..b61f17b 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" - - - diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..c88b566 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -3,3 +3,14 @@ doc = File.read("wordcount.txt") # ... + doc = doc.split(" ") + h = {} + doc.each do |word| + if h[word] + h[word] += 1 + else + h[word] = 1 + end + end + + puts h From 4c11fb1e622aecb0ac20d5cee479597bc5d9610a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:16:07 +0800 Subject: [PATCH 28/29] 27 28 --- 28-word-count.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/28-word-count.rb b/28-word-count.rb index c88b566..052d2dc 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -12,5 +12,5 @@ h[word] = 1 end end - + puts h From 34f3b1d4db9b114f5d597e21170e11cb1114d35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=98=8A=E9=9C=96?= Date: Wed, 30 Aug 2017 22:18:00 +0800 Subject: [PATCH 29/29] 29-todos.rb --- 29-todos.rb | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..54218f8 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -16,18 +16,27 @@ command = gets.chomp if command == "add" - print "请输入代办事项: " - # ... - elsif command == "remove" - print "请输入要删除的编号: " - # ... - elsif command == "save" - puts "存盘离开" - - # ... - break; - else - puts "看不懂,请再输入一次" - end -end + print "请输入代办事项: " + # ... + newTodo = gets.chomp + todos.push(newTodo) + elsif command == "remove" + print "请输入要删除的编号: " + # ... + index = gets.chomp.to_i + todos.pop(index) + elsif command == "save" + puts "存盘离开" + File.open("todos.txt", "w ") do |f| + todos.each do |line| + f << line + f << "\n" + end + end + # ... + break; + else + puts "看不懂,请再输入一次" + end + end