Skip to content

Instantly share code, notes, and snippets.

/arraysort.rb Secret

Created September 1, 2015 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/87b3b22d5f994f6cff1d to your computer and use it in GitHub Desktop.
Save anonymous/87b3b22d5f994f6cff1d to your computer and use it in GitHub Desktop.
puts 'Please type in as many words as you want, one word at a time.'
puts 'You HAVE to provide at least 1 word! I am not kidding!'
puts 'Press Enter on an empty line to finish'
puts 'I will repeat the words back to you in alphabetical order'
puts 'I will preserve your capitalization and keep all of your duplicates'
puts 'And for fun, after sorting, I will shuffle up all of your words! Coz I am goofy!'
unsorted_array = []
userInput = gets.chomp
unsorted_array.push userInput
while unsorted_array[0].length < 1
puts 'Oh, come on, you have to give me at least 1 word! I am serious!'
unsorted_array.delete_at(0)
userInput = gets.chomp
unsorted_array.push userInput
end
while userInput.length >= 1
unsorted_array.push userInput
userInput = gets.chomp
end
sorted_array = []
while unsorted_array.length > 1
smallest_element = unsorted_array[0]
x=0
while x < unsorted_array.length - 1
if smallest_element.downcase > unsorted_array[x + 1].downcase
smallest_element = unsorted_array [x + 1]
end
x += 1
end
sorted_array.push(smallest_element)
index_to_delete = unsorted_array.index(smallest_element)
unsorted_array.delete_at(index_to_delete)
end
sorted_array.push(unsorted_array[0])
unsorted_array.delete_at(0)
puts
puts 'this is your freshly sorted array. Enjoy! :)'
puts sorted_array
puts '------------------------------------'
random_array = []
while sorted_array.length > 1
random_index = rand(sorted_array.length - 1)
random_array.push(sorted_array[random_index])
sorted_array.delete_at(random_index)
end
random_array.push(sorted_array[0])
sorted_array.delete_at(0)
puts
puts 'And just for fun, here are all your words in random order! Yay!'
puts random_array
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment