@smilence
2020-01-29T05:47:29.000000Z
字数 2677
阅读 796
入门编程与算法
num = 0
str_1 = "ab"
str_2 = "xy"
# 1.1 插入, 结合
str_1 + str_2 # => "abxy"
str_1 + num.to_s # => "ab0"
str_2 + 'z' # => "xyz"
str_1 << 'c' # => str_1 became "abc"
# 1.2 读取
str_1[0] # => 'a'
str_1[1..-1] # => 'bc' 取substring window
str_1[0...-1] # => 'ab'
# 1.3 反向
reversed_str = str_1.reverse # reversed_str became "cba", str_1 stay the same
str_1.reverse! # str_1 became "cba"
# 1.4 查找
str_1 = "abc"
str_1.include?('w') # => false, 也可以解读为 w 是不是属于str_1中的一个
str_1.include?("bc") # => true
str_1.index('b') # => 1
str_1.index("bc") # => 1
# 1.5 循环
i = 0
while i < str.length
# do something about str[i]
i += 1
end
str.each_char do |char|
# do something about char
end
str.each_char.with_index do |char, idx|
# do something about char and idx
end
arr_ints = [1, 2, 3]
arr_strs = ["I", "am", "April"]
# 2.1 插入, 删除, 结合
arr_ints << 4 # => [1, 2, 3, 4]
arr_strs << "Li" # => ["I", "am", "April", "Li"]
arr_ints + [5, 6] # => [1, 2, 3, 4, 5, 6]
arr_ints.push(7) # => 尾部插入 [1, 2, 3, 4, 5, 6, 7]
ele = arr_ints.pop() # => 尾部取出 arr_ints = [1,2,3,4,5,6], ele = 7
ele = arr_ints.shift() # => 头部往左shift arr_ints = [2,3,4,5,6], ele = 1
arr_ints.unshift(1) # => unshift, 头部插入 [1,2,3,4,5,6]
# 2.2 读取
arr_ints[0] # => 1
arr_ints[1..-1] # => [2, 3, 4, 5, 6]
arr_ints[1...-1] # => [2, 3, 4, 5]
# 2.3 反向
reversed = arr_ints.reverse # reversed became [5,4,3,2,1], arr_ints stay same
arr_ints.reverse! # arr_ints became [5,4,3,2,1]
# 2.4 查找
arr_ints.include?(2) # true
arr_ints.index(2) # => 1
# 2.5 循环
arr = [1, 2, 3, 4]
# while--------
i = 0
while i < arr.length
# do something about arr[i]
i += 1
end
# each --------
arr.each do |ele|
# do something about ele
end
# each.with_index --------
arr.each.with_index do |ele, idx|
# do something about ele and idx
end
# map --------
new_arr = arr.map do |ele|
ele * 2 # specify the new element here
end
# select --------
filtered_arr = arr.select do |ele|
ele > 0 # specify the condition for the new element here
end
# 3.1 number range
# execute from 1 to 3
(1..3).each do |n|
# do something about n
end
# execute from 1, and before 3
(1...3).each do |n|
# do something about n
end
# execute 3 times
3.times do
# do same thing
end
# 3.2 combinations
arr = ['A', 'K', 'Q']
# 3.2.1 list element pairs
hands = []
arr.each do |ele1|
arr.each do |ele2|
hands << ele1 + ele2
end
end
# hands = ["AA", "AK", "AQ", "KK", "KA", "KQ", "QA", "QK", "QQ"] ~ 3*3 = 9
# 3.2.2 list unique pairs
# assume only one A, one K, one Q
hands = []
arr.each.with_index do |ele1, idx1|
arr.each.with_index do |ele2, idx2|
if idx2 > idx1
hands << ele1 + ele2
end
end
end
# hands = ["AK", "AQ", "KQ"] ~ 3*2/2 = 3
Hash就是一组 {key => value}
pair, 可以把array看做key为index 0,1,2,3...
的特殊hash
hash = {}
# 4.1 写入
hash["AA"] = 2
hash["KK"] = 3 # hash = { "AA" => 2, "KK" => 3 }
# 4.2 读取
hash["KK"] # => 3
hash["QQ"] # => nil
# 4.3 用于记录profile
profile = {
"name" => "April"
"age" => 18
"height" => 171
}
profile["age"] # => 18
# 4.4 用于计数
counter = Hash.new(0) # 新建一个Hash, 默认value总是0
str = "neswnsws"
str.each_char do |char|
counter[char] += 1
end
counter["n"] == counter["s"] # false
# 4.5 hash的遍历
counter.each do |k, v|
# do something about k and v
end
pair_arr = count.sort_by {|k,v| v} # => [["e", 1], ["n", 2], ["w", 2], ["s", 3]]
pair_arr[-1][0] # => get the max element, "s"