【问题标题】:Sorting array with multiple variables使用多个变量对数组进行排序
【发布时间】:2011-12-05 21:45:57
【问题描述】:

我正在尝试创建一个程序,用户可以在其中输入多个名称。然后这些名称按字母顺序显示在彼此下方,并每隔一个名称向后打印(显示)。我已经阅读了几个教程,这是我使用 ruby​​ 的第二天。这是我目前所掌握的。

name_list = {}
puts 'please enter names seperated by a space:'
name_list = gets.chomp
names = name_list.split(" ")

获取名字...

names.sort do |a,b| a.upcase <=> b.upcase end
display = "#{names}"
for ss in 0...display.length
       print ss, ": ", display[ss], "\n"
end

按字母顺序排列它们。 我真的很难将它们融合在一起我想我在这里至少有六个错误......如果我走错了路,有人可以指导我获取一些信息,以便我可以重新开始吗??

编辑

我也有使用类的想法。 但我必须对名称进行编程,我希望用户能够通过控制台添加信息。 A类

def 初始化(名称) @name = 名称 结尾 def to_s @name.reverse 结尾 结束

>> a = [A.new("greg"),A.new("pete"),A.new("paul")]

>> puts a

【问题讨论】:

  • 你想用最后一个循环实现什么?预期的输出是什么?

标签: ruby arrays sorting


【解决方案1】:

代码中的问题:

  • name_list 定义为顶部的空哈希但未使用。
  • 拆分(“”)->拆分
  • 排序{ |a, b| a.method b.method } -> sort_by { |x| x.method } -> sort_by(&:method)
  • 排序不是就地操作,分配结果(或直接使用)。
  • display = "#{names}" -> display = 名字
  • 对于 ss in 0...display.length -> enumerable.each_with_index { |item, index| ... }
  • 不要在一行中写 do/end,使用 { ... }

我会写:

puts 'Please enter names separated by spaces'
gets.split.sort_by(&:upcase).each_with_index do |name, index|
  puts "%s: %s" % [index, (index % 2).zero? ? name : name.reverse]
end

【讨论】:

  • 谢谢,我以为我遇到了一些问题!!是的...我昨天才开始使用红宝石。语法如此简单令人困惑。我习惯了很多统一的程序......你摇我的袜子......
【解决方案2】:

那么几点建议:

names.sort do |a,b| a.upcase <=> b.upcase end # Will not modify the "names" array, but will return a sorted array.
names.sort! do |a,b| a.upcase <=> b.upcase end # Will modify the "names" array.

显示你的名字:

names.each_with_index do |name, index|
    if index % 2 == 0
        puts name
    else
        puts name.reverse
    end
end

【讨论】:

  • 所以如果我想在彼此之间打印它们,我会在末尾添加 /n 还是打印名称??
【解决方案3】:
puts 'please enter names seperated by a space`enter code here` :'
names = gets.chomp.split(" ")

names.sort! {|a,b| a.upcase <=> b.upcase }  # For a single line use {..} instead of do..end

names.each_with_index do |n,i|
  if i % 2 == 0
    p n
  else
    p n.reverse
  end
end

您也可以使用三元运算符,在这种情况下,我使用了完整的 if else 块以提高可读性。

names.each_with_index do |n,i|
  p (i % 2 == 0) ? n : n.reverse
end

编辑

command = ""
names = []
while command != "exit"
  puts 'please enter names seperated by a space`enter code here` :'

  command = gets.chomp!

  if command == "display"
    names.sort! {|a,b| a.upcase <=> b.upcase }  # For a single line use {..} instead of do..end        
    names.each_with_index do |n,i|
      if i % 2 == 0
        p n
      else
        p n.reverse
      end
    end
  else
    names << command
  end
end

【讨论】:

  • 我通常会避免使用三元运算符,因为它会损害代码的可读性。然而,在这种情况下,使用它是一个不错的情况。此外,OP 以不区分大小写的方式排序,您也应该这样做。
  • 谢谢伙计.. 是否可以输入一个触发器,说他可以输入任意数量的名称,但是当他键入“显示”时,它会显示名称.. 由你摇我袜子的方式。我已经使用了我的 google-fu 并且无法到达任何地方:)
  • @Romain,很好发现。我已经改变了。
  • @Sliknox 我已经更新了我的代码,以便在我开始工作之前快速完成这项工作。不过它应该可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2012-11-02
  • 2014-04-01
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多