【问题标题】:How do I apply a division operator on a string in Ruby?如何在 Ruby 中对字符串应用除法运算符?
【发布时间】:2016-03-02 03:07:17
【问题描述】:

我正在尝试将除法运算符/ 添加到String,它需要一个整数。

运算符应该产生一个字符串数组。数组的大小是给定的整数,它的元素是原始字符串的子字符串,这样,当按顺序连接时,就会产生原始字符串。

如果字符串长度不能被整数整除,则某些子字符串(一个字符)应该比其他子字符串长。两个字符串的长度不能相差超过一个,任何较长的字符串都应该出现在任何较短的字符串之前。

像这样:

"This is a relatively long string" / 7
# => ["This ", "is a ", "relat", "ively", " lon", "g st", "ring"]

我该如何开始?

【问题讨论】:

  • 我记得有一个 Rails 方法可以做到这一点或类似的东西,但我忘记了方法名称。我认为它使用正则表达式。
  • 请解释为什么该数组中有大小为 5 的字符串。
  • @Aetherus 数组中应该有 num 字符串,如果有余数,每个字符串的长度相等或长度为 +/- 1。
  • 这有什么实际用途?
  • 我已经为这个难题提供了一个解决方案。谢谢。

标签: ruby-on-rails arrays ruby string divide-by-zero


【解决方案1】:

你可以使用递归。

class String
  def /(n)
    return [self] if n==1
    m = (self.size.to_f/n).ceil
    [self[0...m]].concat(self[m..-1] / (n-1))
  end
end

str = "This would be a woefully short string had I not padded it out."

str / 7
  # => ["This woul", "d be a wo", "efully sh", "ort strin", "g had I n",
  #     "ot padded", " it out."] 


(str / 7).map(&:size)
  #=> [10, 10, 9, 9, 9, 9, 9]

【讨论】:

    【解决方案2】:
    class String
      def /(num)
          n, rem = self.size.divmod(num)
          p = 0
          res = []
          rem.times{res << self[p..p+n]; p+=n+1} 
          (num-rem).times{res << self[p...p+n]; p+=n}
          res
      end
    end
    
    p "This is a relatively long string" / 7
    

    ["This ", "is a ", "relat", "ively", " lon", "g st", "ring"]
    

    【讨论】:

      【解决方案3】:

      这可行:

      class String
        def /(n)
          chars.in_groups(n).map(&:join) 
        end
      end
      
      "This is a relatively long string" / 7
      #=> ["This ", "is a ", "relat", "ively", " lon", "g st", "ring"]
      

      in_groups 是一种 Rails 方法,可将数组拆分为 n 个组。

      【讨论】:

      • 迄今为止最好的答案,imo。 Rails 中似乎有许多很酷的“纯 Ruby”方法,由于某种原因,Ruby 没有采用。
      【解决方案4】:

      这是我的解决方案,有点庞大但 O(n) 时间。

      如果有任何边缘情况失败,请告诉我:

      class String
        def /(num)
      
          if num > self.length
            return [] # or whatever, since this is an edge case / can't be done
          end
      
          remainder = self.length % num
          result = []
          substr = ""
      
          i = 1
          while i <= self.length
            substr += self[i-1]
      
            if i % (self.length / num) == 0
              if remainder > 0
                substr += self[i]
                i += 1
                remainder -= 1
              end
      
              result << substr
              substr = ""
            end
      
            i += 1
          end
      
          result
        end
      end
      

      编辑:重构 - 用子字符串替换子数组

      【讨论】:

        【解决方案5】:
        class String
          def /(num)
            partition_size = length / num
            if length % num == 0
              scan /.{#{partition_size}}/
            else
              scan /.{#{partition_size},#{partition_size + 1}}/
            end  
          end
        end
        

        【讨论】:

        • 请给我角落的箱子。
        • "hellohello" / 4 将其拆分为 3 而不是 4
        猜你喜欢
        • 1970-01-01
        • 2013-11-29
        • 1970-01-01
        • 2019-07-25
        • 2014-11-11
        • 2015-02-08
        • 2011-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多