diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..0380a8f 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,5 @@ # 题目: 输入名字,输出 "Hello, 名字" -print "请输入你的名字,然后按 Enter: " +print "请输入你的\"名字\",然后按 Enter: " your_name = gets - -# ... - -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hi,#{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..88f9260 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -7,7 +7,10 @@ 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..d38648a 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -1,11 +1,11 @@ # 题目: 使用者输入直角三角形的宽和高,输出三角形的面积 print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = (gets).to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets - +b = (gets).to_i +c = (a*b)/2 # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: ___#{c}______" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..3918da7 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -5,8 +5,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets +#left = pizzas.to_i - (pizzas.to_i / people.to_i)*people.to_i +left = pizzas.to_i % people.to_i -# ..... - -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{ pizzas.to_i / people.to_i}片" +puts "还剩下几片: #{left}片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..eb262d0 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,17 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +bmi = weight.to_f/(height.to_f*height.to_f) -puts "您的 BMI 是: _________" +if bmi>=24.0 + result = "过重" +elsif bmi<18.5 + result = "过轻" +else + result = "正常" +end -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file + +puts "您的 BMI 是: #{bmi}" + +puts "您的 BMI 结果是: #{result}" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..5bdb2d6 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -2,9 +2,22 @@ print "请输入一个整数,然后按 Enter: " -x = gets +y = (gets).to_i +puts y +if y == 0 + x = "0" +elsif y > 0 + x = "正数" +else + x = "负数" +end +if y % 2 == 0 + z = "偶数" +else + z = "奇数" +end # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +puts "这个数是#{x}" +puts "这个数是#{z} (偶数或奇数)" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..bde93f5 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,28 @@ # 当 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 -# .... +if x < 0 + result = "A" +elsif x > 0 + if y > 0 && z > 0 + result = "B" + elsif y >0 && z < 0 + result = "C" + elsif y < 0 && z > 0 + result = "D" + else + result = "E" + end +else + result = "0" +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +puts "结果是#{result}" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..a359a1f 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -1,14 +1,40 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 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 -# .... +if x > y + if x > z + result = "x" + elsif x < z + result = "z" + else + result = "x和z 并列最大" + end +elsif x = y + if x > z + result = "x和y 并列最大" + elsif x < z + result = "z" + else + result = "x y z 一样大" + end +else + if x >= z + result = "Y" + elsif y > z + result = "Y" + elsif y < z + result = "z" + else + result = "y和z 并列最大" + end +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 #{result} (x或y或z)" diff --git a/09-function.rb b/09-function.rb index b1f922d..2d9b462 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + return (a*b)/2 end print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = (gets).to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets +b = (gets).to_i 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..584c44c 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,46 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y + if x > z + return result = "x" + elsif x < z + return result = "z" + else + return result = "x和z 并列最大" + end + elsif x = y + if x > z + return result = "x和y 并列最大" + elsif x < z + return result = "z" + else + return result = "x y z 一样大" + end + else + if x >= z + return result = "Y" + elsif y > z + return result = "Y" + elsif y < z + return result = "z" + else + result = "y和z 并列最大" + end + 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}" diff --git a/11-seven.rb b/11-seven.rb index 26c221d..b308999 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,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..5a74ad9 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,11 @@ while ( i <= 100 ) - # .... + if i % 2 == 0 + 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..71b76cb 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,12 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end - +n = (gets).to_i +i = 1 +while ( i < (n+1) ) + # while ( ...) + # + # end + puts "#{n} * #{i} = #{i*n}" + i += 1 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..c3b851d 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,7 +1,17 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + r = true + while i < n + + if n % i == 0 + r = false + break + end + i +=1 + end + return r end print "请输入数字 N,然后按 Enter: " diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..aa2c5f8 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,18 @@ while (true) print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - - if n.to_i == target + if n.to_i < target + puts "低了,再猜一次" + elsif n.to_i > target + puts "太高了,再猜一次" + else puts "恭喜猜中啦! " break end -end \ No newline at end of file + # if n.to_i == target + # puts "恭喜猜中啦! " + # break + # end + +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..49d74ff 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,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也行。 +arr = [8, 12, 36, 93, 9, 75] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..7601f66 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,26 @@ end end +total = 0 +max = arr[0] +min = arr[0] + +arr.each do |i| + total = total + i + if max <= i + max = i + end + + if min >=i + min = i + end +end + +avg = total.to_f / arr.size + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是#{total}" +puts "平均是#{avg}" +puts "最大值是#{max}" +puts "最小值是#{min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..d55e9e8 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,12 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets - +n = (gets).to_i +i = 0 +while i < n + arr << i*i + i +=1 +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..19b76ca 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + even_arr = [] + arr.each do |i| + if i % 2 == 0 + even_arr.push(i) + end + end + even_arr 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..d5bc776 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,16 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + even_arr = [] + arr.each do |i| + if i % 2 == 0 + even_arr.push(i) + end + end + even_arr.uniq.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..de0312b 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -2,11 +2,32 @@ # https://zh.wikipedia.org/wiki/选择排序 def selection_sort(arr) - #... + temp = [] + i = 0 + l = arr.size - 1 + # 双循环:外面的循环,是按顺序从前向后依次提取值,直到倒数第二个结束。 + # 嵌套的循环,是让外循环提取的值和后续的值进行对比。 + # 所以两个循环的次数都是n-1 + while i < l + min = i + j = i + 1 + while j < arr.size + if arr[min] > arr[j] + min = j + end + j += 1 + end + temp = arr[i] + arr[i] = arr[min] + arr[min] = temp + + i += 1 + 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.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] diff --git a/22-missing.rb b/22-missing.rb index 6898714..27dc05e 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -1,7 +1,24 @@ # 给定一阵列内含数字,请输出 0~9 中不见的数字 def find_missing(arr) - # ... + contrast = [1,2,3,4,5,6,7,8,9] + result = [] + + contrast.each do |c| + arr_have = false + + arr.each do |a| + if c == a + arr_have = true + break + end + end + if arr_have == false + result << c + end + end + + return result end answer = find_missing( [2,2,1,5,8,4] ) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..c52d21b 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,15 @@ # 给定一 Hash,输出有最大 value 的 key def find_max(hash) - # ... + arr = hash.values + arr_max = arr.max + re_string = '' + hash.each do |key, value| + if value == arr_max + re_string = re_string + key + " " + end + end + return re_string end h = { @@ -9,11 +17,9 @@ def find_max(hash) "b" => 38, "c" => 21, "d" => 80, - "e" => 10 + "e" => 80 } answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..4c704c9 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,13 @@ # 给定一 Hash,输出 value 是偶数的 keys def find_even_keys(hash) - - # ... (请回传一个数组) - + even_key = [] + hash.each do |key, value| + if value % 2 == 0 + even_key.push(key) + end + end + return even_key end h = { @@ -17,5 +21,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..af3045f 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -3,8 +3,32 @@ def count(arr) h = {} + #相当于 uniq + # count = [] + # arr.each do |i| + # if count.include?("i") + # else + # count << i + # end + # end + # + # count.each do |i| + # h[i] = arr.count(i) + # end + + + # arru = arr.uniq + # arru.each do |i| + # h[i] = arr.count(i) + # end + + # 另外一种高效的写法 arr.each do |i| - # ... + if h[i] == nil + h[i] = 1 + else + h[i] = h[i] + 1 + end end return h # 回传一个 hash @@ -15,4 +39,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..ff2baa0 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -8,9 +8,19 @@ { "name" => "Vincent", "age" => 6 }, ] -# .... +# 找出成年人,生成新的数组 +adult = [] +arr.each do |i| + if i["age"] > 18 + adult << i + end +end -puts "所有成年人,并由小到大: _________" +# 排序 +result = adult.sort_by{|i| i["age"] } + + +puts "所有成年人,并由小到大:#{result}" # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..efa46d2 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,16 +1,27 @@ class Person - # ... -end - -p1 = Person.new -p1.first_name = "Peter" -p1.last_name = "Wang" -p1.greet # 输出 "Hello, Peter Wang" + attr_accessor :first_name, :last_name -p2 = Person.new -p2.first_name = "William" -p2.last_name = "Zhang" -p2.greet # 输出 "Hello, William Zhang" + def initialize(first, last) + @first_name = first + @last_name = last + end + def greet + puts "Hello, #{first_name} #{last_name}" + end +end +# p1 = Person.new +# p1.first_name = "Peter" +# p1.last_name = "Wang" +# p1.greet # 输出 "Hello, Peter Wang" +# +# p2 = Person.new +# p2.first_name = "William" +# p2.last_name = "Zhang" +# p2.greet # 输出 "Hello, William Zhang" +p3 = Person.new("王", "明") +p3.first_name +p3.last_name +p3.greet diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..dbd936e 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,18 @@ doc = File.read("wordcount.txt") -# ... +h = {} + +doc.each_line { |line| + words = line.split + words.each { |w| + word = w.gsub(/[,()'".]/, '').downcase + if h.has_key?(word) + h[word] = h[word] + 1 + else + h[word] = 1 + end + } +} + +puts h diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..caec58b 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -2,6 +2,7 @@ text = File.read("todos.txt") + todos = [] text.each_line do |line| todos << line.chomp @@ -11,23 +12,29 @@ puts "#{index}: #{todo}" end + while (true) print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " command = gets.chomp if command == "add" print "请输入代办事项: " - # ... + new_event = gets.chomp + todos << new_event elsif command == "remove" print "请输入要删除的编号: " - # ... + code = gets.chomp.to_i + todos.delete_at(code) 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/arithmetic/01-bucket.rb b/arithmetic/01-bucket.rb new file mode 100644 index 0000000..00e3b2e --- /dev/null +++ b/arithmetic/01-bucket.rb @@ -0,0 +1,37 @@ +a = [] +for i in 0..10 do + a[i] = 0 +end +print a + +puts "\n" +a[2] = 1 +a[3] = 1 +a[5] = 2 +a[8] = 1 + +# 正序排列 +for i in 0..10 do + j = 1 + while j <= a[i] + print i + j += 1 + end +end + +puts "\n" +# 反序排列 +i = 10 +while i >=0 + j = 1 + while j <= a[i] + print i + j += 1 + end + i -= 1 +end +puts "\n" +# 结果是: +# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] +# 23558 +# 85532 diff --git a/arithmetic/02-popo.rb b/arithmetic/02-popo.rb new file mode 100644 index 0000000..055c656 --- /dev/null +++ b/arithmetic/02-popo.rb @@ -0,0 +1,33 @@ +# 实践冒泡排序 + +def selection_sort(arr) + i = 0 # 用来计算外循环次数 ,参与内循环次数计算 + temp = [] # 用于冒泡的数据交换 + while i < arr.size - 1 # 只比较n-1次,因为有n个数 + j = 1 # 初始化内循环的key。 + + while j < arr.size - i + # 每次循环内循环结束后,把最大/小值放到数组最后, + # 下一次外循环的内循环,就不比较已经定位的数字了,用每次外循环初始化j = 1来实现。所以每次外循环都会减少一次内循环,用减 i 来实现。 + # 第一次外循环时,内循环比较9次;第二次外循环时,内循环比较8次;依次递减至结束。 + if arr[j-1] <= arr[j] #如果true则交换值 + # 大于等于 是 从小到大排序 + # 小于等于 是 从大到小排序 + temp = arr[j -1] + arr[j-1] = arr[j] + arr[j] = temp + end + j = j + 1 + end + + i = i + 1 + end + + return arr +end + +arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] + +answer = selection_sort(arr) + +puts answer.to_s diff --git a/arithmetic/03-binary.rb b/arithmetic/03-binary.rb new file mode 100644 index 0000000..6f6696a --- /dev/null +++ b/arithmetic/03-binary.rb @@ -0,0 +1,37 @@ + +def quicksort(a,left,right) + if left > right # 设定何时停止该方法的调用。 + return + end + + temp = a[left] #设定最左边的为基准数 + i = left + j= right + while i != j # i和j从两边向中间靠拢,直到i和j重合,结束本轮循环。 + while a[j] <= temp && i < j #顺序很重要,要先从右往左找。找大于基准数的数,则降序排序。 + j -= 1 + end + while a[i] >= temp && i < j #再向➡️找,根据是升序还是降序来选择 比较运算符号,此例找小于基准数的数。 + i += 1 + end + if i < j #当i 和 j 没有相遇的时候,交换数据 + t = a[i] + a[i] = a[j] + a[j] = t + end + end + a[left] = a[i] #将基准数归位。 + a[i] = temp + print a.to_s + "\n" + quicksort(a,left,i-1) # 在quciksort的方法中,再次调用本方法,形成递归的过程。这个是处理i左边。直到穷尽,即当left>right时停止调用该方法。 + quicksort(a,i+1,right) # 这个处理i右边 + +end + + + +a = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] + +quicksort(a,0,9) +# 0和9 代表数组最左边和最右边的key +print a diff --git a/todos.txt b/todos.txt index 4757e85..a0057b6 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1 @@ -Buy book -Go Shopping -Walk -Gogo +work2