From 20a2872d37a709fcd053ae5dfe3e2b15be9b4241 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 1 Sep 2017 23:01:20 +0800 Subject: [PATCH 01/29] hello #{} --- 01-hello.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..5d9711b 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,8 @@ # 题目: 输入名字,输出 "Hello, 名字" -print "请输入你的名字,然后按 Enter: " +print '请输入你的名字,然后按 Enter: ' your_name = gets # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello, #{your_name} " From dc7d53e66b4f9dbed15bc19946ec46269ac7e7d0 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 1 Sep 2017 23:09:01 +0800 Subject: [PATCH 02/29] add c to change --- 02-variable.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..901b6e1 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -11,3 +11,9 @@ puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" +c = a +a = b +b = c + +puts "a 现在是 #{a}" +puts "b 现在是 #{b}" From bafb221cbc685701363d03a90f68456cb6ce6d25 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 1 Sep 2017 23:26:07 +0800 Subject: [PATCH 03/29] to_i --- 03-triangle.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..1d7e8ce 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -1,11 +1,11 @@ # 题目: 使用者输入直角三角形的宽和高,输出三角形的面积 -print "请输入直角三角形的高,然后按 Enter: " +print '请输入直角三角形的高,然后按 Enter: ' a = gets -print "请输入直角三角形的底边,然后按 Enter: " +print '请输入直角三角形的底边,然后按 Enter: ' b = gets # ..... - -puts "直角三角形的面积是: _________" \ No newline at end of file +c = (a.to_i * b.to_i) / 2 +puts "直角三角形的面积是: #{c} " From d591eca6e3a947a14fcd10d2f17495d1c6bd2d7a Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 1 Sep 2017 23:29:46 +0800 Subject: [PATCH 04/29] to_i and % --- 04-pizzas.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..8c220df 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -1,12 +1,14 @@ # 题目: 输入有多少片比萨饼和多少人,输出每人可以分到几片,以及剩下几片 -print "请输入有多少片比萨饼,然后按 Enter: " +print '请输入有多少片比萨饼,然后按 Enter: ' pizzas = gets -print "请输入有多少人要吃,然后按 Enter: " +print '请输入有多少人要吃,然后按 Enter: ' people = gets # ..... +average = pizzas.to_i / people.to_i +left = pizzas.to_i % people.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{average} 片" +puts "还剩下几片: #{left} 片" From dc08f61c5ef26b031caa8a8bbdc47f337a8c6d43 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 1 Sep 2017 23:48:02 +0800 Subject: [PATCH 05/29] a**2 = a*a and a.round(2) --- 05-bmi.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..24efe3a 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -4,14 +4,21 @@ # 如果 BMI >= 24,显示过重 # 如果 BMI 介于 18.5 ~ 24,显示正常 -print "请输入您的体重(公斤),然后按 Enter: " +print '请输入您的体重(公斤),然后按 Enter: ' weight = gets -print "请输入您的身高(厘米),然后按 Enter: " +print '请输入您的身高(厘米),然后按 Enter: ' height = gets # ..... +bmi = weight.to_f / (height.to_f * 0.01)**2 -puts "您的 BMI 是: _________" +puts "您的 BMI 是: #{bmi.round(2)}" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +if bmi < 18.5 + puts '您的 BMI 结果是: 过轻' +elsif bmi >= 24 + puts '您的 BMI 结果是: 过重' +else + puts '您的 BMI 结果是: 正常' +end From 51403d6067f5004667825b6b0732affd0629743b Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sat, 2 Sep 2017 23:26:56 +0800 Subject: [PATCH 06/29] == if else --- 06-interger-positive.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..e5b64c6 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -1,10 +1,21 @@ # 题目: 输入一个数字 x,请判断是否正数、零或负数,以及是不是偶数 - -print "请输入一个整数,然后按 Enter: " +print '请输入一个整数,然后按 Enter: ' x = gets # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +( +if x.to_i > 0 + puts '这个数是正数' + if x.to_i.even? + puts '这个数是偶数' + else + puts '这个数是奇数' + end +elsif x.to_i == 0 + puts '这个数是零' +else + puts '这个数是负数' +end +) From ab70cdd3f0940bbd945ca975202473d3d126fc89 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sat, 2 Sep 2017 23:52:09 +0800 Subject: [PATCH 07/29] if else if elseif else end end --- 07-abcde.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..8c244ff 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -8,15 +8,28 @@ # 当 z > 0 输出 "D" # 当 z < 0 输出 "E" -print "请输入一个整数x,然后按 Enter: " +print '请输入一个整数x,然后按 Enter: ' x = gets -print "请输入一个整数y,然后按 Enter: " +print '请输入一个整数y,然后按 Enter: ' y = gets -print "请输入一个整数z,然后按 Enter: " +print '请输入一个整数z,然后按 Enter: ' z = gets # .... +if x.to_i < 0 + puts 'A' +else + if (x.to_i > 0) && (y.to_i > 0) && (z.to_i > 0) + puts 'B' + elsif (x.to_i > 0) && (y.to_i > 0) && z.to_i < 0 + puts 'C' + else + puts 'D' if (y.to_i < 0) && (z.to_i > 0) + puts 'E' if (y.to_i < 0) && (z.to_i < 0) -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +end + + puts '0' if x.to_i == 0 +end From 3287145fc7736d37bb24b57039f7115bb0c4b7b1 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 21:42:36 +0800 Subject: [PATCH 08/29] 08 if elsif else && --- 08-find-max.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..f2c16b9 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -1,14 +1,30 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 -print "请输入一个数字x,然后按 Enter: " -x = gets +print '请输入一个数字x,然后按 Enter: ' +x = gets.to_i -print "请输入一个数字y,然后按 Enter: " -y = gets +print '请输入一个数字y,然后按 Enter: ' +y = gets.to_i -print "请输入一个数字z,然后按 Enter: " -z = gets +print '请输入一个数字z,然后按 Enter: ' +z = gets.to_i # .... +# +# if a <= 0 && b >= 0 +# puts '最大的数是y' +# elsif a >= 0 && c >= 0 +# puts '最大的数是 x' +# else +# puts '最大的数是z' +# end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +if x >= y && x >= z + puts '最大的数是 x' + +elsif y >= x && y >= z + puts '最大的数是y' +else + puts '最大的数是z' + +end From a2f3df27c77d654de671f84d8bad7432427abcf3 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 21:48:09 +0800 Subject: [PATCH 09/29] 09 return def --- 09-function.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..2be399b 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + (a * b) / 2 end -print "请输入直角三角形的高,然后按 Enter: " -a = gets +print '请输入直角三角形的高,然后按 Enter: ' +a = gets.to_f -print "请输入直角三角形的底边,然后按 Enter: " -b = gets +print '请输入直角三角形的底边,然后按 Enter: ' +b = gets.to_f -answer = calculate_area(a,b) +answer = calculate_area(a, b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From d5718c7dd2da9520b4b3313b1e690e11b5713e00 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 21:56:43 +0800 Subject: [PATCH 10/29] 10 def --- 10-function.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..f0d7d17 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 && x >= z + 'x' + elsif y >= x && y >= z + 'y' + else + 'z' + end end -print "请输入一个数字x,然后按 Enter: " -x = gets +print '请输入一个数字x,然后按 Enter: ' +x = gets.to_f -print "请输入一个数字y,然后按 Enter: " -y = gets +print '请输入一个数字y,然后按 Enter: ' +y = gets.to_f -print "请输入一个数字z,然后按 Enter: " -z = gets +print '请输入一个数字z,然后按 Enter: ' +z = gets.to_f # .... -answer = find_max(x,y,z) +answer = find_max(x, y, z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From cd970e5c2ffaf8221a2689620a30d677635296f7 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 22:01:43 +0800 Subject: [PATCH 11/29] 11 while if --- 11-seven.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..be4a0b8 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -1,9 +1,7 @@ # 题目: 列出 1 到 100 之间,所有 7 的倍数 i = 1 -while ( i <= 100 ) - - # .... - - i+=1 -end \ No newline at end of file +while i <= 100 + puts i if i % 7 == 0 + i += 1 +end From 06868853c63595205973727c447ab898cc4d3fa7 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 22:14:58 +0800 Subject: [PATCH 12/29] 12 %2 == even --- 12-sum-even.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..ac8eed4 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -3,11 +3,9 @@ i = 1 total = 0 -while ( i <= 100 ) - - # .... - - i+=1 +while i <= 100 + total += i if i.even? + i += 1 end -puts total \ No newline at end of file +puts total From 095f34b4346d0caca3ffb3da378b8a612d55c6ab Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 3 Sep 2017 23:11:24 +0800 Subject: [PATCH 13/29] 13 while end y = 0 --- 13-nn.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..aa6529c 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,16 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 -print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end +print '请输入数字 N,然后按 Enter: ' +n = gets.to_i +x = 0 +y = 0 +while x <= n + while y <= n + nn = x.to_i * y.to_i + puts "#{x} * #{y} = #{nn}" + y += 1 + end + x += 1 + y = 0 +end From b34112797a9e0a1f3d1a1d862ee83217c34d4d9a Mon Sep 17 00:00:00 2001 From: rockyjia Date: Mon, 4 Sep 2017 23:09:07 +0800 Subject: [PATCH 14/29] 14 while end true --- 14-prime.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..9d342d4 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,14 +1,19 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + x = 2 + while x <= (n / 2) + return false if n % x == 0 + x += 1 + end + true end -print "请输入数字 N,然后按 Enter: " -n = gets +print '请输入数字 N,然后按 Enter: ' +n = gets.to_f if is_prime(n.to_i) - puts "这是质数" + puts '这是质数' else - puts "这不是质数" + puts '这不是质数' end From 9fc4a500277f8854b6026c0ebd867192cf17beaa Mon Sep 17 00:00:00 2001 From: rockyjia Date: Mon, 4 Sep 2017 23:17:00 +0800 Subject: [PATCH 15/29] 15 elsif else --- 15-guess-number.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..9f3ba59 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -2,16 +2,21 @@ target = rand(100) -while (true) - print "请猜一个 0~99 的数字 N,然后按 Enter: " - n = gets - - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" +loop do + print '请猜一个 0~99 的数字 N,然后按 Enter: ' + n = gets.to_i + # if n > target + # puts '太高了,再猜一次' + # elsif n < target + # puts '太低了,再猜一次' + # end if n.to_i == target - puts "恭喜猜中啦! " + puts '恭喜猜中啦! ' break + elsif n > target + puts '太高了,再猜一次' + else + puts '太低了,再猜一次' end - -end \ No newline at end of file +end From 632857c4dca5ae23c8da9da288ce31116c6e5bd5 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Tue, 5 Sep 2017 22:09:48 +0800 Subject: [PATCH 16/29] 16 for end --- 16-array-sum.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..4cf3456 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,13 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + x = 0 + for i in array + x = i if x < i + end + x end - arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - From 01e56198b4871c221025cf1126d3f38baa635855 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Tue, 5 Sep 2017 23:03:46 +0800 Subject: [PATCH 17/29] 17 def sum --- 17-array-stats.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..3c0f94c 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -2,8 +2,8 @@ arr = [] -while (true) - print "请输入数字,结束请直接按 Enter: " +loop do + print '请输入数字,结束请直接按 Enter: ' user_input = gets if user_input == "\n" break @@ -14,7 +14,20 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +def sum(array) + x = 0 + array.each do |i| + x += i + end + x +end + +def average(array) + a = sum(array) + a / array.size +end + +puts "总和是 #{sum(arr)} " +puts "平均是 #{average(arr)}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" From cd2f0b2156f07dd1fcefba5461ec6f4d7f8f42ca Mon Sep 17 00:00:00 2001 From: rockyjia Date: Tue, 5 Sep 2017 23:29:09 +0800 Subject: [PATCH 18/29] 18 while arr.push --- 18-square.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..2e34fa4 100644 --- a/18-square.rb +++ b/18-square.rb @@ -2,9 +2,13 @@ arr = [] -print "请输入数字 N,然后按 Enter: " -n = gets +print '请输入数字 N,然后按 Enter: ' +n = gets.to_i +x = 0 -# ... +while x <= n - 1 + arr.push(x**2) + x += 1 +end -puts arr.to_s \ No newline at end of file +puts arr.to_s From d5cb4dac1d636c7858df80452a06da9fe1d37799 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Tue, 5 Sep 2017 23:50:39 +0800 Subject: [PATCH 19/29] 19 arr.each push --- 19-filter.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..738eb71 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,13 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + arreven = [] + arr.each do |i| + arreven.push(i) if i.even? + end + arreven end -arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +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 a572f09e4f7a962bb2e3535c3923c2176a629796 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Wed, 6 Sep 2017 23:20:04 +0800 Subject: [PATCH 20/29] 20 arr.uniq.sort --- 20-sorting.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..5a64a45 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,15 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... -end + arreven = [] + arr.each do |i| + arreven.push(i) if i.even? + end -arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] + # arreven.sort.uniq + arreven.uniq.sort +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).to_s # 应该是 [42, 46, 68, 86] From 4a40aadd3ce035c8751b5be2a0f069c57a8de058 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Thu, 7 Sep 2017 22:36:15 +0800 Subject: [PATCH 21/29] 21 times do --- 21-selection-sort.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..6c4ca13 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -1,12 +1,34 @@ # 给定一数组内含数字,请实作选择排序法进行排序。 # https://zh.wikipedia.org/wiki/选择排序 +# def selection_sort(arr) +# (0..arr.size - 1).each do |i| +# min = arr[i] +# k = i +# (i + 1..arr.size - 1).each do |j| +# if arr[j] < min +# min = arr[j] +# k = j +# end +# end +# arr[k] = arr[i] +# arr[i] = min +# end +# arr +# end + def selection_sort(arr) - #... + sorted = [] + arr.size.times do |_i| + min = arr.min + sorted << min + arr.delete_at(arr.index(min)) + end + sorted 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 8200edf2aff13e02f8a74fa4a85099f6ed64bbca Mon Sep 17 00:00:00 2001 From: rockyjia Date: Thu, 7 Sep 2017 23:15:54 +0800 Subject: [PATCH 22/29] 22 []-[] --- 22-missing.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..45f69ff 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,9 +1,17 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 +# def find_missing(arr) +# missing = [] +# (0..9).each do |i| +# missing << i unless arr.include?(i) +# end +# missing +# end + def find_missing(arr) - # ... + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - arr.uniq.sort end -answer = find_missing( [2,2,1,5,8,4] ) +answer = find_missing([2, 2, 1, 5, 8, 4]) puts answer.to_s # 应该是 [0,3,6,7,9] From 38b30f3016d409dbc145d6509fb1dbc8aaf74c12 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 8 Sep 2017 23:26:45 +0800 Subject: [PATCH 23/29] 23 hash.max_by --- 23-hash-max.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..0141ba7 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,19 +1,17 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + hash.max_by { |_k, v| v } end h = { - "a" => 71, - "b" => 38, - "c" => 21, - "d" => 80, - "e" => 10 + 'a' => 71, + 'b' => 38, + 'c' => 21, + 'd' => 80, + 'e' => 10 } answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 5b9da5858effbe0f507133dfc9d79efd1aa9ae69 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Fri, 8 Sep 2017 23:42:57 +0800 Subject: [PATCH 24/29] 24 hash.values.each hash.key(v) --- 24-hash-even.rb | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..74538ea 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,21 +1,26 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + even = [] + hash.values.each do |v| + even << hash.key(v) if v.even? + end + # arr = [] + # even.each do |k| + # arr << hash.key(k) + # end + # arr + even end h = { - "a" => 71, - "b" => 38, - "c" => 21, - "d" => 80, - "e" => 10 + 'a' => 71, + 'b' => 38, + 'c' => 21, + 'd' => 80, + 'e' => 10 } answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 4c995ff0fbea59d616b59be98b3b41d9a42a5509 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sat, 9 Sep 2017 23:37:39 +0800 Subject: [PATCH 25/29] 25 h[i] = arr.count(i) --- 25-hash-count.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..e335897 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 + 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 = %w[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} - From 30ed9efc3b8e296cf598201810e4ddfd3f41aae1 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 10 Sep 2017 00:11:00 +0800 Subject: [PATCH 26/29] 26 push() i[] --- 26-hash-filter.rb | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..37c65ba 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -1,20 +1,23 @@ # 给定一个数组包含 Hash,请过滤和排序 arr = [ - { "name" => "Peter", "age" => 30 }, - { "name" => "John", "age" => 15 }, - { "name" => "David", "age" => 45 }, - { "name" => "Steven", "age" => 22 }, - { "name" => "Vincent", "age" => 6 }, + { 'name' => 'Peter', 'age' => 30 }, + { 'name' => 'John', 'age' => 15 }, + { 'name' => 'David', 'age' => 45 }, + { 'name' => 'Steven', 'age' => 22 }, + { 'name' => 'Vincent', 'age' => 6 } ] -# .... - -puts "所有成年人,并由小到大: _________" +answer = [] +arr.each do |i| + answer.push(i) if i['age'] >= 18 +end +answer.sort_by { |k| k['age'] } +puts "所有成年人,并由小到大: #{answer}" # 答案应该是 -#[ +# [ # { "name" => "Steven", "age" => 22 }, # { "name" => "Peter", "age" => 30 }, # { "name" => "David", "age" => 45 } -#] +# ] From 3804cabfcca6b2b4b2f64052fff1cee2867db1e9 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Sun, 10 Sep 2017 22:45:19 +0800 Subject: [PATCH 27/29] 27 attr_accessor --- 27-class.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..5459ced 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,16 +1,18 @@ class Person - # ... + attr_accessor :first_name, :last_name + def greet + "Hello, #{@first_name} #{@last_name}" + end end p1 = Person.new -p1.first_name = "Peter" -p1.last_name = "Wang" +p1.first_name = 'Peter' +p1.last_name = 'Wang' p1.greet # 输出 "Hello, Peter Wang" p2 = Person.new -p2.first_name = "William" -p2.last_name = "Zhang" +p2.first_name = 'William' +p2.last_name = 'Zhang' p2.greet # 输出 "Hello, William Zhang" - - +# https://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby From ada88b21b671c536031c86fef93785066bb69514 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Mon, 11 Sep 2017 23:29:42 +0800 Subject: [PATCH 28/29] 28 doc.gsub.split --- 28-word-count.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..18b74b1 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -1,5 +1,16 @@ # 请打开 wordcount.txt,计算每个单字出现的次数 -doc = File.read("wordcount.txt") +doc = File.read('wordcount.txt') +# word = doc.split(/\r\n|\n/) +word = doc.downcase.gsub(/(n['’]t\b)|\p{P}/, '\1').split(/\s+/).reject(&:empty?) +def count(word) + h = {} + word.each do |i| + h[i] = word.count(i) + end + h # 回传一个 hash +end -# ... +answer = count(word).sort + +puts answer From af2c55dd5e2796b14f6966b31acb1b2f90fb26e8 Mon Sep 17 00:00:00 2001 From: rockyjia Date: Mon, 11 Sep 2017 23:56:44 +0800 Subject: [PATCH 29/29] 29 gets.chomp --- 29-todos.rb | 43 ++++++++++++++++++++++++++++--------------- todos.txt | 3 +++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..23c2ae6 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -1,6 +1,6 @@ # 简易 Todo 代办事项应用 -text = File.read("todos.txt") +text = File.read('todos.txt') todos = [] text.each_line do |line| @@ -11,23 +11,36 @@ puts "#{index}: #{todo}" end -while (true) - print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " +loop do + print '请输入指令 1. add 2. remove 3. save,然后按 Enter: ' command = gets.chomp - if command == "add" - print "请输入代办事项: " - # ... - elsif command == "remove" - print "请输入要删除的编号: " - # ... - elsif command == "save" - puts "存盘离开" + if command == 'add' + print '请输入代办事项: ' + add = gets.chomp + todos << add + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end - # ... - break; + elsif command == 'remove' + print '请输入要删除的编号: ' + remove = gets.to_i + todos.delete_at(remove) + 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 |i| + f << i + f << "\n" + end + end + break else - puts "看不懂,请再输入一次" + puts '看不懂,请再输入一次' end end - diff --git a/todos.txt b/todos.txt index 4757e85..d27e96f 100644 --- a/todos.txt +++ b/todos.txt @@ -2,3 +2,6 @@ Buy book Go Shopping Walk Gogo +3333 +4444 +23456