Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编程基础练习簿——作业 #100

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions 19-filter.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# 给定一阵列内含数字,输出另一个数组只包含偶数


#
# def filter_even(arr)
# arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]
#
# end


arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]
a = []

arr.each do |i|
if i % 2 == 0
puts a.push(i).to_s
puts i # 这部分不知道怎么输出数组了?
end
end


# puts arr.push(x).to_s
#

# puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
20 changes: 20 additions & 0 deletions 28-word-count.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# 请打开 wordcount.txt,计算每个单字出现的次数


doc = File.read("wordcount.txt")

frequency1 = Hash.new(0)
doc.each_char { |chr| frequency1[chr.downcase] +=1 }

puts "Character: "
frequency1.sort_by { |_key, value| value}.each do |key, value|
puts "#{key} : #{value}"
end

puts "-------"

frequency2 = Hash.new(0)
words = doc.to_s.split(" ")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

words = doc.to_s.split 我去掉(“”)后,才会出现单词,要不然出现的和上面一样,是字母。

words.each { |word| frequency2[word.downcase] +=1 }

puts "words: "
frequency2.sort_by {|_key, value| value}.each do |key, value|
puts "#{key} : #{value}"
end

# ...