diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..a978e84 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..53b4c95 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -6,8 +6,9 @@ puts "a 是 #{a}" puts "b 是 #{b}" -# ... +c = a +a = b +b = c puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..77dfaa3 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,8 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets + # ..... +s = (a.to_f*a.to_f)/2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{s}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..cf93c0e 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -7,6 +7,11 @@ people = gets # ..... + s1 = pizzas + s2 = people + t1 = s1.to_i/s2.to_i + t2 = s1.to_i%s2.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file + +puts "每人可分得几片: #{t1} 片" +puts "还剩下几片: #{t2} 片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..9bf7c09 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -11,7 +11,18 @@ height = gets # ..... +w = weight +h = height +b = w.to_f/(h.to_f*h.to_f)*100*100 -puts "您的 BMI 是: _________" +if b < 18.5 + t = "过轻" +elsif b >= 24 + t = "过重" +else ( b >= 18.5 ) && ( b < 24 ) + t = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +puts "您的 BMI 是: #{b}" + +puts "您的 BMI 结果是: #{t}(过轻或正常或过重)" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..11f2694 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -5,6 +5,23 @@ x = gets # .... +s = x.to_i +s1 = s%2 -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if s < 0 + t = "负数" +elsif s > 0 + t = "正数" +else + t = "零" +end + +if s1 > 0 + t1 = "奇数" +elsif s1 == 0 + t1 = "偶数" +end + + +puts "这个数是: #{t}(正数或零或负数)" +puts "这个数是: #{t1} (偶数或奇数)" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..e890710 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -18,5 +18,43 @@ z = gets # .... +=begin +t1 = x.to_i +t2 = y.to_i +t3 = z.to_i -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if t1 < 0 + s = "A" +elsif t1 > 0 && t2 > 0 && t3 > 0 + s = "B" +elsif t1 > 0 && t2 > 0 && t3 < 0 + s = "C" +elsif t1 > 0 && t2 < 0 && t3 > 0 + s = "D" +else t1 >0 && t2 < 0 && t3 < 0 + s = "E" +end +=end + +x = x.to_i +y = y.to_i +z = z.to_i + +if x < 0 + s = "A" +elsif x > 0 && y > 0 + if z > 0 + s = "B" + elsif z < 0 + s = "C" + end +elsif x > 0 && y < 0 + if z > 0 + s = "D" + elsif z < 0 + s = "E" + end +end + + +puts "结果是: #{s} (A或B或C或D或E)" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..f0ba7a1 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -10,5 +10,20 @@ z = gets # .... +x = x.to_i +y = y.to_i +z = z.to_i -puts "最大的数是 ________(x或y或z)" \ No newline at end of file + + +if x >= y && x >= z + t = "x" +elsif y >= z && y >= x + t = "y" +elsif z >= x && z >= y + t = "z" +end + +# t = [x, y, z].max + +puts "最大的数是: #{t} (x或y或z)" diff --git a/09-function.rb b/09-function.rb index b1f922d..6fa1049 100644 --- a/09-function.rb +++ b/09-function.rb @@ -2,6 +2,9 @@ def calculate_area(a, b) # .... + a = a.to_f + b = b.to_f + s = ( a * b ) / 2 end print "请输入直角三角形的高,然后按 Enter: " @@ -12,4 +15,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..35a511b 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,18 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + x = x.to_f + y = y.to_f + z = z.to_f + + if x >= y && x >= z + t = "x" + elsif y >= z && y >= x + t = "y" + elsif z >= x && z >= y + t = "z" + end + end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +28,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..82def6d 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -4,6 +4,9 @@ while ( i <= 100 ) # .... - - i+=1 -end \ No newline at end of file + if i % 7 == 0 + puts i + end + i += 1 + +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..34e8aec 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -7,7 +7,12 @@ # .... + if i % 2 == 0 + puts 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..1d44ae8 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -2,10 +2,21 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i +x = x.to_i +y = y.to_i # while ( ... ) # while ( ...) # # end # end - +x = 0 +while x <= n + y = 0 + while y <= n + puts "#{x} x #{y} = #{x*y}" + y += 1 + end + x += 1 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..7af5487 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -2,6 +2,24 @@ def is_prime(n) # .... + if n < 2 + return false + end + + if n == 2 + return true + end + + if n > 2 + i = 2 + while i <= n/2 + if n % i == 0 + return false + end + i += 1 + end + return true + end end print "请输入数字 N,然后按 Enter: " diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..88a1a46 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -12,6 +12,10 @@ if n.to_i == target puts "恭喜猜中啦! " break + elsif n.to_i > target + puts "太高了,再猜一次" + elsif n.to_i < target + puts "太低了,再猜一次" end -end \ No newline at end of file +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..c702f39 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -2,10 +2,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] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..e03a37c 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -14,7 +14,22 @@ puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +sum = 0 +m = n = arr[0] +arr.each do |i| + sum += i + if m < i + m = i + end + if n > i + n = i + end +end + +average = sum / arr.size + + +puts "总和是 #{sum}" +puts "平均是 #{average}" +puts "最大值是 #{m}" +puts "最小值是 #{n}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..8637155 100644 --- a/18-square.rb +++ b/18-square.rb @@ -5,6 +5,12 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i + +(0..n-1).each_with_index do |i| + # arr.push (i*i) + arr << i ** 2 +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..1b16f9b 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -2,8 +2,15 @@ def filter_even(arr) #... + arr2 = [] + arr.each do |i| + if i % 2 == 0 + arr2.push (i) + end + end + arr2 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..876372a 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -3,9 +3,17 @@ def filter_even(arr) #... + arr2 = [] + arr.each do |i| + if i % 2 == 0 + arr2.push(i) + end + end + arr2 = arr2.uniq + arr2 = arr2.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] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..f6fdecb 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -3,10 +3,22 @@ def selection_sort(arr) #... + i = 0 + while i < ( arr.size - 1 ) + j = i + 1 + while j < arr.size + if arr[i] > arr[j] + arr[i], arr[j] = arr[j], arr[i] + end + j += 1 + end + i += 1 + 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..e7e3af9 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -2,6 +2,20 @@ def find_missing(arr) # ... + missingArr = [] + for i in 0..9 + missing = true + for a in arr + if a == i + missing = false + break + end + end + if missing + missingArr.push(i) + end + end + missingArr end answer = find_missing( [2,2,1,5,8,4] ) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..4915875 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -2,6 +2,13 @@ def find_max(hash) # ... + m = hash["a"] + hash.each do |key, value| + if m < value + m = value + end + end + hash.key(m) end h = { @@ -15,5 +22,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..6451655 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -3,7 +3,13 @@ def find_even_keys(hash) # ... (请回传一个数组) - + result = {} + hash.each do |key, value| + if value % 2 == 0 + result[key] = value + end + end + result.keys end h = { @@ -17,5 +23,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..158569d 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -5,6 +5,11 @@ def count(arr) arr.each do |i| # ... + if h[i] == nil + h[i] = 1 + else + h[i] += 1 + end end return h # 回传一个 hash @@ -15,4 +20,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..5f2dcae 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -9,8 +9,15 @@ ] # .... +a = [] +arr.each do |i| + if i["age"] >= 18 + a << i + end +end +a.sort! { |x, y| x["age"] <=> y["age"] } -puts "所有成年人,并由小到大: _________" +puts "所有成年人,并由小到大: #{a}" # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..a4db247 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,10 @@ class Person # ... + attr_accessor :first_name, :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..8d0c12f 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -3,3 +3,15 @@ doc = File.read("wordcount.txt") # ... +arr = doc.split("") +h = {} + +arr.each do |wd| + if h[d] == nil + h[d] = 1 + else + h[d] += 1 + end +end + +puts h diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..ad4494c 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -18,16 +18,24 @@ if command == "add" print "请输入代办事项: " # ... + newTodo = gets.chomp + todos.push(newTodo) elsif command == "remove" print "请输入要删除的编号: " # ... + i = gets.chomp.to_i + todos.delete_at(i) 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..840bb11 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1,4 @@ Buy book Go Shopping -Walk -Gogo +Ruby on Rails +gogo