【问题标题】:Ruby method to reverse string inputRuby方法来反转字符串输入
【发布时间】:2014-11-06 02:02:50
【问题描述】:

我不明白为什么 reversed_string=string[i] + reversed_string 将最后一个字符放在首位。似乎 string[i] 将索引第一个字符而不是最后一个字符。因此,如果字符串是“abc”,则索引 0 将是“a”而不是“c”。有人可以解释一下 ruby​​ 如何从索引 0 获取“c”吗?然后,当然,索引 1 中的“b”?等等等等。

编写一个将字符串作为输入的方法,并以相反的顺序返回具有相同字母的新字符串。

难度:简单。

def reverse(string)
  reversed_string = ""

  i = 0
  while i < string.length
    reversed_string = string[i] + reversed_string

    i += 1
  end

  return reversed_string
end
puts("reverse(\"abc\") == \"cba\": #{reverse("abc") == "cba"}")
puts("reverse(\"a\") == \"a\": #{reverse("a") == "a"}")
puts("reverse(\"\") == \"\": #{reverse("") == ""}")

【问题讨论】:

  • "abcdefg".reverse! => "gfedcba"
  • @scottxu 我认为这个问题是为了通过编写一个反转字符串的方法来练习编程。

标签: ruby string reverse


【解决方案1】:
reversed_string = string[i] + reversed_string

例如,如果string"abc"string[0] 确实是"a",但这里它被放在reversed_string开头,而不是结尾。 reversed_string 在每次迭代中累加为:

"a" + ""     #string[0] + ""  => "a"
"b" + "a"    #string[1] + "a" => "ba"
"c" + "ba"   #string[2] + "ba"=> "cba"

【讨论】:

    【解决方案2】:

    假设你不能使用 Ruby Class String 的内置 Reverse 方法,你可以试试下面的

    def reverse_string(string)
      new_string = []
      i = string.length-1
      while i >= 0
        new_string.push(string[i])
        i -= 1
      end
      new_string.join
    end
    

    这将创建一个新的字符串对象,但它会在不使用任何内置方法的情况下反转字符串。

    【讨论】:

    • 您正在使用内置方法。例如pushlength
    【解决方案3】:

    如您所知,有一种方法String#reverse 可以反转字符串。我知道您不要使用该方法,而是编写您自己的方法,其中方法的参数是要反转的字符串。其他人会建议您这样做的方法。

    由于您是 Ruby 新手,我认为向您展示如何为 String 类(例如 String#my_reverse)编写一个与 String#reverse 行为完全相同的新方法可能会很有启发性。那么对于字符串"three blind mice",我们将有:

    "three blind mice".reverse    #=> "ecim dnilb eerht"
    "three blind mice".my_reverse #=> "ecim dnilb eerht"
    

    要为 String 类创建一个不带参数的方法,我们通常这样做:

    class String
      def my_method
        ...
      end
    end
    

    我们通过发送一个作为String 类实例的接收器来调用my_method。例如,如果写:

    "three blind mice".my_method
    

    我们将方法String#my_method 发送给接收者"three blind mice"。在方法的定义中,接收者被称为self。这里self 将是"three blind mice"。同样,正如该字符串的第二个字符(偏移量 1)是 "three blind mice"[1] #=&gt; "h"self[1] #=&gt; "h"。我们可以检查:

    class String
      def my_method
        puts "I is '#{self}'"
        (0...self.size).each { |i| puts self[i] }
      end
    end
    
    "three blind mice".my_method
    

    将打印:

    I is 'three blind mice'
    t
    h
    r
    e
    e
    
    b
    ...
    c
    e
    

    my_reverse的方法几乎一样:

    class String
      def my_reverse
        sz = self.size
        str = ''
        (0...sz).each { |i| str << self[sz-1-i] }
        str
      end
    end
    
    "three blind mice".my_reverse
      #=> "ecim dnilb eerht"
    

    您可以将self 视为一个变量,其值为接收者,但与变量不同的是,您不能将 self 重新分配给不同的对象。例如,我们可以写x = 1; x = 'cat',但不能写self = 'cat'。然而,正如我们已经看到的,我们可以更改 self 对其他对象的引用,例如 self[1] = 'r'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2017-08-31
      • 2011-12-23
      相关资源
      最近更新 更多