开发者

Sorting array with multiple variables

开发者 https://www.devze.com 2023-04-12 05:17 出处:网络
I am trying to create a program where by the user can enter multiple names. those names are then displayed under each other in alphabetical order, and print(display) every second name backwards. i hav

I am trying to create a program where by the user can enter multiple names. those names are then displayed under each other in alphabetical order, and print(display) every second name backwards. i have gone through several tutorials this is my second day using ruby.. here is what i have so far.

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

to grab names...

names.sor开发者_JAVA技巧t do |a,b| a.upcase <=> b.upcase end
display = "#{names}"
for ss in 0...display.length
       print ss, ": ", display[ss], "\n"
end

to arrange them alphabetically and under each other. i am really struggling to mesh it all together i think i have got at least half a dozen errors in here...if i am on the wrong path could someone guide me to some info so i can start again??

EDIT

i also had this idea of using a class. but i would have to program the names in i wanted the user to be able to add info via the consol. class A

def initialize(name) @name = name end def to_s @name.reverse end end

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

>> puts a


Problems in your code:

  • name_list defined as an empty hash at the top but not used.
  • split(" ") -> split
  • sort { |a, b| a.method <=> b.method } -> sort_by { |x| x.method } -> sort_by(&:method)
  • sort is not an in-place operation, assign the result (or directly use it).
  • display = "#{names}" -> display = names
  • for ss in 0...display.length -> enumerable.each_with_index { |item, index| ... }
  • don't write do/end in one-liners, use { ... }

I'd write:

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


A few pointers then:

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.

To display your names:

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


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

You can also use a ternary operator, I used the full if else block for readability in this case.

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

EDIT

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号