From 89b49206e55578713a10b047cf23d2906a7370be Mon Sep 17 00:00:00 2001 From: FengYue <10761101@gm.nfu.edu.tw> Date: Thu, 13 Jan 2022 20:13:24 +0800 Subject: [PATCH 1/2] finish programming -exercise hw --- 01-hello.rb | 2 +- 02-variable.rb | 4 ++-- 03-triangle.rb | 4 ++-- 04-pizzas.rb | 8 +++++--- 05-bmi.rb | 15 ++++++++++++--- 06-interger-positive.rb | 18 +++++++++++++++--- 07-abcde.rb | 21 +++++++++++++++++++-- 08-find-max.rb | 16 ++++++++++++++-- 09-function.rb | 4 ++-- 10-function.rb | 15 ++++++++++++++- 11-seven.rb | 8 ++++---- 12-sum-even.rb | 7 ++++--- 13-nn.rb | 25 ++++++++++++++++++++++--- 14-prime.rb | 21 ++++++++++++++------- 15-guess-number.rb | 7 +++++-- 16-array-sum.rb | 2 +- 17-array-stats.rb | 13 +++++++++---- 18-square.rb | 6 ++++-- 19-filter.rb | 10 ++++++++-- 20-sorting.rb | 10 ++++++++-- 21-selection-sort.rb | 13 +++++++++++-- 22-missing.rb | 7 ++++++- 23-hash-max.rb | 3 ++- 24-hash-even.rb | 14 +++++++++++++- 25-hash-count.rb | 7 ++++++- 26-hash-filter.rb | 22 ++++++++++++++++++++-- 27-class.rb | 5 ++++- 28-word-count.rb | 11 +++++++++-- 29-todos.rb | 18 +++++++++++++++--- todos.text | 4 ++++ todos.txt | 2 +- 31 files changed, 256 insertions(+), 66 deletions(-) create mode 100644 todos.text diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..cb02ea6 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "Hi, #{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..b8a8446 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -1,7 +1,7 @@ # 题目: 交换 a, b 变数的值 -a = 1 -b = 2 +a = 2 +b = 1 puts "a 是 #{a}" puts "b 是 #{b}" diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..13fd14c 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,6 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets -# ..... +c = (a.to_i) * (b.to_i) / 2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{c}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..72d13e7 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -6,7 +6,9 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -# ..... +a = (pizzas.to_i) / (people.to_i) -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +b = (pizzas.to_i) % (people.to_i) + +puts "每人可分得几片: #{a} 片" +puts "还剩下几片: #{b} 片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..5f4f177 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,17 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +w = weight.to_f +h = height.to_f / 100 +bmi = (w / (h * h)).round(2) -puts "您的 BMI 是: _________" +if bmi < 18.5 + bmi_result = "過輕" +elsif bmi >= 24 + bmi_result = "過重" +else bmi >=18.5 || bim <=24 + bmi_result = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 是: #{bmi}" +puts "您的 BMI 结果是: #{bmi_result}" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..5aaaf30 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -4,7 +4,19 @@ print "请输入一个整数,然后按 Enter: " x = gets -# .... +if x.to_i > 0 + inter_result = "正數" +elsif x.to_i == 0 + inter_result = "零" +else x.to_i <0 + inter_result = "負數" +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x.to_i % 2 == 0 + this_number = "偶數" +else + this_number = "奇數" +end + +puts "这个数是#{inter_result} (正数或零或负数)" +puts "这个数是#{this_number} (偶数或奇数)" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..0dc29e7 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,6 +17,23 @@ print "请输入一个整数z,然后按 Enter: " z = gets -# .... +if x.to_i < 0 + result = "A" +else + if y.to_i > 0 + if z.to_i > 0 + result = "B" + else + result = "C" + end + else + if z.to_i > 0 + result = "D" + else + result = "E" + end + end +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file + +puts "结果是#{result}(A或B或C或D或E)" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..b8d0fdf 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,18 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +if x > y + if x > z + find_max = "x" + else + find_max = "z" + end +else + if y > z + find_max = "y" + else + find_max = "z" + end +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 #{find_max}(x或y或z)" diff --git a/09-function.rb b/09-function.rb index b1f922d..267211f 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,7 +1,7 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + (b.to_i * a.to_i) / 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..1e94c5b 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,19 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y + if x > z + "x" + else + "z" + end + else + if y > z + "y" + else + "z" + end + end end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +29,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" diff --git a/11-seven.rb b/11-seven.rb index 26c221d..5b01f64 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,8 @@ 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..9ec986a 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,10 @@ while ( i <= 100 ) - # .... - + if i % 2 == 0 + total += 1 + 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..4fad67d 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,28 @@ print "请输入数字 N,然后按 Enter: " n = gets -# while ( ... ) -# while ( ...) -# +# i = 1 +# while ( i <= n.to_i ) +# j = 1 +# while ( j <= i) +# result = i * j +# print "#{j} x #{i} = #{result} " +# j += 1 # end +# +# print"\n" +# i += 1 # end +i = 0 +j = 0 +n = n.to_i +while (i <= n ) + while (j <= n) + result = i * j + puts "#{i} x #{j} = #{result}" + j+=1 + end + i+=1 + j = 0 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..33d7784 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,14 +1,21 @@ # 输入一个数字 N,请检查是不是质数 -def is_prime(n) -# .... +def is_prime(num) + for i in 2..n/2 + if n % i == 0 + false + else + ture + end + end end + print "请输入数字 N,然后按 Enter: " n = gets -if is_prime(n.to_i) - puts "这是质数" -else - puts "这不是质数" -end +# if is_prime(n.to_i) +# puts "这是质数" +# else +# puts "这不是质数" +# end diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..89fc37d 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -12,6 +12,9 @@ if n.to_i == target puts "恭喜猜中啦! " break + elsif n.to_i > target + puts "太高了,再猜一次" + else + puts "太低了,再猜一次" end - -end \ No newline at end of file +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..885e0a7 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,7 +1,7 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + array.max end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..6812c9f 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -14,7 +14,12 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +sum = 0 +arr.each do + |a| sum += a +end + +puts "总和是#{sum.to_s }" +puts "平均是 #{sum.to_f/arr.length}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..9bd3388 100644 --- a/18-square.rb +++ b/18-square.rb @@ -5,6 +5,8 @@ print "请输入数字 N,然后按 Enter: " n = gets -# ... +for i in 0..n.to_i + arr[i] = i * i +end -puts arr.to_s \ No newline at end of file +puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..a78f67e 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + result = [] + arr.each do |a| + if a % 2 == 0 + result << a + end + end + return result 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..7cf32de 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + arry_result = [] + arr.each do |result| + if result % 2 == 0 + arry_result << result + end + end + return arry_result 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.uniq.to_s # 应该是 [42, 46, 68, 86] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..8896f10 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,20 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + (0...arr.length).each do |j| + key = arr[j] + i = j -1 + while i >= 0 && arr[i] > key + arr[i + 1] = arr[i] + i = i - 1 + end + arr[i + 1] = key + end + 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] diff --git a/22-missing.rb b/22-missing.rb index 6898714..2b1c740 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,12 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + result = [] + for i in 0..9 + result << i + end + missing_number = result - arr + return missing_number end answer = find_missing( [2,2,1,5,8,4] ) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..f98e0f5 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,8 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + max = hash.values.max + return hash.key(max) end h = { diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..6bfc6f8 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -3,9 +3,21 @@ def find_even_keys(hash) # ... (请回传一个数组) - + result = [] + hash.values.each do |hash| + if hash % 2 == 0 + result << hash + end + end + result2 = [] + result.each do |i| + result2 << hash.key(i) + end + return result2 end + + h = { "a" => 71, "b" => 38, diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..ad9d4ee 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -4,7 +4,12 @@ def count(arr) h = {} arr.each do |i| - # ... + key = i.to_sym + if array_total = h[key] + h[key] = array_total + 1 + else + h[key] = 1 + end end return h # 回传一个 hash diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..e28edaf 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,27 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... +result = [] +age = [] +for i in arr + age << i["age"] +end -puts "所有成年人,并由小到大: _________" +age_check = [] +for j in age + if j > 18 + age_check << j + end +end + +for double_check in arr + if age_check.include?(double_check["age"]) + result << double_check + end +end +result = result.sort_by{ |i| i["age"] } + +puts "所有成年人,并由小到大:#{result}" # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..4403a91 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,8 @@ class Person - # ... + attr_accessor :first_name, :last_name + def greet + puts "Hello, #{first_name} #{last_name}" + end end p1 = Person.new diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..12ec5b3 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -1,5 +1,12 @@ # 请打开 wordcount.txt,计算每个单字出现的次数 - doc = File.read("wordcount.txt") -# ... +count = Hash.new(0) +doc.each_line do |line| + words = line.split + words.each do |word| + count[word] += 1 + end +end + +puts count diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..e315755 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -17,14 +17,26 @@ if command == "add" print "请输入代办事项: " - # ... + add = gets.chomp + todos << add + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end elsif command == "remove" print "请输入要删除的编号: " - # ... + todos.delete_at(gets.chomp.to_i) + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end elsif command == "save" puts "存盘离开" - # ... + target = File.open("todos.txt", 'w') + for i in todos + target.write(i) + target.write("\n") + end + target.close() break; else puts "看不懂,请再输入一次" diff --git a/todos.text b/todos.text new file mode 100644 index 0000000..4757e85 --- /dev/null +++ b/todos.text @@ -0,0 +1,4 @@ +Buy book +Go Shopping +Walk +Gogo diff --git a/todos.txt b/todos.txt index 4757e85..ed97647 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1,4 @@ Buy book Go Shopping Walk -Gogo +go home From 56e55f5d484a587d850dabf6a7665068ae6896f0 Mon Sep 17 00:00:00 2001 From: FengYue <10761101@gm.nfu.edu.tw> Date: Thu, 13 Jan 2022 20:15:24 +0800 Subject: [PATCH 2/2] fix 02-variable.rb --- 02-variable.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index b8a8446..ccc790b 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -1,12 +1,12 @@ # 题目: 交换 a, b 变数的值 -a = 2 -b = 1 +a = 1 +b = 2 puts "a 是 #{a}" puts "b 是 #{b}" -# ... +a, b = b, a puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}"