Arrays 字符串填充和切片到固定长度

Arrays 字符串填充和切片到固定长度,arrays,ruby,string,padding,slice,Arrays,Ruby,String,Padding,Slice,对于一些只有1和0的随机长度数组,我需要使其开始和结束总是分别为001和100 一些字符串形式的示例,以提高可读性 "11010" => "00110100" "01010" => "0010100" "10000" => "00100" "01100" => "001100" "11111" => "001111100" "00100" => "00100" "00010" => "00100" "00000" => "" # cases

对于一些只有1和0的随机长度数组,我需要使其开始和结束总是分别为
001
100

一些字符串形式的示例,以提高可读性

"11010" => "00110100"
"01010" => "0010100"
"10000" => "00100"
"01100" => "001100"
"11111" => "001111100"
"00100" => "00100"
"00010" => "00100"
"00000" => ""    # cases with no 1's are ignored
我用的代码是shift/unshift-push/pops-until

def norm(arr)
  (arr.index(1) > 2 ? arr.shift : arr.unshift(0)) until arr.index(1) == 2
  (arr.rindex(1) > arr.length-3 ? arr.push(0) : arr.pop) until arr.rindex(1) == arr.length-3
  arr
end

是否有更好的(某种程度上)填充、切片等方法?

您可以忽略第一次
1
之前和最后一次
1
之后的任何
0
。所以

def norm(arr)
  if arr.include?('1')
    ['0', '0'] + arr.slice(arr.index('1')..arr.rindex('1')) + ['0', '0']
  else
    []
  end
end

您可以忽略第一次
1
之前和最后一次
1
之后的任何
0
。所以

def norm(arr)
  if arr.include?('1')
    ['0', '0'] + arr.slice(arr.index('1')..arr.rindex('1')) + ['0', '0']
  else
    []
  end
end

您可以忽略第一次
1
之前和最后一次
1
之后的任何
0
。所以

def norm(arr)
  if arr.include?('1')
    ['0', '0'] + arr.slice(arr.index('1')..arr.rindex('1')) + ['0', '0']
  else
    []
  end
end

您可以忽略第一次
1
之前和最后一次
1
之后的任何
0
。所以

def norm(arr)
  if arr.include?('1')
    ['0', '0'] + arr.slice(arr.index('1')..arr.rindex('1')) + ['0', '0']
  else
    []
  end
end
您可以与正则表达式一起使用:

r = /
    \A0*   # match beginning of the string followed by zero or more zeroes
    (?=1)  # match 1 in a positive lookahead
    |      # or
    (?<=1) # match 1 in a positive lookbehind
    0*\z   # match zero of more zero's followed by the end of string
    /x     # regex "extended" or "free spacing" mode 

arr = %w| 11010 01010 10000 01100 11111 00100 00010 00000 |
  #=> ["11010", "01010", "10000", "01100", "11111", "00100", "00010", "00000"] 

