Skip to content

Commit 1e19379

Browse files
committed
Add exercise 16~25
1 parent bf844e0 commit 1e19379

10 files changed

+148
-0
lines changed

16-array-sum.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 给定一阵列内含数字,输出最大值
2+
3+
def find_max(array)
4+
#....
5+
end
6+
7+
arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88]
8+
9+
max = find_max(arr)
10+
puts "Max is #{max}" # 应该是 88
11+

17-array-stats.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 使用者不断输入数字存进 Array,最后输出总和、平均、最大值、最小值
2+
3+
arr = []
4+
5+
while (true)
6+
print "请输入数字,结束请直接按 Enter: "
7+
user_input = gets
8+
if user_input == "\n"
9+
break
10+
else
11+
arr << user_input.to_i
12+
end
13+
end
14+
15+
puts arr.to_s
16+
17+
puts "总和是 _____"
18+
puts "平均是 _____"
19+
puts "最大值是 _____"
20+
puts "最小值是 _____"

18-square.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 建构一个阵列有一百个的元素,内容是 0, 1, 4, 9, 16, 25...... 每个元素是该索引的平方
2+
3+
arr = []
4+
5+
print "请输入数字 N,然后按 Enter: "
6+
n = gets
7+
8+
# ...
9+
10+
puts arr.to_s

19-filter.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 给定一阵列内含数字,输出另一个数组只包含偶数
2+
3+
def filter_even(arr)
4+
#...
5+
end
6+
7+
arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]
8+
9+
puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]

20-sorting.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 承上题,请排序并去除重复的数字
2+
# Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复
3+
4+
def filter_even(arr)
5+
#...
6+
end
7+
8+
arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]
9+
10+
11+
puts "________" # 应该是 [42, 46, 68, 86]

21-missing.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 给定一阵列内含数字,请输出 0~9 中不见的数字
2+
3+
def find_missing(arr)
4+
# ...
5+
end
6+
7+
answer = find_missing( [2,2,1,5,8,4] )
8+
9+
puts answer.to_s # 应该是 [0,3,6,7,9]

22-hash-max.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 给定一 Hash,输出有最大 value 的 key
2+
3+
def find_max(hash)
4+
# ...
5+
end
6+
7+
h = {
8+
"a" => 71,
9+
"b" => 38,
10+
"c" => 21,
11+
"d" => 80,
12+
"e" => 10
13+
}
14+
15+
answer = find_max(h)
16+
17+
puts "有最大 value 的是 #{answer}" # 应该是 d
18+
19+

23-hash-even.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 给定一 Hash,输出 value 是偶数的 keys
2+
3+
def find_even_keys(hash)
4+
5+
# ... (请回传一个数组)
6+
7+
end
8+
9+
h = {
10+
"a" => 71,
11+
"b" => 38,
12+
"c" => 21,
13+
"d" => 80,
14+
"e" => 10
15+
}
16+
17+
answer = find_even_keys(h)
18+
19+
puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e]
20+
21+

24-hash-count.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 计算一个阵列中各个元素的出现频率
2+
3+
def count(arr)
4+
h = {}
5+
6+
arr.each do |i|
7+
# ...
8+
end
9+
10+
return h # 回传一个 hash
11+
end
12+
13+
arr = ["a", "d", "d", "c", "b", "c", "c", "c", "d", "d", "e", "e", "e", "d", "a", "c", "e", "a", "d", "e"]
14+
15+
answer = count(arr)
16+
17+
puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5}
18+

25-hash-filter.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 给定一个数组包含 Hash,请过滤和排序
2+
3+
arr = [
4+
{ "name" => "Peter", "age" => 30 },
5+
{ "name" => "John", "age" => 15 },
6+
{ "name" => "David", "age" => 45 },
7+
{ "name" => "Steven", "age" => 22 },
8+
{ "name" => "Vincent", "age" => 6 },
9+
]
10+
11+
# ....
12+
13+
puts "所有成年人,并由小到大: _________"
14+
15+
# 答案应该是
16+
#[
17+
# { "name" => "Steven", "age" => 22 },
18+
# { "name" => "Peter", "age" => 30 },
19+
# { "name" => "David", "age" => 45 }
20+
#]

0 commit comments

Comments
 (0)