From 9521e2aa55486842d4467cfdcca8b2446ec033b1 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sat, 18 Apr 2020 23:12:10 +0800 Subject: [PATCH 01/30] 01 --- 01-hello.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..f0e0216 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,7 @@ # 题目: 输入名字,输出 "Hello, 名字" print "请输入你的名字,然后按 Enter: " -your_name = gets -# ... +your_name = gets -puts "(请替换成最后的答案)" \ No newline at end of file +puts "Hello, #{your_name}" From 5c4ddfef85b7b49682692bbb2045b7fae2609c9b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sat, 18 Apr 2020 23:54:02 +0800 Subject: [PATCH 02/30] 02 --- 02-variable.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..d94a652 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}" +puts "a \"应该\"是 2,现在是 #{a}" +puts "b \"应该\"是 1,现在是 #{b}" +# 这才是真正的学习了,什么叫 “ ’=’ 是赋值运算符(assignment operator),是指派右边的值到左边的变量” From 24b6f5431917e4d3c05cac5d080ea07b85ca2bb3 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 20 Apr 2020 22:56:17 +0800 Subject: [PATCH 03/30] 04 --- 03-triangle.rb | 4 ++-- 04-pizzas.rb | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..00e3c5e 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,6 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets -# ..... +c = a.to_f * b.to_f / 2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{c}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..d2c0cb4 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -6,7 +6,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -# ..... +a = pizzas.to_i +b = people.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{a/b} 片" +puts "还剩下几片: #{a%b} 片" From d2abca52ce5992cb8e189f2382a104d1148e132c Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:22:10 +0800 Subject: [PATCH 04/30] 05 --- 05-bmi.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..51be6c8 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,18 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +b = height.to_f/100 -puts "您的 BMI 是: _________" +a = weight.to_f/(b * b) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +if a < 18.5 + c = "过轻" +elsif a >= 24 + c = "过重" +else + c = "正常" +end + +puts "您的 BMI 是: #{a}" + +puts "您的 BMI 结果是: #{c}" From a1f3d0ba0dffd9d939219a3741f9af984b038d7f Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:31:09 +0800 Subject: [PATCH 05/30] 06 --- 06-interger-positive.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..c6ad3d6 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -4,7 +4,19 @@ print "请输入一个整数,然后按 Enter: " x = gets -# .... +if x.to_i > 0 + a = "正数" +elsif x.to_i < 0 + a = "负数" +else + a = "零" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x.to_i % 2 == 0 + b = "偶数" +else + b = "奇数" +end + +puts "这个数是#{a}" +puts "这个数是#{b}" From 1ed641d585cadc1372cae65e1516e18fcb319038 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:48:38 +0800 Subject: [PATCH 06/30] 07 --- 07-abcde.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..d16ac81 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,6 +17,22 @@ print "请输入一个整数z,然后按 Enter: " z = gets -# .... +if x.to_i < 0 + a = "A" +else + if y.to_i > 0 + if z.to_i > 0 + a = "B" + else + a = "C" + end + else + if z.to_i >0 + a = "D" + else + a = "E" + end + end +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是#{a}" From 870eadd3a2a26a6a121fcaf573a53c84a5528435 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 22:16:22 +0800 Subject: [PATCH 07/30] 08 --- 08-find-max.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..42bb2b3 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,18 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +if x > y + if x > z + c = "x" + else + c = "z" + end +else + if y > z + c = "y" + else y < z + c = "z" + end +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 #{c}" From 17793832e5e53bfdc66eab7ec058c126b884a47f Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 17:25:27 +0800 Subject: [PATCH 08/30] 09 --- 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 5c935c1d23146bca758dfdd7a0d3f0c97d89930b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:26:54 +0800 Subject: [PATCH 09/30] modified 08 --- 08-find-max.rb | 8 ++++---- 10-function.rb | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 42bb2b3..e80071b 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,16 +9,16 @@ print "请输入一个数字z,然后按 Enter: " z = gets -if x > y - if x > z +if x.to_f > y.to_f + if x.to_f > z.to_f c = "x" else c = "z" end else - if y > z + if y.to_f > z.to_f c = "y" - else y < z + else c = "z" end end diff --git a/10-function.rb b/10-function.rb index bb450fb..e677d23 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,19 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y + if x > z + c = "x" + else + c = "z" + end + else x < y + if y > z + c = "y" + else y < z + c = "z" + end + end end print "请输入一个数字x,然后按 Enter: " @@ -12,8 +25,7 @@ def find_max(x, y, z) print "请输入一个数字z,然后按 Enter: " z = gets -# .... answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From b54e770f983f7e6ed73e8b11bac2c550c91867c5 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:36:00 +0800 Subject: [PATCH 10/30] 10 --- 10-function.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/10-function.rb b/10-function.rb index e677d23..cb7faf5 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,16 +1,16 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) - if x > y - if x > z + if x.to_f > y.to_f + if x.to_f > z.to_f c = "x" else c = "z" end - else x < y - if y > z + else + if y.to_f > z.to_f c = "y" - else y < z + else c = "z" end end From 6286e719f76d2cad4111bdb82636f4574797993b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:59:04 +0800 Subject: [PATCH 11/30] modified 10 --- 10-function.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/10-function.rb b/10-function.rb index cb7faf5..e31148e 100644 --- a/10-function.rb +++ b/10-function.rb @@ -3,15 +3,15 @@ def find_max(x, y, z) if x.to_f > y.to_f if x.to_f > z.to_f - c = "x" + return "x" else - c = "z" + return "z" end else if y.to_f > z.to_f - c = "y" + return "y" else - c = "z" + return "z" end end end @@ -26,6 +26,7 @@ def find_max(x, y, z) z = gets + answer = find_max(x,y,z) puts "最大的数是 #{answer}" From ba6f9a2302712ac0a50b3f185004b5fed40f1b41 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 22:33:57 +0800 Subject: [PATCH 12/30] 11 --- 11-seven.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..3efc448 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,10 @@ i = 1 while ( i <= 100 ) - # .... + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file + +end From 302558fbb3123ca8c489650d9270600dd9fd0f4c Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 22:53:29 +0800 Subject: [PATCH 13/30] 12 --- 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 a3c67bf366250f6d0f173f7bd769992a989903b9 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Tue, 28 Apr 2020 23:56:59 +0800 Subject: [PATCH 14/30] 13 --- 13-nn.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..6ae12de 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,19 @@ print "请输入数字 N,然后按 Enter: " n = gets -# while ( ... ) -# while ( ...) -# -# end -# end +i = 0 + while ( i.to_i < n.to_i ) + + puts "#{i.to_i} x #{i.to_i} = #{i * i}" + puts "#{i.to_i} x #{i.to_i + 1} = #{i * (i + 1)}" + puts "#{i.to_i + 1} x #{i.to_i} = #{i * (i + 1)}" + + i += 1 + + while ( i.to_i == n.to_i ) + puts "#{i.to_i} x #{i.to_i} = #{i * i}" + break + end + + end From c4a8e4d6ef5cb2453318b46b7ce3fcbab1e2513a Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 00:03:54 +0800 Subject: [PATCH 15/30] if or while --- 13-nn.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/13-nn.rb b/13-nn.rb index 6ae12de..3be93e1 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,6 +1,7 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " + n = gets i = 0 From 0695b6b0330e5915113f7304a9ef29f2b252ee44 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 20:51:13 +0800 Subject: [PATCH 16/30] 14 --- 14-prime.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..bafd797 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,9 +1,21 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + + while i.to_i < n + a = n % i + if a.to_i == 0 + break + else + i += 1 + end + end + + a != 0 end + print "请输入数字 N,然后按 Enter: " n = gets From 61ac7fe6321c8fefd9cfc8e771347a2532a5d469 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 21:28:39 +0800 Subject: [PATCH 17/30] 15 --- 15-guess-number.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..cd15327 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,15 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" + if n.to_i < target + puts "太低了,再猜一次" + elsif n.to_i > target + puts "太高了,再猜一次" + end if n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end From d26dbac920a9a60aa35c33883b0750a3de73f7aa Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 22:11:34 +0800 Subject: [PATCH 18/30] modified 15 --- 15-guess-number.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index cd15327..0b4722c 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -1,5 +1,6 @@ # 题目 猜数字游戏:程序会先产生随机数,然后用户开始猜数字。程序会针对猜的数字回答太高、太低或猜中,猜中后程序就会终止。 + target = rand(100) while (true) @@ -10,9 +11,7 @@ puts "太低了,再猜一次" elsif n.to_i > target puts "太高了,再猜一次" - end - - if n.to_i == target + else n.to_i == target puts "恭喜猜中啦! " break end From a29c2056e92000de7ee1c06255e2264b2bd4e0dd Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 3 May 2020 21:49:32 +0800 Subject: [PATCH 19/30] 16 --- 16-array-sum.rb | 20 ++++++++++++++++++-- 17-array-stats.rb | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..7f0bce0 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,27 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + i = 1 + x = array[0] + + while i < array.length + if x < array[i] + x = array[i] + end + i += 1 + end + + return x + end +# array.max + +# array.each do |i| +# if x < i +# x = i + 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..3b12e19 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -17,4 +17,4 @@ puts "总和是 _____" puts "平均是 _____" puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "最小值是 _____" From fd67f7353408377a97579d875defb97be77488c5 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 3 May 2020 23:40:31 +0800 Subject: [PATCH 20/30] 17 --- 17-array-stats.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 3b12e19..b633e5d 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,17 @@ end end + i = 0 + x = 0 + while i < arr.size + x = x + arr[i] + i+=1 + end + + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" +puts "总和是 #{x}" +puts "平均是 #{(x.to_f/arr.size)}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" From de8165680cb58df21df0fa7a9de8d8702279a3bf Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 4 May 2020 23:09:30 +0800 Subject: [PATCH 21/30] 18 --- 18-square.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..7a50b94 100644 --- a/18-square.rb +++ b/18-square.rb @@ -5,6 +5,15 @@ print "请输入数字 N,然后按 Enter: " n = gets -# ... +i = 0 -puts arr.to_s \ No newline at end of file +while i < n.to_i + if n == "\n" + break + else + arr << i * i + i += 1 + end +end + +puts arr.to_s From b3dd07ecc6a5346ae39c03708d920e3985bd35be Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Tue, 5 May 2020 22:31:23 +0800 Subject: [PATCH 22/30] 19 --- 19-filter.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..8370ce9 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,17 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + array = [] + + for i in arr + if i % 2 == 0 + array << i + end + end + + puts array.to_s 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 c57943963b140d2504b45d08d40fc08081929abb Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Tue, 5 May 2020 22:58:06 +0800 Subject: [PATCH 23/30] 20 --- 20-sorting.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..850e63e 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,18 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + array = [] + + arr.each do |i| + if i % 2 == 0 + array << i + end + end + + puts array.sort.uniq.to_s 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 38b948cf82beaef133b9cc79a5caee3f998a43f4 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Thu, 7 May 2020 23:38:40 +0800 Subject: [PATCH 24/30] 21 --- 21-selection-sort.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..70170ee 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,28 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + + j = 0 + i = 1 + while j < arr.size + while i < arr.size + if arr[j] > arr[i] + min = arr[j] + arr[j] = arr[i] + arr[i] = min + end + i += 1 + end + i = j + 1 + j += 1 + end + + puts arr.to_s + 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 8601598acea862f68b2e88caf0c6e365f283557e Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Fri, 8 May 2020 21:52:10 +0800 Subject: [PATCH 25/30] 22 --- 22-missing.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..810d25b 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,12 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + arr.each do |i| + array.delete(i) + end + + puts array.to_s end answer = find_missing( [2,2,1,5,8,4] ) From 63828ad2bf1652825f850073818de6115868dc71 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Fri, 8 May 2020 23:32:02 +0800 Subject: [PATCH 26/30] 23 --- 23-hash-max.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..322e72c 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,9 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + arr = hash.values + x = arr.max + return hash.key(x) end h = { @@ -15,5 +17,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 52923c67746965510f09bbd77b4ded1477cbb772 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Thu, 14 May 2020 18:02:51 +0800 Subject: [PATCH 27/30] 24 --- 24-hash-even.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..91aef13 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,15 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + arr = [] + + hash.each do |key, value| + if value % 2 == 0 + arr << key + end + end + return arr + #return arr * """" end h = { @@ -17,5 +23,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 71538c171d6562d59a5bfaa22b9966ce5d677ce0 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Tue, 19 May 2020 18:35:08 +0800 Subject: [PATCH 28/30] 25 --- 25-hash-count.rb | 5 ++--- 26-hash-filter.rb | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..35bff81 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,15 +4,14 @@ def count(arr) h = {} arr.each do |i| - # ... + h[i] = arr.count(i) end return h # 回传一个 hash end -arr = ["a", "d", "d", "c", "b", "c", "c", "c", "d", "d", "e", "e", "e", "d", "a", "c", "e", "a", "d", "e"] +arr = ["a", "d", "d", "c","b", "c", "c", "c", "d", "d", "e", "e", "e", "d", "a", "c", "e", "a", "d", "e"] 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..804a560 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,9 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... -puts "所有成年人,并由小到大: _________" + +puts "所有成年人,并由小到大: #{answer}" # 答案应该是 #[ From 73f9d7977e0d95abe589a0d91c74aea8ff755b69 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 20 May 2020 01:21:35 +0800 Subject: [PATCH 29/30] 26 --- 26-hash-filter.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 804a560..8b54c22 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,24 @@ { "name" => "Vincent", "age" => 6 }, ] +i = 0 +j = 1 +while i < arr.size + while j < arr.size + if arr[i]["age"] > arr[j]["age"] + min = arr[j] + arr[j] = arr[i] + arr[i] = min + end + j += 1 + end + i += 1 + j = i + 1 +end -puts "所有成年人,并由小到大: #{answer}" + +puts "所有成年人,并由小到大: #{arr}" # 答案应该是 #[ From 356871ee4d89b27de564a6be56f6c2d85f0f82c2 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 20 May 2020 13:29:04 +0800 Subject: [PATCH 30/30] 27 --- 27-class.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..25c33c4 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,10 @@ class Person - # ... + attr_accessor :first_name + attr_accessor :last_name + + def greet + puts "Hello, #{first_name} #{last_name}" + end end p1 = Person.new @@ -11,6 +16,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - -