diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..11399c5 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -4,5 +4,5 @@ your_name = gets # ... - -puts "(请替换成最后的答案)" \ No newline at end of file +print your_name + "is " +puts "smartful" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..9555713 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -1,7 +1,7 @@ # 题目: 交换 a, b 变数的值 -a = 1 -b = 2 +a = 7 +b = 8 puts "a 是 #{a}" puts "b 是 #{b}" @@ -10,4 +10,3 @@ puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..f5ba257 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -1,5 +1,7 @@ # 题目: 使用者输入直角三角形的宽和高,输出三角形的面积 + + print "请输入直角三角形的高,然后按 Enter: " a = gets @@ -8,4 +10,6 @@ # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{ a.to_f * b.to_f / 2 }" + +#second time diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..6aa3b45 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).floor } 片" +puts "还剩下几片: #{ pizzas.to_i % people.to_i } " diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..eec7755 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -4,14 +4,23 @@ # 如果 BMI >= 24,显示过重 # 如果 BMI 介于 18.5 ~ 24,显示正常 + print "请输入您的体重(公斤),然后按 Enter: " weight = gets print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +BMI = (weight.to_f / (height.to_f/100) ** 2).round(2) + +puts "您的 BMI 是: #{ BMI }" -puts "您的 BMI 是: _________" +if BMI < 18.5 + puts "您的 BMI 结果是: 过轻" +elsif BMI > 24 + puts "您的 BMI 结果是: 过重" +else + puts "您的 BMI 结果是: 正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +#bug :输入任意非数字串 diff --git a/06-integer-positive.rb b/06-integer-positive.rb new file mode 100644 index 0000000..be3cd77 --- /dev/null +++ b/06-integer-positive.rb @@ -0,0 +1,23 @@ +# 题目: 输入一个数字 x,请判断是否正数、零或负数,以及是不是偶数 + + +print "请输入一个整数,然后按 Enter: " +x = gets + +# .... + +if x.to_f > 0 + puts "正数" +elsif x.to_f < 0 + puts "负数" +else + puts "0" +end + +if x.to_f % 2 == 0 + puts "偶数" +else + puts "奇数" +end + +#bug :输入任意非数字串为0和偶数 diff --git a/06-interger-positive.rb b/06-interger-positive.rb deleted file mode 100644 index a240f5f..0000000 --- a/06-interger-positive.rb +++ /dev/null @@ -1,10 +0,0 @@ -# 题目: 输入一个数字 x,请判断是否正数、零或负数,以及是不是偶数 - - -print "请输入一个整数,然后按 Enter: " -x = gets - -# .... - -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..19e8b4d 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -17,6 +17,28 @@ print "请输入一个整数z,然后按 Enter: " z = gets -# .... +q = x.to_f +w = y.to_f +e = z.to_f -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if q < 0 + puts "A" +elsif q > 0 && w > 0 + if e > 0 + puts "B" + elsif e < 0 + puts "C" + else + puts "none1" + end +elsif q > 0 && w < 0 + if e > 0 + puts "D" + elsif e < 0 + puts "E" + else + puts "none2" + end +else + puts "none3" +end diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..3b52244 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,18 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +q = x.to_f +w = y.to_f +e = z.to_f + +if q >= w && q >= e + puts "最大的数是 #{x}" +elsif w >= q && w >= e + puts "最大的数是 #{y}" +elsif e >= w && e >= q + puts "最大的数是 #{z}" +else + puts "none" +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +# .... diff --git a/09-function.rb b/09-function.rb index b1f922d..acc468e 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..1701b16 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,20 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + q = x.to_f + w = y.to_f + e = z.to_f + + if q >= w && q >= e + return x + elsif w >= q && w >= e + return y + elsif e >= w && e >= q + return z + else + return "none" + end + end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +30,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..67c1cc9 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,9 @@ i = 1 while ( i <= 100 ) - - # .... + if (7*i) < 100 + puts 7*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..dc29380 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -1,13 +1,11 @@ # 题目: 求 1~100 所有偶数的和 -i = 1 +i = 0 total = 0 while ( i <= 100 ) - - # .... - - i+=1 + total += i + i+=2 end -puts total \ No newline at end of file +puts total diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..bff10d2 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -2,10 +2,19 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i -# while ( ... ) -# while ( ...) -# -# end -# end +i = 0 +while ( i <= n ) + j = 0 + + while ( j <= n) + + puts "#{i} * #{j} = #{i*j}" + j += 1 + + end + + i += 1 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..7a3a77c 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,11 +1,26 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + if n < 2 + return false + elsif n == 2 || n == 3 + return true + else + i = 2 + while ( i <= (n/2) ) + if (n % i) != 0 + return true + else + return false + end + i += 1 + end + end end print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_f if is_prime(n.to_i) puts "这是质数" diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..f577082 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,13 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - 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..5312ced 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,17 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + max = array[0] + for i in 1..((array.length)-1) + if array[i] > max + max = array[i] + end + end + + return max end -arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] +arr = [888, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) -puts "Max is #{max}" # 应该是 88 - +puts "Max is #{max}" # 应该是 888 diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..40e3723 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,18 @@ end end +total = 0 +for i in arr + total += i +end + +average = total.to_f / arr.length +max = arr.max +min = arr.min + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 #{total}" +puts "平均是 #{average}" +puts "最大值是 #{max}" +puts "最小值是 #{min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..8bfe5d6 100644 --- a/18-square.rb +++ b/18-square.rb @@ -4,7 +4,10 @@ print "请输入数字 N,然后按 Enter: " n = gets +n = n.to_i -# ... +for i in 0..n-1 + 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..7bbcbde 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,20 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 + + + def filter_even(arr) - #... + result = [] + + arr.each do |i| + if i % 2 == 0 + result << i + end + end + + return result end -arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1, 0] -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..82dbeb0 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,13 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + arr.sort! + arr.uniq! + arr end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] +a = filter_even(arr).to_s -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +puts "#{a}" # 应该是 [42, 46, 68, 86] diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..890ec4c 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,25 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + + for i in (0..arr.size-1) + for j in (i+1)..(arr.size-1) + if arr[j] < arr[i] + tmp = arr[i] + arr[i] = arr[j] + arr[j] = tmp + end + end + 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.inspect +# 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] +# inspect also aliased as: to_s diff --git a/22-missing.rb b/22-missing.rb index 6898714..af70410 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,9 +1,24 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + result = [] + flag = [0,0,0,0,0,0,0,0,0,0] + + for a in arr + flag[a] = 1 #index 位置为1 + end + + for i in 0..(flag.size-1) + if ( flag[i] == 0 ) + result << i + end + end + + result end -answer = find_missing( [2,2,1,5,8,4] ) +arr = [2,2,1,5,8,4] + +answer = find_missing( arr ).inspect -puts answer.to_s # 应该是 [0,3,6,7,9] +puts answer # 应该是 [0,3,6,7,9] diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..443117d 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,8 @@ -# 给定一 Hash,输出有最大 value 的 key +#给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + result = hash.values.max + hash::key(result) end h = { @@ -11,9 +12,7 @@ def find_max(hash) "d" => 80, "e" => 10 } - +# answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..c2c880b 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -2,7 +2,17 @@ def find_even_keys(hash) - # ... (请回传一个数组) + result1 = [] + h = hash.values + for i in h + result1 << i if i % 2 == 0 + end + + result2 = [] + for j in result1 + result2 << hash::key(j) + end + result2 end @@ -17,5 +27,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..777c767 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -3,8 +3,9 @@ def count(arr) h = {} - arr.each do |i| - # ... + arr.uniq.each do |i| + a = arr.count(i) + h[i] = a end return h # 回传一个 hash @@ -15,4 +16,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..4c86a8e 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,10 +7,30 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] +def filter_and_sort(arr) -# .... + h = [] + arr.each do |a| + if a["age"] > 18 + h << a + end + h + end -puts "所有成年人,并由小到大: _________" + (0..h.length-1).each do |i| + minname, index = h[i], i + (i+1..h.length-1).each do |j| + minname, index = h[j], j if h[j]["age"] < minname["age"] + h[i], h[index] = h[index], h[i] #这句和 21-selection-sort 一样 ,但此句未理解 + end + end + return h + +end + +h = filter_and_sort(arr) + +puts "所有成年人,并由小到大: #{h}" # 答案应该是 #[ @@ -18,3 +38,21 @@ # { "name" => "Peter", "age" => 30 }, # { "name" => "David", "age" => 45 } #] + +# 另一种思路 +# ages = [] +# arr.each do |h| +# ages.push(h["age"]) +# end +# ages = ages.sort +# newArr = [] +# for age in ages +# arr.each do |h| +# if age == h["age"] +# newArr.push(h) +# break +# end +# end +# end +# +# puts "所有成年人,并由小到大: #{newArr}" diff --git a/27-class.rb b/27-class.rb index 8cec2c9..138c333 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 "Hi, #{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..cdcc464 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,13 @@ doc = File.read("wordcount.txt") -# ... +words = doc.split # words = doc.split(" ") + +words_count = {} + +for w in words + words_count[w] ||= 0 + words_count[w] += 1 +end + +puts(words_count) diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..5c907f8 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -1,5 +1,4 @@ # 简易 Todo 代办事项应用 - text = File.read("todos.txt") todos = [] @@ -17,17 +16,35 @@ if command == "add" print "请输入代办事项: " - # ... + + task = gets.chomp + todos << task + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end + + elsif command == "remove" print "请输入要删除的编号: " - # ... + + task = gets.chomp + todos.delete_at(task.to_i) + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end + elsif command == "save" puts "存盘离开" - # ... + File.open("todos.txt", "w+") do |f| + todos.each do |todo| + f << todo + f << "\n" + end + end + break; else puts "看不懂,请再输入一次" end end - diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..0f0b1c8 --- /dev/null +++ b/README.txt @@ -0,0 +1 @@ +for changing name