From 55bee6eea1fc2be0ba97de497e36dc8562adafa7 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 11:20:43 +0800 Subject: [PATCH 01/33] have done 01 and 02 --- 01-hello.rb | 7 ++----- 02-variable.rb | 5 ++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..0380a8f 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,5 @@ # 题目: 输入名字,输出 "Hello, 名字" -print "请输入你的名字,然后按 Enter: " +print "请输入你的\"名字\",然后按 Enter: " your_name = gets - -# ... - -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hi,#{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..88f9260 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -7,7 +7,10 @@ puts "b 是 #{b}" # ... +c = a +a = b +b = c + puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - From 895da192741db157ca1d124c0fd61bc32d465c64 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 11:24:07 +0800 Subject: [PATCH 02/33] 03 --- 03-triangle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..fbc7f90 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -5,7 +5,7 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets - +c = a.to_i * b.to_i # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: ___#{c}______" From 2b8d717c75a0ac5da2ff4aac243ad76e214e2ee3 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 11:29:05 +0800 Subject: [PATCH 03/33] 04 --- 04-pizzas.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..ec17dd8 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -5,8 +5,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets - +left = pizzas.to_i - (pizzas.to_i / people.to_i)*people.to_i # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{ pizzas.to_i / people.to_i}片" +puts "还剩下几片: #{left}片" From 0441f3e427307da79d5f9ebf621a21cd83d8d0a0 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 12:46:52 +0800 Subject: [PATCH 04/33] 04 --- 04-pizzas.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index ec17dd8..3918da7 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -5,8 +5,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -left = pizzas.to_i - (pizzas.to_i / people.to_i)*people.to_i -# ..... +#left = pizzas.to_i - (pizzas.to_i / people.to_i)*people.to_i +left = pizzas.to_i % people.to_i puts "每人可分得几片: #{ pizzas.to_i / people.to_i}片" puts "还剩下几片: #{left}片" From 456715c53baff3b6299f9b586b61ed1bfbe12e5d Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 14:51:59 +0800 Subject: [PATCH 05/33] 05 --- 05-bmi.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..eb262d0 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,17 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +bmi = weight.to_f/(height.to_f*height.to_f) -puts "您的 BMI 是: _________" +if bmi>=24.0 + result = "过重" +elsif bmi<18.5 + result = "过轻" +else + result = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file + +puts "您的 BMI 是: #{bmi}" + +puts "您的 BMI 结果是: #{result}" From 43b8f57695a5fe21de56d39558c1f1600bd21de3 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 15:02:16 +0800 Subject: [PATCH 06/33] 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..5bdb2d6 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -2,9 +2,22 @@ print "请输入一个整数,然后按 Enter: " -x = gets +y = (gets).to_i +puts y +if y == 0 + x = "0" +elsif y > 0 + x = "正数" +else + x = "负数" +end +if y % 2 == 0 + z = "偶数" +else + z = "奇数" +end # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +puts "这个数是#{x}" +puts "这个数是#{z} (偶数或奇数)" From 99798a96f3141dd77326c75bf2b51768c06cd23c Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 15:16:14 +0800 Subject: [PATCH 07/33] 07 --- 07-abcde.rb | 24 +++++++++++++++++++----- 08-find-max.rb | 16 +++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..bde93f5 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,28 @@ # 当 z < 0 输出 "E" print "请输入一个整数x,然后按 Enter: " -x = gets +x = (gets).to_i print "请输入一个整数y,然后按 Enter: " -y = gets +y = (gets).to_i print "请输入一个整数z,然后按 Enter: " -z = gets +z = (gets).to_i -# .... +if x < 0 + result = "A" +elsif x > 0 + if y > 0 && z > 0 + result = "B" + elsif y >0 && z < 0 + result = "C" + elsif y < 0 && z > 0 + result = "D" + else + result = "E" + end +else + result = "0" +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是#{result}" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..fbfaa1c 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -1,14 +1,20 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 print "请输入一个数字x,然后按 Enter: " -x = gets +x = (gets).to_i print "请输入一个数字y,然后按 Enter: " -y = gets +y = (gets).to_i print "请输入一个数字z,然后按 Enter: " -z = gets +z = (gets).to_i -# .... +if x > y && x > z + result = "x" +elsif y > x && y > z + result = "y" +else + result = "z" +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 ________(x或y或z)" From 369ffefea0840053e4aee92ddf432690b0184a62 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 15:33:47 +0800 Subject: [PATCH 08/33] 08 --- 08-find-max.rb | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index fbfaa1c..a359a1f 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,12 +9,32 @@ print "请输入一个数字z,然后按 Enter: " z = (gets).to_i -if x > y && x > z - result = "x" -elsif y > x && y > z - result = "y" +if x > y + if x > z + result = "x" + elsif x < z + result = "z" + else + result = "x和z 并列最大" + end +elsif x = y + if x > z + result = "x和y 并列最大" + elsif x < z + result = "z" + else + result = "x y z 一样大" + end else - result = "z" + if x >= z + result = "Y" + elsif y > z + result = "Y" + elsif y < z + result = "z" + else + result = "y和z 并列最大" + end end -puts "最大的数是 ________(x或y或z)" +puts "最大的数是 #{result} (x或y或z)" From 888e78d7edc017d75a4d07f7a8e0d7feca65ec3c Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:18:06 +0800 Subject: [PATCH 09/33] 03 --- 03-triangle.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fbc7f90..d38648a 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -1,11 +1,11 @@ # 题目: 使用者输入直角三角形的宽和高,输出三角形的面积 print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = (gets).to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets -c = a.to_i * b.to_i +b = (gets).to_i +c = (a*b)/2 # ..... puts "直角三角形的面积是: ___#{c}______" From 0a5f6f4d7509b8009a5838033464397d0bc69efe Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:22:03 +0800 Subject: [PATCH 10/33] 09 --- 09-function.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..2d9b462 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + return (a*b)/2 end print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = (gets).to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets +b = (gets).to_i answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From edb43aead78f6c65f5c2978534eb5b8cd7f5ad1a Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:29:25 +0800 Subject: [PATCH 11/33] 10 --- 10-function.rb | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..584c44c 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,46 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y + if x > z + return result = "x" + elsif x < z + return result = "z" + else + return result = "x和z 并列最大" + end + elsif x = y + if x > z + return result = "x和y 并列最大" + elsif x < z + return result = "z" + else + return result = "x y z 一样大" + end + else + if x >= z + return result = "Y" + elsif y > z + return result = "Y" + elsif y < z + return result = "z" + else + result = "y和z 并列最大" + end + end end print "请输入一个数字x,然后按 Enter: " -x = gets +x = (gets).to_f print "请输入一个数字y,然后按 Enter: " -y = gets +y = (gets).to_f print "请输入一个数字z,然后按 Enter: " -z = gets +z = (gets).to_f # .... answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From e2f47f09d85ab92f95f4fb78a2f17ae977ba0a86 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:33:27 +0800 Subject: [PATCH 12/33] 11 --- 11-seven.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..b308999 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,8 @@ i = 1 while ( i <= 100 ) - # .... - + if i%7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end From 72e68c5ade7ceb1948dc5a1383960b6afca860fe Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:40:39 +0800 Subject: [PATCH 13/33] 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 f18675dd6856a5cffbd4757c811eb0d7a2d9155e Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 16:49:36 +0800 Subject: [PATCH 14/33] 13 --- 13-nn.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..71b76cb 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,12 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end - +n = (gets).to_i +i = 1 +while ( i < (n+1) ) + # while ( ...) + # + # end + puts "#{n} * #{i} = #{i*n}" + i += 1 +end From a72816dcf4c590a9ddbf70acb8edd77ed86e83fd Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 17:59:33 +0800 Subject: [PATCH 15/33] 14 --- 14-prime.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..c3b851d 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,17 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + r = true + while i < n + + if n % i == 0 + r = false + break + end + i +=1 + end + return r end print "请输入数字 N,然后按 Enter: " From 39171a3423749a2ddf41eb9efc7b058cf661565d Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 18:09:58 +0800 Subject: [PATCH 16/33] 15 --- 15-guess-number.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..aa2c5f8 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,18 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - - if n.to_i == target + if n.to_i < target + puts "低了,再猜一次" + elsif n.to_i > target + puts "太高了,再猜一次" + else puts "恭喜猜中啦! " break end -end \ No newline at end of file + # if n.to_i == target + # puts "恭喜猜中啦! " + # break + # end + +end From c6737e2023b4dc9b79c3a69c713df008339889aa Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 20:02:08 +0800 Subject: [PATCH 17/33] max --- 16-array-sum.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..ab06d76 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,16 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = array[0] + array.each do |i| + if m <= i + m = i + end + end + return m end -arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] +arr = [8, 12, 36, 93, 9, 75] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - From e0568b7cba4a0f0fd1951f94e15da1a515ad53a2 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 20:02:24 +0800 Subject: [PATCH 18/33] 15max --- 16-array-sum.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index ab06d76..49d74ff 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -9,7 +9,7 @@ def find_max(array) end return m end - +# 直接用max也行。 arr = [8, 12, 36, 93, 9, 75] max = find_max(arr) From b45213e1fe57deeee9abd106d8ecc05883278577 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 20:26:18 +0800 Subject: [PATCH 19/33] 17 --- 17-array-stats.rb | 25 +++++++++++++++++++++---- 18-square.rb | 8 ++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..7601f66 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,26 @@ end end +total = 0 +max = arr[0] +min = arr[0] + +arr.each do |i| + total = total + i + if max <= i + max = i + end + + if min >=i + min = i + end +end + +avg = total.to_f / arr.size + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是#{total}" +puts "平均是#{avg}" +puts "最大值是#{max}" +puts "最小值是#{min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..b1ef4be 100644 --- a/18-square.rb +++ b/18-square.rb @@ -4,7 +4,11 @@ print "请输入数字 N,然后按 Enter: " n = gets - +i = 0 +while i < n + arr << i**2 + i++ +end # ... -puts arr.to_s \ No newline at end of file +puts arr.to_s From d630aa0ccec15e8dfd0d0a2245ffcf2ef0e55d21 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 20:30:26 +0800 Subject: [PATCH 20/33] 18 --- 18-square.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/18-square.rb b/18-square.rb index b1ef4be..d55e9e8 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,11 +3,11 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = (gets).to_i i = 0 while i < n - arr << i**2 - i++ + arr << i*i + i +=1 end # ... From 89faa1c08cf083ae2dd1a44b4450c3d8802c10fa Mon Sep 17 00:00:00 2001 From: chentianwei Date: Mon, 1 Jan 2018 20:33:56 +0800 Subject: [PATCH 21/33] 19 --- 19-filter.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..19b76ca 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + even_arr = [] + arr.each do |i| + if i % 2 == 0 + even_arr.push(i) + end + end + even_arr 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 e3e223392de4856c06ba048542297ab69d65a099 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Tue, 2 Jan 2018 20:11:57 +0800 Subject: [PATCH 22/33] 20 --- 20-sorting.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..d5bc776 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + even_arr = [] + arr.each do |i| + if i % 2 == 0 + even_arr.push(i) + end + end + even_arr.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 f046f2c02bbd7f6d5a3cbf4e8ef64009a3447899 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Wed, 3 Jan 2018 09:56:47 +0800 Subject: [PATCH 23/33] 21 --- 21-selection-sort.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..de0312b 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,32 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + temp = [] + i = 0 + l = arr.size - 1 + # 双循环:外面的循环,是按顺序从前向后依次提取值,直到倒数第二个结束。 + # 嵌套的循环,是让外循环提取的值和后续的值进行对比。 + # 所以两个循环的次数都是n-1 + while i < l + min = i + j = i + 1 + while j < arr.size + if arr[min] > arr[j] + min = j + end + j += 1 + end + temp = arr[i] + arr[i] = arr[min] + arr[min] = temp + + i += 1 + end + return 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 94c0000e9c9375054e9fcfe859a3f9db3fe44cf0 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Wed, 3 Jan 2018 10:26:26 +0800 Subject: [PATCH 24/33] 23 --- 22-missing.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..49a7820 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,27 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + contrast = [0,1,2,3,4,5,6,7,8,9] + result = [] + c = 0 + while c < contrast.size + a = 0 + while a < arr.size + arr_have = false + if contrast[c] == arr[a] + arr_have = true + break + end + a += 1 + end + if arr_have == false + result << contrast[c] + end + + c += 1 + end + + return result end answer = find_missing( [2,2,1,5,8,4] ) From 9ccb11c64c37244b45a711e470930c07df3fc413 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Wed, 3 Jan 2018 22:20:04 +0800 Subject: [PATCH 25/33] repair arr.each --- 22-missing.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/22-missing.rb b/22-missing.rb index 49a7820..27dc05e 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,24 +1,21 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - contrast = [0,1,2,3,4,5,6,7,8,9] + contrast = [1,2,3,4,5,6,7,8,9] result = [] - c = 0 - while c < contrast.size - a = 0 - while a < arr.size - arr_have = false - if contrast[c] == arr[a] + + contrast.each do |c| + arr_have = false + + arr.each do |a| + if c == a arr_have = true break end - a += 1 end if arr_have == false - result << contrast[c] + result << c end - - c += 1 end return result From 53d4ea0c0af384fc70f01364e572847934c12f88 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Thu, 4 Jan 2018 20:15:40 +0800 Subject: [PATCH 26/33] 23 and 24 --- 23-hash-max.rb | 14 ++++++++++---- 24-hash-even.rb | 12 +++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..c52d21b 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,15 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + arr = hash.values + arr_max = arr.max + re_string = '' + hash.each do |key, value| + if value == arr_max + re_string = re_string + key + " " + end + end + return re_string end h = { @@ -9,11 +17,9 @@ def find_max(hash) "b" => 38, "c" => 21, "d" => 80, - "e" => 10 + "e" => 80 } answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..4c704c9 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,13 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + even_key = [] + hash.each do |key, value| + if value % 2 == 0 + even_key.push(key) + end + end + return even_key end h = { @@ -17,5 +21,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 4faba8e0f7fcbbeb7badb7be05098d169e013b3a Mon Sep 17 00:00:00 2001 From: chentianwei Date: Fri, 5 Jan 2018 09:36:17 +0800 Subject: [PATCH 27/33] 26 --- 25-hash-count.rb | 27 +++++++++++++++++++++++++-- 26-hash-filter.rb | 14 ++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..af3045f 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -3,8 +3,32 @@ def count(arr) h = {} + #相当于 uniq + # count = [] + # arr.each do |i| + # if count.include?("i") + # else + # count << i + # end + # end + # + # count.each do |i| + # h[i] = arr.count(i) + # end + + + # arru = arr.uniq + # arru.each do |i| + # h[i] = arr.count(i) + # end + + # 另外一种高效的写法 arr.each do |i| - # ... + if h[i] == nil + h[i] = 1 + else + h[i] = h[i] + 1 + end end return h # 回传一个 hash @@ -15,4 +39,3 @@ def count(arr) 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..ff2baa0 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,19 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... +# 找出成年人,生成新的数组 +adult = [] +arr.each do |i| + if i["age"] > 18 + adult << i + end +end -puts "所有成年人,并由小到大: _________" +# 排序 +result = adult.sort_by{|i| i["age"] } + + +puts "所有成年人,并由小到大:#{result}" # 答案应该是 #[ From e7ba3960b68e4ca572e4d3a73c74d60bf495254e Mon Sep 17 00:00:00 2001 From: chentianwei Date: Fri, 5 Jan 2018 21:23:02 +0800 Subject: [PATCH 28/33] 27 --- 27-class.rb | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..efa46d2 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,16 +1,27 @@ class Person - # ... -end - -p1 = Person.new -p1.first_name = "Peter" -p1.last_name = "Wang" -p1.greet # 输出 "Hello, Peter Wang" + attr_accessor :first_name, :last_name -p2 = Person.new -p2.first_name = "William" -p2.last_name = "Zhang" -p2.greet # 输出 "Hello, William Zhang" + def initialize(first, last) + @first_name = first + @last_name = last + end + def greet + puts "Hello, #{first_name} #{last_name}" + end +end +# p1 = Person.new +# 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.greet # 输出 "Hello, William Zhang" +p3 = Person.new("王", "明") +p3.first_name +p3.last_name +p3.greet From 3e028c295841225c8bedc664cf56d6f1c1dbf5a0 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Fri, 5 Jan 2018 21:53:47 +0800 Subject: [PATCH 29/33] 28 --- 28-word-count.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..dbd936e 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,18 @@ doc = File.read("wordcount.txt") -# ... +h = {} + +doc.each_line { |line| + words = line.split + words.each { |w| + word = w.gsub(/[,()'".]/, '').downcase + if h.has_key?(word) + h[word] = h[word] + 1 + else + h[word] = 1 + end + } +} + +puts h From 801ee7d4ffbe1f9f9eace074dc6a2fd01cc14748 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Sun, 7 Jan 2018 16:32:58 +0800 Subject: [PATCH 30/33] 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 dbd936e..6d1844f 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -16,4 +16,4 @@ } } -puts h +puts hg From 21a64a1990553d99073cbc69a2a20e309cc42bed Mon Sep 17 00:00:00 2001 From: chentianwei Date: Sun, 7 Jan 2018 16:46:34 +0800 Subject: [PATCH 31/33] 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 6d1844f..dbd936e 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -16,4 +16,4 @@ } } -puts hg +puts h From ae222ecd02e49098cca82e56d40b6eeb73247edb Mon Sep 17 00:00:00 2001 From: chentianwei Date: Sun, 7 Jan 2018 17:42:48 +0800 Subject: [PATCH 32/33] 29 --- 29-todos.rb | 17 ++++++++++++----- todos.txt | 5 +---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..caec58b 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -2,6 +2,7 @@ text = File.read("todos.txt") + todos = [] text.each_line do |line| todos << line.chomp @@ -11,23 +12,29 @@ puts "#{index}: #{todo}" end + while (true) print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " command = gets.chomp if command == "add" print "请输入代办事项: " - # ... + new_event = gets.chomp + todos << new_event elsif command == "remove" print "请输入要删除的编号: " - # ... + code = gets.chomp.to_i + todos.delete_at(code) 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 - diff --git a/todos.txt b/todos.txt index 4757e85..a0057b6 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1 @@ -Buy book -Go Shopping -Walk -Gogo +work2 From 46cca81a3f297ef7b0a8290fe1b4a077513ae170 Mon Sep 17 00:00:00 2001 From: chentianwei Date: Wed, 10 Jan 2018 09:40:04 +0800 Subject: [PATCH 33/33] quicksort --- arithmetic/01-bucket.rb | 37 +++++++++++++++++++++++++++++++++++++ arithmetic/02-popo.rb | 33 +++++++++++++++++++++++++++++++++ arithmetic/03-binary.rb | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 arithmetic/01-bucket.rb create mode 100644 arithmetic/02-popo.rb create mode 100644 arithmetic/03-binary.rb diff --git a/arithmetic/01-bucket.rb b/arithmetic/01-bucket.rb new file mode 100644 index 0000000..00e3b2e --- /dev/null +++ b/arithmetic/01-bucket.rb @@ -0,0 +1,37 @@ +a = [] +for i in 0..10 do + a[i] = 0 +end +print a + +puts "\n" +a[2] = 1 +a[3] = 1 +a[5] = 2 +a[8] = 1 + +# 正序排列 +for i in 0..10 do + j = 1 + while j <= a[i] + print i + j += 1 + end +end + +puts "\n" +# 反序排列 +i = 10 +while i >=0 + j = 1 + while j <= a[i] + print i + j += 1 + end + i -= 1 +end +puts "\n" +# 结果是: +# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] +# 23558 +# 85532 diff --git a/arithmetic/02-popo.rb b/arithmetic/02-popo.rb new file mode 100644 index 0000000..055c656 --- /dev/null +++ b/arithmetic/02-popo.rb @@ -0,0 +1,33 @@ +# 实践冒泡排序 + +def selection_sort(arr) + i = 0 # 用来计算外循环次数 ,参与内循环次数计算 + temp = [] # 用于冒泡的数据交换 + while i < arr.size - 1 # 只比较n-1次,因为有n个数 + j = 1 # 初始化内循环的key。 + + while j < arr.size - i + # 每次循环内循环结束后,把最大/小值放到数组最后, + # 下一次外循环的内循环,就不比较已经定位的数字了,用每次外循环初始化j = 1来实现。所以每次外循环都会减少一次内循环,用减 i 来实现。 + # 第一次外循环时,内循环比较9次;第二次外循环时,内循环比较8次;依次递减至结束。 + if arr[j-1] <= arr[j] #如果true则交换值 + # 大于等于 是 从小到大排序 + # 小于等于 是 从大到小排序 + temp = arr[j -1] + arr[j-1] = arr[j] + arr[j] = temp + end + j = j + 1 + end + + i = i + 1 + end + + return arr +end + +arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] + +answer = selection_sort(arr) + +puts answer.to_s diff --git a/arithmetic/03-binary.rb b/arithmetic/03-binary.rb new file mode 100644 index 0000000..6f6696a --- /dev/null +++ b/arithmetic/03-binary.rb @@ -0,0 +1,37 @@ + +def quicksort(a,left,right) + if left > right # 设定何时停止该方法的调用。 + return + end + + temp = a[left] #设定最左边的为基准数 + i = left + j= right + while i != j # i和j从两边向中间靠拢,直到i和j重合,结束本轮循环。 + while a[j] <= temp && i < j #顺序很重要,要先从右往左找。找大于基准数的数,则降序排序。 + j -= 1 + end + while a[i] >= temp && i < j #再向➡️找,根据是升序还是降序来选择 比较运算符号,此例找小于基准数的数。 + i += 1 + end + if i < j #当i 和 j 没有相遇的时候,交换数据 + t = a[i] + a[i] = a[j] + a[j] = t + end + end + a[left] = a[i] #将基准数归位。 + a[i] = temp + print a.to_s + "\n" + quicksort(a,left,i-1) # 在quciksort的方法中,再次调用本方法,形成递归的过程。这个是处理i左边。直到穷尽,即当left>right时停止调用该方法。 + quicksort(a,i+1,right) # 这个处理i右边 + +end + + + +a = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] + +quicksort(a,0,9) +# 0和9 代表数组最左边和最右边的key +print a