From e5d3d1f792b90207c6e09430f572bcd6a5d569e6 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Fri, 14 Jul 2017 22:41:04 +0800 Subject: [PATCH 1/7] 1 and 2 --- 01-hello.rb | 2 +- 02-variable.rb | 5 ++--- 03-triangle.rb | 4 +++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..49be714 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello,#{ your_name }" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..2821710 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,5 @@ # ... -puts "a 应该是 2,现在是 #{a}" -puts "b 应该是 1,现在是 #{b}" - +puts "a 应该是 2,现在是 #{b}" +puts "b 应该是 1,现在是 #{a}" diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..22fbd8b 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -8,4 +8,6 @@ # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{a} #{b} " + +a * b = gets From 65bee305f97ee53a31ecabaccbbc526dd6c89052 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Fri, 14 Jul 2017 23:57:31 +0800 Subject: [PATCH 2/7] 3 and 4 --- 03-triangle.rb | 4 +--- 04-pizzas.rb | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index 22fbd8b..ef44081 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,8 +6,6 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets -# ..... -puts "直角三角形的面积是: #{a} #{b} " -a * b = gets +puts "直角三角形的面积是: #{a.to_f*b.to_f/2} " diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..45f2ca4 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_f-people.to_i*(pizzas.to_i/people.to_i).to_f}片" From c56ea5f1f33791a86dd0ebbcba34940cbd518ff8 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Sat, 15 Jul 2017 01:35:10 +0800 Subject: [PATCH 3/7] 5 6 7 8 --- 05-bmi.rb | 14 ++++++++++---- 06-interger-positive.rb | 15 ++++++++++++--- 07-abcde.rb | 20 ++++++++++++++------ 08-find-max.rb | 16 ++++++++++------ 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..22dc314 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -7,11 +7,17 @@ print "请输入您的体重(公斤),然后按 Enter: " weight = gets -print "请输入您的身高(厘米),然后按 Enter: " +print "请输入您的身高(米),然后按 Enter: " height = gets -# ..... +bmi = weight.to_f/(height.to_f*height.to_f) -puts "您的 BMI 是: _________" +puts "您的 BMI 是: #{bmi}" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +if bmi.to_f < 18.5 + puts "您的体重过轻" +elsif bmi.to_f >= 24 + puts "您的体重过重" +else + puts "您的体重正常" +end diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..633e84a 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -4,7 +4,16 @@ print "请输入一个整数,然后按 Enter: " x = gets -# .... +if x.to_i > 0 + puts "这个数是正数" +elsif x.to_i < 0 + puts "这个数是负数" +else + puts "这个数是零" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x.to_i/2*2 == x.to_i + puts "且是偶数" +else + puts "且是奇数" +end diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..f026568 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,22 @@ # 当 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 -# .... - -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if x.to_i < 0 + puts "结果是A" +elsif x > 0 && y > 0 && z > 0 + puts "结果是B" +elsif x > 0 && y > 0 && z < 0 + puts "结果是C" +elsif x > 0 && y < 0 && z > 0 + puts "结果是D" +else x > 0 && y < 0 && z < 0 + puts "结果是E" +end diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..bfd247e 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -1,14 +1,18 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 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 -# .... - -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +if x > y && x > z + puts "最大的数是 #{x}" +elsif x > y && x < z + puts "最大的数是 #{z}" +else x < y && y > z + puts "最大的数是 #{y}" +end From 664fe78097145d97595c15d8837878ee17abbc44 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Sat, 15 Jul 2017 01:51:41 +0800 Subject: [PATCH 4/7] 9 and 10 function --- 09-function.rb | 4 ++-- 10-function.rb | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..a3948f3 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}" diff --git a/10-function.rb b/10-function.rb index bb450fb..68283e8 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 x > y && x < z + z + else x < y && y > z + y + 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 d15a0ea16b6ccf0053e562d6b5c400a092fb65d4 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Sat, 15 Jul 2017 23:12:48 +0800 Subject: [PATCH 5/7] while break end and n%i == 0 for 11 12 13 14 15 --- 11-seven.rb | 6 ++++-- 12-sum-even.rb | 6 ++++-- 13-nn.rb | 19 +++++++++++-------- 14-prime.rb | 15 ++++++++++++--- 15-guess-number.rb | 11 +++++++---- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..1e25d1e 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,9 @@ i = 1 while ( i <= 100 ) - # .... + if i%7 == 0 + puts i + end i+=1 -end \ No newline at end of file +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..8430920 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,11 @@ while ( i <= 100 ) - # .... + if i/2*2 == i + total += i + end i+=1 end -puts total \ No newline at end of file +puts total diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..67524de 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,14 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end - +n = gets.to_i +a = 0 + while ( a <= n ) + b = 0 + while ( b <= n ) + c = a*b + puts "#{a}x#{b}=#{c}" + b = b + 1 + end + a = a + 1 + end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..2f0f1eb 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,11 +1,20 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + while i <= n/2 + if n % i == 0 + return false + break + else + return true + end + i += 1 + end end -print "请输入数字 N,然后按 Enter: " -n = gets +print "请输入数字 N>1,可以判断是不是质数,然后按 Enter: " +n = gets.to_i if is_prime(n.to_i) puts "这是质数" diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..f859a0e 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,16 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets + if n.to_i < target + puts "太低了,再猜一次" - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" +elsif n.to_i > target - if n.to_i == target + puts "太高了,再猜一次" + +else n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end From d9834eaa559de15dcdea7a2c2e455bf6f9ac8e77 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Thu, 27 Jul 2017 00:14:21 +0800 Subject: [PATCH 6/7] 17 stop --- 16-array-sum.rb | 15 +++++++++++---- 17-array-stats.rb | 6 ++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..ad8dba8 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,18 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = array[0] + array.each do |i| + + if i > m + m = i + end + + end + return m end -arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] +array = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] -max = find_max(arr) +max = find_max(array) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..dfd560d 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -13,8 +13,10 @@ end puts arr.to_s - +array.each do |i| + +end puts "总和是 _____" puts "平均是 _____" puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "最小值是 _____" From 1d3667339b471fe1a298444bb6f86c459d997f94 Mon Sep 17 00:00:00 2001 From: xiaowu Date: Sat, 26 Aug 2017 20:00:58 +0800 Subject: [PATCH 7/7] all --- 01-hello.rb | 2 +- 07-abcde.rb | 2 +- 17-array-stats.rb | 61 +++++++++++++++++++++++++++++++++++++++----- 18-square.rb | 16 +++++++++--- 19-filter.rb | 13 ++++++++-- 20-sorting.rb | 16 +++++++++--- 21-selection-sort.rb | 19 ++++++++++++-- 22-missing.rb | 21 ++++++++++++++- 23-hash-max.rb | 12 ++++++--- 24-hash-even.rb | 4 +-- 25-hash-count.rb | 7 +++-- 26-hash-filter.rb | 8 ++++-- 27-class.rb | 10 +++++--- 28-word-count.rb | 8 +++++- 29-todos.rb | 46 +++++++++++++++++++++------------ todos.txt | 1 + 16 files changed, 196 insertions(+), 50 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index 49be714..ffd7f9e 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "hello,#{ your_name }" +print "hello,#{ your_name }" diff --git a/07-abcde.rb b/07-abcde.rb index f026568..93df815 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,7 +17,7 @@ print "请输入一个整数z,然后按 Enter: " z = gets.to_i -if x.to_i < 0 +if x < 0 puts "结果是A" elsif x > 0 && y > 0 && z > 0 puts "结果是B" diff --git a/17-array-stats.rb b/17-array-stats.rb index dfd560d..0ec1276 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -13,10 +13,59 @@ end puts arr.to_s -array.each do |i| - +puts arr.size +def find_max(arr) + m = arr[0] + arr.each do |i| + + if i > m + m = i + end + + end + return m +end +max = find_max(arr) +puts "最大值是 #{max}" + +def find_min(arr) + n = arr[0] + arr.each do |i| + + if i < n + n = i + end + + end + return n +end +min = find_min(arr) +puts "最小值是 #{min}" + +def find_sum(arr) + s = 0 + arr.each do |i| + + + s = i + s + + + end + return s +end +sum = find_sum(arr) +puts "总和是 #{sum}" + +def find_average(arr) + a = 0 + arr.each do |i| + + + a = i + a + + + end + return a/arr.size end -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" +average = find_average(arr) +puts " 平均数是#{average}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..31b2578 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,18 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i -# ... -puts arr.to_s \ No newline at end of file + s = 0 + while s <= n + a = s * s + arr << a + s = s + 1 + end + + + + + +puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..faa738d 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,18 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + str = [] + arr.each do |i| + + if i%2 == 0 + + str << i + + end + end + return str 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] diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..fa90198 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,20 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... -end + str = [] + arr.each do |i| + + if i%2 == 0 + + str << i + end + + end + return str.uniq + +end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +puts filter_even(arr).sort.to_s # 应该是 [42, 46, 68, 86] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index 9cb58f8..0495564 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,26 @@ # https://zh.wikipedia.org/wiki/选择排序 def insertion_sort(arr) - #... + j =0 + while (j < arr.size) + i = 1 + x = arr[0] + while ( i < (arr.size-j) ) + if x > arr[i] + x = arr[i] + arr[i] = arr[i-1] + arr[i-1] = x + end + i += 1 + x = arr[i-1] + end + j += 1 + end + return arr end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] answer = insertion_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] diff --git a/22-missing.rb b/22-missing.rb index 6898714..fb9d2e5 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,26 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + anw = [] + i = 0 + while ( i < 10 ) + j = 1 + while (j <= arr.size ) + if arr[j-1] == i + break + else j += 1 + end + end + + if j == (arr.size+1) + anw << i + end + + i += 1 + + end + + return anw end answer = find_missing( [2,2,1,5,8,4] ) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..31d140c 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,15 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + max_value = hash.values.max + max_key = "" + hash.each do |key,value| + if value == max_value + max_key = key + break + end + end + max_key end h = { @@ -15,5 +23,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..bed7566 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -2,7 +2,7 @@ def find_even_keys(hash) - # ... (请回传一个数组) + hash.select { |k,v| v % 2 == 0 }.keys end @@ -17,5 +17,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..4facfbd 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,7 +4,11 @@ def count(arr) h = {} arr.each do |i| - # ... + if h[i] == nil + h[i] = 1 + else + h[i] = h[i] + 1 + end end return h # 回传一个 hash @@ -15,4 +19,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..a1c1876 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,13 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... +def process(arr) + arr.sort_by{ |i| i["age"]}.select{ |a| a["age"] >= 18 } +end -puts "所有成年人,并由小到大: _________" + answer = process(arr) + +puts "所有成年人,并由小到大: #{answer}" # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..08b70ec 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" - - - diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..b620eb7 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,10 @@ doc = File.read("wordcount.txt") -# ... +arr = doc.split("") + h = {} + arr.uniq.each do |i| + h[i] = arr.count(i) + end + + puts h diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..16ab1e0 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -12,22 +12,36 @@ end while (true) - print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " - command = gets.chomp + print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " + command = gets.chomp - if command == "add" - print "请输入代办事项: " - # ... - elsif command == "remove" - print "请输入要删除的编号: " - # ... - elsif command == "save" - puts "存盘离开" + todo = nil + if command == "add" + print "请输入代办事项: " + # ... + todo = gets.chomp + todos << todo + elsif command == "remove" + print "请输入要删除的编号: " + # ... + remove = gets.chomp.to_i + todos.delete_at(remove) + elsif command == "save" + File.open("todos.txt", "w+") do |f| + todos.each do |i| + f << i + f << "\n" + end + end + puts "存盘离开" - # ... - break; - else - puts "看不懂,请再输入一次" + # ... + puts "现代办事项为:" + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end + break; + else + puts "看不懂,请再输入一次" + end end -end - diff --git a/todos.txt b/todos.txt index 4757e85..feabb0d 100644 --- a/todos.txt +++ b/todos.txt @@ -2,3 +2,4 @@ Buy book Go Shopping Walk Gogo +chifan