@smilence
2020-01-28T21:47:29.000000Z
字数 2677
阅读 1090
入门编程与算法
num = 0str_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 windowstr_1[0...-1] # => 'ab'# 1.3 反向reversed_str = str_1.reverse # reversed_str became "cba", str_1 stay the samestr_1.reverse! # str_1 became "cba"# 1.4 查找str_1 = "abc"str_1.include?('w') # => false, 也可以解读为 w 是不是属于str_1中的一个str_1.include?("bc") # => truestr_1.index('b') # => 1str_1.index("bc") # => 1# 1.5 循环i = 0while i < str.length# do something about str[i]i += 1endstr.each_char do |char|# do something about charendstr.each_char.with_index do |char, idx|# do something about char and idxend
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 = 7ele = arr_ints.shift() # => 头部往左shift arr_ints = [2,3,4,5,6], ele = 1arr_ints.unshift(1) # => unshift, 头部插入 [1,2,3,4,5,6]# 2.2 读取arr_ints[0] # => 1arr_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 samearr_ints.reverse! # arr_ints became [5,4,3,2,1]# 2.4 查找arr_ints.include?(2) # truearr_ints.index(2) # => 1# 2.5 循环arr = [1, 2, 3, 4]# while--------i = 0while i < arr.length# do something about arr[i]i += 1end# each --------arr.each do |ele|# do something about eleend# each.with_index --------arr.each.with_index do |ele, idx|# do something about ele and idxend# map --------new_arr = arr.map do |ele|ele * 2 # specify the new element hereend# select --------filtered_arr = arr.select do |ele|ele > 0 # specify the condition for the new element hereend
# 3.1 number range# execute from 1 to 3(1..3).each do |n|# do something about nend# execute from 1, and before 3(1...3).each do |n|# do something about nend# execute 3 times3.times do# do same thingend# 3.2 combinationsarr = ['A', 'K', 'Q']# 3.2.1 list element pairshands = []arr.each do |ele1|arr.each do |ele2|hands << ele1 + ele2endend# 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 Qhands = []arr.each.with_index do |ele1, idx1|arr.each.with_index do |ele2, idx2|if idx2 > idx1hands << ele1 + ele2endendend# 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"] = 2hash["KK"] = 3 # hash = { "AA" => 2, "KK" => 3 }# 4.2 读取hash["KK"] # => 3hash["QQ"] # => nil# 4.3 用于记录profileprofile = {"name" => "April""age" => 18"height" => 171}profile["age"] # => 18# 4.4 用于计数counter = Hash.new(0) # 新建一个Hash, 默认value总是0str = "neswnsws"str.each_char do |char|counter[char] += 1endcounter["n"] == counter["s"] # false# 4.5 hash的遍历counter.each do |k, v|# do something about k and vendpair_arr = count.sort_by {|k,v| v} # => [["e", 1], ["n", 2], ["w", 2], ["s", 3]]pair_arr[-1][0] # => get the max element, "s"