arr.each { |s| puts "#{s} -> #{ s.gsub(r,'00') }" }
# 11010 -> 00110100
# 01010 -> 0010100
# 10000 -> 00100
# 01100 -> 001100
# 11111 -> 001111100
# 00100 -> 00100
# 00010 -> 00100
# 00000 -> 00000
r=/
\A0*#匹配字符串的开头,后跟零或多个零
(?=1)#以积极的前瞻性匹配1
|#或
(?可以与正则表达式一起使用:

r = /
    \A0*   # match beginning of the string followed by zero or more zeroes
    (?=1)  # match 1 in a positive lookahead
    |      # or
    (?<=1) # match 1 in a positive lookbehind
    0*\z   # match zero of more zero's followed by the end of string
    /x     # regex "extended" or "free spacing" mode 

arr = %w| 11010 01010 10000 01100 11111 00100 00010 00000 |
  #=> ["11010", "01010", "10000", "01100", "11111", "00100", "00010", "00000"] 

arr.each { |s| puts "#{s} -> #{ s.gsub(r,'00') }" }
# 11010 -> 00110100
# 01010 -> 0010100
# 10000 -> 00100
# 01100 -> 001100
# 11111 -> 001111100
# 00100 -> 00100
# 00010 -> 00100
# 00000 -> 00000
r=/
\A0*#匹配字符串的开头,后跟零或多个零
(?=1)#以积极的前瞻性匹配1
|#或
(?可以与正则表达式一起使用:

r = /
    \A0*   # match beginning of the string followed by zero or more zeroes
    (?=1)  # match 1 in a positive lookahead
    |      # or
    (?<=1) # match 1 in a positive lookbehind
    0*\z   # match zero of more zero's followed by the end of string
    /x     # regex "extended" or "free spacing" mode 

arr = %w| 11010 01010 10000 01100 11111 00100 00010 00000 |
  #=> ["11010", "01010", "10000", "01100", "11111", "00100", "00010", "00000"] 

arr.each { |s| puts "#{s} -> #{ s.gsub(r,'00') }" }
# 11010 -> 00110100
# 01010 -> 0010100
# 10000 -> 00100
# 01100 -> 001100
# 11111 -> 001111100
# 00100 -> 00100
# 00010 -> 00100
# 00000 -> 00000
r=/
\A0*#匹配字符串的开头,后跟零或多个零
(?=1)#以积极的前瞻性匹配1
|#或
(?可以与正则表达式一起使用:

r = /
    \A0*   # match beginning of the string followed by zero or more zeroes
    (?=1)  # match 1 in a positive lookahead
    |      # or
    (?<=1) # match 1 in a positive lookbehind
    0*\z   # match zero of more zero's followed by the end of string
    /x     # regex "extended" or "free spacing" mode 

arr = %w| 11010 01010 10000 01100 11111 00100 00010 00000 |
  #=> ["11010", "01010", "10000", "01100", "11111", "00100", "00010", "00000"] 

arr.each { |s| puts "#{s} -> #{ s.gsub(r,'00') }" }
# 11010 -> 00110100
# 01010 -> 0010100
# 10000 -> 00100
# 01100 -> 001100
# 11111 -> 001111100
# 00100 -> 00100
# 00010 -> 00100
# 00000 -> 00000
r=/
\A0*#匹配字符串的开头,后跟零或多个零
(?=1)#以积极的前瞻性匹配1
|#或

(?与其尝试做任何复杂的事情,我会这样做:

[
  "11010", # => "00110100"
  "01010", # => "0010100"
  "10000", # => "00100"
  "01100", # => "001100"
  "11111", # => "001111100"
  "00100", # => "00100"
  "00010", # => "00100"
  "00000", # => ""    # cases with no 1's are ignored
].each do |s|
  if s['1']
    s[/^0*1/] = '001'
    s[/10*$/] = '100'
  end
  puts s
end
运行它会导致:

# >> 00110100
# >> 0010100
# >> 00100
# >> 001100
# >> 001111100
# >> 00100
# >> 00100
# >> 00000

这是通过首先检查字符串是否包含
1
来实现的。如果包含
0
s,则代码将任何数量的前导
0
s和
1
替换为
001
,然后将尾随
1
替换为任何数量的
0
s,替换为
100
,而不是尝试做任何复杂的事情,我会d这样做:

[
  "11010", # => "00110100"
  "01010", # => "0010100"
  "10000", # => "00100"
  "01100", # => "001100"
  "11111", # => "001111100"
  "00100", # => "00100"
  "00010", # => "00100"
  "00000", # => ""    # cases with no 1's are ignored
].each do |s|
  if s['1']
    s[/^0*1/] = '001'
    s[/10*$/] = '100'
  end
  puts s
end
运行它会导致:

# >> 00110100
# >> 0010100
# >> 00100
# >> 001100
# >> 001111100
# >> 00100
# >> 00100
# >> 00000

这是通过首先检查字符串是否包含
1
来实现的。如果包含
0
s,则代码将任何数量的前导
0
s和
1
替换为
001
,然后将尾随
1
替换为任何数量的
0
s,替换为
100
,而不是尝试做任何复杂的事情,我会d这样做:

[
  "11010", # => "00110100"
  "01010", # => "0010100"
  "10000", # => "00100"
  "01100", # => "001100"
  "11111", # => "001111100"
  "00100", # => "00100"
  "00010", # => "00100"
  "00000", # => ""    # cases with no 1's are ignored
].each do |s|
  if s['1']
    s[/^0*1/] = '001'
    s[/10*$/] = '100'
  end
  puts s
end
运行它会导致:

# >> 00110100
# >> 0010100
# >> 00100
# >> 001100
# >> 001111100
# >> 00100
# >> 00100
# >> 00000

这是通过首先检查字符串是否包含
1
来实现的。如果包含
0
s,则代码将任何数量的前导
0
s和
1
替换为
001
,然后将尾随
1
替换为任何数量的
0
s,替换为
100
,而不是尝试做任何复杂的事情,我会d这样做:

[
  "11010", # => "00110100"
  "01010", # => "0010100"
  "10000", # => "00100"
  "01100", # => "001100"
  "11111", # => "001111100"
  "00100", # => "00100"
  "00010", # => "00100"
  "00000", # => ""    # cases with no 1's are ignored
].each do |s|
  if s['1']
    s[/^0*1/] = '001'
    s[/10*$/] = '100'
  end
  puts s
end
运行它会导致:

# >> 00110100
# >> 0010100
# >> 00100
# >> 001100
# >> 001111100
# >> 00100
# >> 00100
# >> 00000

首先检查字符串是否包含
1
。如果包含,则代码会用
001
替换任何数量的前导
0
s和
1
,然后用
100
替换任何数量的
0
,我建议等待时间超过27分钟选择答案。通常最好的答案需要一段时间来研究和开发。你不认为现在选择答案有点早吗?我正在修改我的答案,@theTinMan刚刚发布了一个答案,其他人可能也在研究答案。不清楚你所说的“只有1和0的数组”是什么意思但随后显示字符串。您的代码也意味着您要传入数组,但差异令人困惑。请编辑您的问题,明确说明这一点。我是否应该将示例格式化为数组?
[1,1,0,0,1,0]=>[0,0,1,1,0,0,1,0]
这会比
110010=>001100100
更清楚吗?你不能在中途实质性地改变你的问题,因为这会使评论和答案变得令人费解和毫无意义,甚至可能导致完美的答案被否决。此外,你把问题改成“整数”会让情况更糟因为整数
001
1
相同,这使得问题毫无意义。您应该编辑并“回滚”你的问题很重要。是的,你是新手,但你不认为在发布问题之前你应该做一点调查吗?这是一个开始。我建议在选择答案之前等待超过27分钟。通常情况下,最好的答案需要一段时间来研究和开发。你不认为现在开始有点早吗选择答案?我正在修改我的答案,@theTinMan刚刚发布了一个答案,其他人可能也在研究答案。不清楚你所说的“只有1和0的数组”是什么意思但随后显示字符串。您的代码也意味着您要传入数组,但差异令人困惑。请编辑您的问题,明确说明这一点。我是否应该将示例格式化为数组?
[1,1,0,0,1,0]=>[0,0,1,1,0,0,1,0]
这会比
110010=>001100100
更清楚吗?你不能在中途实质性地改变你的问题,因为这会使评论和答案变得令人费解和毫无意义,甚至可能导致完美的答案被否决。此外,你把问题改成“整数”会让情况更糟因为整数
001
1
相同,这使得问题毫无意义。您应该编辑并“回滚”你的问题很重要。是的,你是新手,但你不认为在发布问题之前你应该做一点调查吗?这是一个开始。我建议在选择答案之前等待超过27分钟。通常情况下,最好的答案需要一段时间来研究和开发。你不认为现在开始有点早吗选择一个答案?我正在修改我的答案,@theTinMan刚刚发布了一个答案,其他人可能也在研究答案。不清楚“只有1和0的数组”是什么意思,但随后会显示字符串。您的代码暗示您也在传递数组,但差异是c