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.
2.2 对一组元素做一个判断(boolean) 的问题
给定一组元素, 对某个事实做true or false判断的问题,可以认为是inject的特殊情况, 用:
全都是 / 只存在 => array.all? or (0..num).all
有没有 / 至少有一个 => array.any? or (0..num).any
完全没有 / 除去某个就没有了 => array.none? or (0..num).none
e.g. 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 example coprime?(25, 12) is true because 1 is the only number that divides both 25 and 12.
words = str.split(" ")
# 把parts按照题意转换成目标array
new_words = words.map do |part|
new_word = ...
new_word
end
return new_Words.join(" ")
def matrix_addition_reloaded(*matrices)
return matrices.inject() do |acc, m|
return nil if acc.length != m.length || acc[0].length != m[0].length
matrix_addition(acc, m)
end
end
# 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
# 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
def peak_finder(arr)
peaks = []
return [] if arr.length == 0
if arr.length > 0 && arr[0] > arr[1]
peaks << arr[0]
end
(1...arr.length-1).each do |idx|
if arr[idx] > arr[idx-1] && arr[idx] > arr[idx+1]
peaks << arr[idx]
end
end
if arr.length > 0 && arr[-1] > arr[-2]
peaks << arr[-1]
end
return peaks
end
e.g. # p compress_str("aaabbc") # => "3a2bc"
def compress_str(str)
return str if str.empty?
count = 1
new_str = ""
(0...str.length).each do |idx|
if idx != str.length - 1 && str[idx] == str[idx+1]
count += 1
else
new_str += count == 1 ? str[idx] : count.to_s + str[idx]
count = 1
end
end
return new_str
end
# 674. Longest Continuous Increasing Subsequence
def find_length_of_lcis(nums)
return 0 if nums.empty?
max_count, count = 1, 1
(0...nums.length).each do |idx|
# 可以认为越界元素一定不会满足条件
if idx != nums.length-1 && nums[idx] < nums[idx+1]
count += 1
else
max_count = count if count > max_count
count = 1
end
end
return max_count
end
completed = false
until completed
completed = true # 默认这一轮completed为true
(0...arr.length).each do |idx|
# 处理每一个arr[idx]
if condition 满足一定条件下
# 修改arr[idx]
completed = false
end
end
end
return arr
e.g. bubble sort
def bubble_sort(array)
sorted = false
until sorted
sorted = true
(0...array.length - 1).each do |i|
if array[i] > array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
sorted = false
end
end
end
return array
end
def hipsterfy(word)
vowels = "aeiou"
i = word.length - 1
while i >= 0
if vowels.include?(word[i])
return word[0...i] + word[i+1..-1]
end
i -= 1
end
word
end
def is_prime?(num) # 实际上是查找因子
return false if num <= 1
(2..num).each do |n|
return false if num % n == 0
end
return true
end
def perfect_square?(num)
i = 1
square = nil
while square <= num
square = i * i
return true if square == num
i += 1
end
return false
end
def power_of_two?(num)
return true if num == 1
result = 1
while result <= num
result = result * 2
return true if result == num
end
return false
end
def nth_prime(n)
count = 0
num = 1
until count == n
num += 1
if is_prime?(num)
count += 1
end
end
num
end
e.g.
def palindrome?(string)
i, j = 0, string.length - 1
while i < j
return false if string[i] != string[j]
i += 1
j -= 1
end
return true
end
arr.each_with_index do |ele1, idx1|
arr.each_with_index do |ele2, idx2|
if idx2 > idx1
# do something about ele1 and ele2
collection
def substrings(string)
subs = []
(0...string.length).each do |start_idx|
(0...string.length).each do |end_idx|
subs << string[start_idx..end_idx] if start_idx <= end_idx
end
end
subs
end
def unique_chars?(string)
hash = Hash.new(0)
string.each_char do |char|
return false if hash.has_key?(char)
hash[char] += 1
end
true
end