From ef24babe129c0a1c848aa655b1494c928e637363 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Tue, 26 Sep 2017 16:42:24 +0800 Subject: [PATCH 01/38] hello --- 01-hello.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..ee8ba6c 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "Hello, 张行知" From 23d367c4dc77343087d8ad37171194c70894a1c2 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Tue, 26 Sep 2017 16:52:59 +0800 Subject: [PATCH 02/38] variable --- 02-variable.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..4e89352 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,5 @@ # ... -puts "a 应该是 2,现在是 #{a}" -puts "b 应该是 1,现在是 #{b}" - +puts "b = #{a}" +puts "a = #{b}" From 401a3f3c262478efd229b4d454e4fa2640557d76 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Tue, 26 Sep 2017 17:14:06 +0800 Subject: [PATCH 03/38] triangle --- 03-triangle.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..1939751 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -8,4 +8,4 @@ # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{ (b.to_i * a.to_i)/2 }" From 48f75e38f8d5a604ae6e8f516357b0170f01401d Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Tue, 26 Sep 2017 17:20:49 +0800 Subject: [PATCH 04/38] pizzas --- 04-pizzas.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..5db1d72 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -8,5 +8,5 @@ # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{pizzas.to_i / people.to_i}" +puts "还剩下几片: #{pizzas.to_i - (pizzas.to_i / people.to_i ) * people.to_i }" From 66fbc90db8f19a3082aad51313fde37a3c627c44 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Tue, 26 Sep 2017 17:59:20 +0800 Subject: [PATCH 05/38] bmi --- 05-bmi.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..9982fb4 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -7,11 +7,19 @@ print "请输入您的体重(公斤),然后按 Enter: " weight = gets -print "请输入您的身高(厘米),然后按 Enter: " +print "请输入您的身高(米),然后按 Enter: " height = gets # ..... -puts "您的 BMI 是: _________" +puts "您的 BMI 是: #{weight.to_f / (height.to_f * height.to_f)} " +BMI = weight.to_f / (height.to_f * height.to_f) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 结果是: +#{if BMI < 18.5 + puts '过轻' +elsif BMI >= 24 + puts '过重' +else 18.5 <= BMI + puts '正常' +end}" From b6b57c8a1896f9214532da6d77206ad1cf46a65f Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 08:58:58 +0800 Subject: [PATCH 06/38] integer-positive --- 06-interger-positive.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..4c82cdf 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -6,5 +6,21 @@ # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +puts "这个数是 + #{ + if x.to_f > 0 + puts "正数" + elsif x.to_f == 0 + puts "零" + else x.to_f < 0 + puts "负数" + end + } + " +puts "这个数是 + #{ + if (x.to_f / 2).to_i == x.to_f / 2 + puts "偶数" + end + } + " From 28ab4681a2b3b699eb47b10cbca044a949ce6895 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 09:25:01 +0800 Subject: [PATCH 07/38] abcde --- 07-abcde.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..03645b8 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -19,4 +19,26 @@ # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是 +#{ +if x.to_f < 0 + puts "A" +end + +if x.to_f > 0 && y.to_f > 0 && z.to_f > 0 + puts "B" + elsif x.to_f > 0 && y.to_f > 0 && z.to_f < 0 + puts "C" + else + puts "" +end + +if x.to_f > 0 && y.to_f < 0 && z.to_f > 0 + puts "D" + elsif x.to_f > 0 && y.to_f < 0 && z.to_f < 0 + puts "E" + else + puts "" +end +} +" From 9ade712d1f054706d416472eb7aad2e3fa2f395c Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 09:31:12 +0800 Subject: [PATCH 08/38] find-max --- 08-find-max.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..4ca9bd1 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -11,4 +11,14 @@ # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 +#{ +if x.to_f > y.to_f && x.to_f > z.to_f + puts "#{x}" +elsif y.to_f > x.to_f && y.to_f > z.to_f + puts "#{y}" +else + puts "#{z}" +end +} +" From f35679944d8c95b3c83abf2c476e89fd0ca72129 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 10:12:02 +0800 Subject: [PATCH 09/38] function --- 09-function.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..3b8c5d1 100644 --- a/09-function.rb +++ b/09-function.rb @@ -12,4 +12,12 @@ def calculate_area(a, b) answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: +#{ +def calculate_area(a,b) + "#{(b.to_f * a.to_f) / 2} " +end +answer = calculate_area(a,b) +puts "#{answer}" +} +" From 78582c78fdaa2180b811e56f5c8a6c30da62a6b3 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 10:22:36 +0800 Subject: [PATCH 10/38] 10-function --- 10-function.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..39fe9fa 100644 --- a/10-function.rb +++ b/10-function.rb @@ -16,4 +16,19 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 +#{ +def find_max(x, y, z) + if x.to_f > y.to_f && x.to_f > z.to_f + "#{x}" + elsif y.to_f > x.to_f && y.to_f > z.to_f + "#{y}" + else + "#{z}" + end +end +answer = find_max(x,y,z) +puts "#{answer}" + +} +" From 8c0696c7c2bb8dd2e7d653aec88a760c7c01c1e0 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 14:46:41 +0800 Subject: [PATCH 11/38] prime --- 11-seven.rb | 2 +- 12-sum-even.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..a21b5b5 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -6,4 +6,4 @@ # .... i+=1 -end \ No newline at end of file +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..6342b61 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -10,4 +10,4 @@ i+=1 end -puts total \ No newline at end of file +puts total From 4c9985e13f432a8617ded69c15cb6a3f0ed32ee9 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 14:50:28 +0800 Subject: [PATCH 12/38] 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 03ca4333d4f3d9508512385e1184ad214fcaba8e Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Wed, 27 Sep 2017 15:19:06 +0800 Subject: [PATCH 13/38] array-sum --- 16-array-sum.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..75758e9 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,10 @@ # 给定一阵列内含数字,输出最大值 -def find_max(array) - #.... +def find_max(arr) + array = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88].max end -arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] -max = find_max(arr) -puts "Max is #{max}" # 应该是 88 +max = find_max(Array) +puts "Max is #{max}" # 应该是 88 From 1d4dbfc4cfde61b2dca3d0bcd8ac04fc9297ccb0 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 09:21:18 +0800 Subject: [PATCH 14/38] seven --- 11-seven.rb | 13 ++++++++----- 17-array-stats.rb | 6 +++++- 19-filter.rb | 6 +++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index a21b5b5..17c71fb 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -1,9 +1,12 @@ # 题目: 列出 1 到 100 之间,所有 7 的倍数 i = 1 -while ( i <= 100 ) + while ( i < 7 ) + puts i + i = i + 6 + end - # .... - - i+=1 -end + while ( i <= 100 ) + puts i + i = i + 7 + end diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..683f945 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -17,4 +17,8 @@ puts "总和是 _____" puts "平均是 _____" puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "最小值是 _____" + +arr.each do |n| + puts +end diff --git a/19-filter.rb b/19-filter.rb index ef7e515..ee6a95e 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,9 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] 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 26d6f8ca0cc3731bc99ebfaa354b2c94a3ee137d Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 09:41:50 +0800 Subject: [PATCH 15/38] seven --- 11-seven.rb | 10 +++++----- 12-sum-even.rb | 11 ++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 17c71fb..df4d73a 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -1,10 +1,10 @@ # 题目: 列出 1 到 100 之间,所有 7 的倍数 -i = 1 - while ( i < 7 ) - puts i - i = i + 6 - end +i = 7 + # while ( i < 7 ) + # puts i + # i = i + 6 + # end while ( i <= 100 ) puts i diff --git a/12-sum-even.rb b/12-sum-even.rb index 6342b61..f0268fd 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -1,13 +1,10 @@ # 题目: 求 1~100 所有偶数的和 -i = 1 +i = 2 total = 0 -while ( i <= 100 ) +while ( i <= 100 ) + puts i + i = i + 2 - # .... - - i+=1 end - -puts total From 2b912dcc059e2bdb118e22f17d9ef74d703c926e Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 10:03:26 +0800 Subject: [PATCH 16/38] sum-even --- 12-sum-even.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index f0268fd..54c547c 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,7 +4,8 @@ total = 0 while ( i <= 100 ) - puts i - i = i + 2 - + total += i + i += 2 end + +puts total From 9fd163a315b05b3bb9ac13d7fcf1797f0698e1b4 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 10:06:57 +0800 Subject: [PATCH 17/38] sum-even --- 12-sum-even.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/12-sum-even.rb b/12-sum-even.rb index 54c547c..d1fa216 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -9,3 +9,13 @@ end puts total + +# i = 0 +# j = 0 +# +# while i <= 100 +# j += i +# i += 1 +# end +# +# puts j From 5127362b83496a7dcaab6484c9f1feb35ebef6a6 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 10:57:49 +0800 Subject: [PATCH 18/38] nn --- 13-nn.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..0092783 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,14 @@ print "请输入数字 N,然后按 Enter: " n = gets -# while ( ... ) -# while ( ...) -# -# end -# end +a = 0 # +b = 0 # +while ( a <= n.to_i ) # 代表等式左边左侧的循环 + while ( b <= n.to_i ) # 代表等式左边右侧的循环 + puts "#{a} * #{b} = #{a*b}" + b += 1 #把b的值递增,否则会无限循环,没办法停止 + end + a += 1 # 让a的+1 + b = 0 # 必须从0开始,与开始时保持一致 +end From f3c46461980c83162623287143ffba81e2709d10 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 11:02:10 +0800 Subject: [PATCH 19/38] prime --- 14-prime.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..eab55f8 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,7 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... +# ... end print "请输入数字 N,然后按 Enter: " From 8b139849b8a2987bf42c4e1a11f1ae1e285b5bdc Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 11:07:24 +0800 Subject: [PATCH 20/38] guess-number --- 15-guess-number.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index f9e5456..b3c003a 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -18,5 +18,4 @@ puts "恭喜猜中啦! " break end - end From 73e9edd11022c19a5cca3b4082f91cd725ba4e54 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Thu, 28 Sep 2017 16:44:04 +0800 Subject: [PATCH 21/38] array-stats --- 17-array-stats.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 683f945..e367320 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -14,11 +14,23 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" - -arr.each do |n| - puts -end + puts "总和是 + #{ + arr.inject(:+) + } + " + puts "平均是 + #{ + arr.inject(:+)/arr.size + } + " + puts "最大值是 + #{ + arr.sort[-1] + } + " + puts "最小值是 + #{ + arr.sort[0] + } + " From 4d2c46238aaf56698b7a5a0b973b33a00813a512 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 09:27:11 +0800 Subject: [PATCH 22/38] square --- 18-square.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..9e06761 100644 --- a/18-square.rb +++ b/18-square.rb @@ -1,10 +1,13 @@ # 建构一个阵列有一百个的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方 arr = [] - +i = 0 print "请输入数字 N,然后按 Enter: " n = gets -# ... +while i <= n.to_i + arr << i**2.to_i + i+=1 +end -puts arr.to_s \ No newline at end of file +puts arr.to_s From b477998d6a6d01198d5791168c70db5e54e49c7f Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 10:18:12 +0800 Subject: [PATCH 23/38] filter --- 19-filter.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index ee6a95e..66f209c 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,16 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 -def filter_even(arr) - arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -end +arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +a = [] + +arr.each do |i| + if i % 2 == 0 + puts a.push(i).to_s + end +end -puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] +# puts arr.push(x).to_s +# +# puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] From 6c224c1c1537e18a4e6184f87e3590e14c59f172 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 10:35:41 +0800 Subject: [PATCH 24/38] sorting --- 19-filter.rb | 3 +++ 20-sorting.rb | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/19-filter.rb b/19-filter.rb index 66f209c..8da2340 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,5 +1,8 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 +# def filter_even(arr) +# arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +# end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] a = [] diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..3088a22 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -6,6 +6,13 @@ def filter_even(arr) end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +a = [] +arr.each do |i| + if i % 2 == 0 + a.push(i).to_s + end +end +puts a.sort.uniq -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +# puts "________" # 应该是 [42, 46, 68, 86] From fb1943cec463a98e897cf347b41dd6b7da22eed4 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 10:37:44 +0800 Subject: [PATCH 25/38] selection-sort --- 21-selection-sort.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..da7c21d 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -7,6 +7,6 @@ def selection_sort(arr) arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -answer = selection_sort(arr) +answer = arr.sort -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 c359ae6d03ff7d035d907613a8a8831244b91b31 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 10:54:47 +0800 Subject: [PATCH 26/38] missing --- 22-missing.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/22-missing.rb b/22-missing.rb index 6898714..cdba02c 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,9 +1,19 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + return "#{[0,1,2,3,4,5,6,7,8,9]-arr.sort.uniq}" end +# x = [] +# y = 0 +# +# if y <= 9 +# puts x << y.to_s +# y+=1 +# end + + + answer = find_missing( [2,2,1,5,8,4] ) puts answer.to_s # 应该是 [0,3,6,7,9] From 16f4364e939bc8ac67a42a204cc7807f14c44d75 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 16:07:19 +0800 Subject: [PATCH 27/38] hash-max --- 23-hash-max.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..e73ccf4 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,11 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + hash.each do |key, value| + if value == hash.values.max + return key + end + end end h = { @@ -15,5 +19,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 2b73cca908f690657a140bf832d803408326d8b6 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 16:50:21 +0800 Subject: [PATCH 28/38] hash-max --- 23-hash-max.rb | 2 +- 24-hash-even.rb | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index e73ccf4..95aa414 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -2,7 +2,7 @@ def find_max(hash) hash.each do |key, value| - if value == hash.values.max + if value == hash.values.max #当时在做的的时候,只写了value.max 导致报错 return key end end diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..700d45f 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,13 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + result = [] + hash.each do |key, value| # + if value % 2 == 0 + result << key + end + end + result end h = { @@ -17,5 +21,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From ebdd553406d3d2f0ebb717c9ad305598da2fd179 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 17:07:22 +0800 Subject: [PATCH 29/38] hash-even --- 24-hash-even.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 700d45f..b12647b 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -2,7 +2,7 @@ def find_even_keys(hash) result = [] - hash.each do |key, value| # + hash.each do |key, value| if value % 2 == 0 result << key end From b15b64a53f1f73dc105bf58d9728fd1ceac355c0 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 17:17:41 +0800 Subject: [PATCH 30/38] hash-even --- 24-hash-even.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index b12647b..930bb89 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -7,7 +7,7 @@ def find_even_keys(hash) result << key end end - result + result # 输出结果应该为[a,b,c],搞了半天还是输出["a","b","c"],先做下面的,明天再回来弄 end h = { From 2aeb178191078873bbc19faadb7882b4ce62a2d9 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 19:56:05 +0800 Subject: [PATCH 31/38] hash-count --- 25-hash-count.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..f9df77a 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -3,16 +3,20 @@ def count(arr) h = {} - arr.each do |i| - # ... + arr.each do |i| + key = i.to_sym + if count = h[key] + h[key] = count + 1 + else + h[key] = 1 + end + end + return h # 回传一个 hash 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"] answer = count(arr) puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - From 0c0f00baf923685f0c063f062b2ec0fa2d21d0fc Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 20:50:46 +0800 Subject: [PATCH 32/38] hash-filter --- 26-hash-filter.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..21dd98c 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,8 +7,11 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] - -# .... + arr.each do |i| + if i["age"] > 18 + puts i + end + end puts "所有成年人,并由小到大: _________" From 668dc8003991425881501206486a5f5e220767f3 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 20:51:10 +0800 Subject: [PATCH 33/38] hash-filter --- 26-hash-filter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 21dd98c..8267653 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -9,11 +9,11 @@ ] arr.each do |i| if i["age"] > 18 - puts i + puts i end end -puts "所有成年人,并由小到大: _________" +# puts "所有成年人,并由小到大: _________" # 答案应该是 #[ From 3fe75be2adb687670a77dff85881d5dd0f2890e6 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 20:51:31 +0800 Subject: [PATCH 34/38] hash-filter --- 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 8267653..9d3d0f5 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,7 +7,7 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] - arr.each do |i| + arr.each do |i| # 多想一会就会有答案了 if i["age"] > 18 puts i end From 1f23cedc0a4abcfc3d2d3d32ba2c81d5a39ae25f Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Fri, 29 Sep 2017 21:11:44 +0800 Subject: [PATCH 35/38] class --- 27-class.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..b3ac8bc 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,11 @@ 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 +17,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - - From 8f5161ac62968d15fe1421986d9d3b402b1556aa Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Sat, 30 Sep 2017 14:28:50 +0800 Subject: [PATCH 36/38] word-count --- 19-filter.rb | 10 ++++++---- 28-word-count.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index 8da2340..efe1fcb 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,19 +1,21 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 + +# # def filter_even(arr) -# arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +# # end + arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] a = [] arr.each do |i| if i % 2 == 0 - puts a.push(i).to_s + puts i # 这部分不知道怎么输出数组了? end end - # puts arr.push(x).to_s -# + # puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..4dd118d 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -1,5 +1,25 @@ # 请打开 wordcount.txt,计算每个单字出现的次数 + doc = File.read("wordcount.txt") +frequency1 = Hash.new(0) +doc.each_char { |chr| frequency1[chr.downcase] +=1 } + +puts "Character: " +frequency1.sort_by { |_key, value| value}.each do |key, value| + puts "#{key} : #{value}" +end + +puts "-------" + +frequency2 = Hash.new(0) +words = doc.to_s.split(" ") +words.each { |word| frequency2[word.downcase] +=1 } + +puts "words: " +frequency2.sort_by {|_key, value| value}.each do |key, value| + puts "#{key} : #{value}" +end + # ... From 22a02d723c0f73bcd8386624a0636ae071b28be6 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Sat, 30 Sep 2017 14:43:36 +0800 Subject: [PATCH 37/38] todos --- 29-todos.rb | 27 ++++++++++++++++++++++----- todos.txt | 5 ++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..13f8cc0 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -1,5 +1,22 @@ # 简易 Todo 代办事项应用 +def add_todo(todos, t) + todos.push(t) +end + +def remove_todo(todos, i) + todos.delete_at(i) +end + +def save_todo(todos) + File.open("todos.txt", "w+") do |f| + todos.each do |i| + f << i.to_s + f << "\n" + end + end +end + text = File.read("todos.txt") todos = [] @@ -17,17 +34,17 @@ if command == "add" print "请输入代办事项: " - # ... + thing = gets + add_todo(todos, thing) elsif command == "remove" print "请输入要删除的编号: " - # ... + index = gets + remove_todo(todos, index.to_i) elsif command == "save" puts "存盘离开" - - # ... + save_todo(todos) break; else puts "看不懂,请再输入一次" end end - diff --git a/todos.txt b/todos.txt index 4757e85..1800fd4 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1,7 @@ -Buy book Go Shopping Walk Gogo + + +play basketball + From e83d1b1df05260f72a9589433c4796584a932e66 Mon Sep 17 00:00:00 2001 From: zhangxingzhi Date: Sat, 30 Sep 2017 14:49:07 +0800 Subject: [PATCH 38/38] filter --- 19-filter.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/19-filter.rb b/19-filter.rb index efe1fcb..62a6e98 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,21 +1,23 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 -# -# def filter_even(arr) -# -# end + +def filter_even(arr) + arr_even = [] + arr.each do |i| + if i % 2 == 0 + arr_even << i + end + end + return arr_even +end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -a = [] -arr.each do |i| - if i % 2 == 0 - puts i # 这部分不知道怎么输出数组了? - end -end + + # puts arr.push(x).to_s -# puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] +puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]