@smilence
2020-02-14T14:22:54.000000Z
字数 4221
阅读 981
入门编程与算法
(x..y).each do |idx|
# do something related to idx
end
n.times do
# do something
end
如果string里包含多个词,用空格隔开,见1.3. 如果不是, 则新建new_str
, 用str.each_char
对老string进行遍历后把char挨个插入到new_str
new_str = ""
str.each_char do |char|
new_str << char.upcase # uppercase each char
end
return new_str
处理这样的string, 总是通过str.split(" ")
把问题转换成array问题 (array of strings), 然后对array进行遍历 (arr.each
/ arr.each_with_index
/ arr.map
) 处理每个分隔开的word.
通过str.split(" ")
把问题转换成array问题 (array of strings), 然后把array按照2.1的方法变化成新array new_arr
, 最后再用new_arr.join(" ")
转回为string
parts = str.split(" ")
# 把parts按照题意转换成目标array
new_parts = parts.map do |part|
...
end
return new_parts.join(" ")
e.g.1 Write a method yell_sentence that takes in a sentence string and returns a new sentence where every word is yelled.
def yell_sentence(sent)
parts = sent.split(" ") # 先转换成array问题
# 把array每个元素变换得到另一个array
new_parts = parts.map do |part|
part.upcase + "!"
end
return new_parts.join(" ")
end
e.g.2 Write a method
o_words
that takes in a sentence string and returns new sentence with the words that contain an "o"
def o_words
parts = sent.split(" ") # 先转换成array问题
# 把array每个元素变换得到另一个array
new_parts = parts.select do |part|
# check if word contain an "o"
part.include?("o")
end
return new_parts.join(" ")
end
如果不要求in-place修改,我们总是可以把
string
当做Array
来处理,通过str.chars
arr.map
或者arr.map.with_index
.
# 把array每个元素乘以2得到新array
new_arr = arr.map do |ele|
ele * 2 # 由旧ele得到新ele
end
# 把array每个元素乘以index得到新array
new_arr = arr.map.with_index do |ele, idx|
ele * idx .. # 由旧ele和旧idx 得到新ele
end
e.g. Write a method
map_by_key
that takes in an array of hashes and a key string. The method should returns a new array containing the values from each hash for the given key.
# input: array of hashes
# output: array of hash[key]
# array => new_array
def map_by_key(arr)
return arr.map do |hash|
hash[key] # each hash => each hash[key]
end
end
arr.select
# Write a method, filter_lengths(strings, length), that accepts an array of strings
# and a length as args. The method should return an array containing the strings
# that have at least the given length. The length argument should be optional; if no length
# is passed in, then 5 should be used as the length.
def filter_lengths(strings, length=5)
strings.select { |str| str.length >= length }
end
arr.each do |ele1|
arr.each do |ele2|
# do something about ele1 and
end
end
arr.each_with_index do |ele1, idx1|
arr.each_with_index do |ele2, idx2|
if idx2 > idx1
# do something about ele1 and ele2
arr.each do |subarr|
subarr.each do |ele|
# do something about ele
end
end
n => 1
的结构给定一组元素,得出一个非boolean的结果(boolean
见2.5),通常是array元素同样的类型, 用array.inject
e.g. Write a method
matrix_addition_reloaded
that accepts any number of matrices as arguments. The method should return a new matrix representing the sum of the arguments. Matrix addition can only be performed on matrices of similar dimensions, so if all of the given matrices do not have the same "height" and "width", then return nil.
def matrix_addition_reloaded(*matrices)
matrix = matrices.first
height = matrix.length
width = matrix[0].length
empty_matrix = Array.new(height) { [0] * width }
matrices.inject(empty_matrix) do |m1, m2|
return nil if m2.length != height or m2[0].length != width
matrix_addition(m1, m2)
end
end
给定一组元素, 对某个事实做true or false判断的问题,可以认为是inject
的特殊情况, 用:
全都是 / 只存在 => array.all?
or (0..num).all
# Write a method, `only_vowels?(str)`, that accepts a string as an arg.
# The method should return true if the string contains only vowels.
# The method should return false otherwise.
def only_vowels?(str)
vowels = "aeiou"
str.split("").all? { |char| vowels.include?(char) }
end
有没有 / 至少有一个 => array.any?
or (0..num).any
# Write a method, adult_in_group?(people), that accepts an array containing people.
# The method should return true if there is at least 1 person with an age of 18 or greater.
# The method should return false otherwise.
def adult_in_group?(people)
people.any? { |person| person[:age] >= 18 }
end
完全没有 / 除去某个就没有了 => array.none?
or (0..num).none
Write a method,
coprime?(num_1, num_2)
, that accepts two numbers as args.
The method should return true if the only common divisor between the two numbers is 1.
The method should return false otherwise. For examplecoprime?(25, 12)
is true because
1 is the only number that divides both 25 and 12.
def coprime?(num_1, num_2)
(2..num_1).none? { |factor| num_1 % factor == 0 && num_2 % factor == 0}
end