Last active
December 11, 2015 02:59
-
-
Save cnocon/4534239 to your computer and use it in GitHub Desktop.
Sorting exercise (Ch. 10) from "Learning to Program" by Chris Pine (2nd Edition)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'Please enter a list of words, separated by commas, that you would like to sort alphabetically.' | |
unsorted = gets.chomp.downcase.split(',') | |
sorted = [] | |
def sorter unsorted, sorted | |
if unsorted.length == 0 | |
return sorted | |
else | |
smalls = unsorted.pop #Biggie, Biggie, Biggie, can't you see? Sometimes your words just hypnotize me... | |
unsorted.each do |tester| | |
if tester < smalls | |
unsorted.push smalls | |
smalls = tester | |
else | |
unsorted.push tester | |
sorted.push smalls | |
end | |
end | |
end | |
sorter unsorted, sorted | |
end | |
puts ' ' | |
puts 'Here\'s your alphabetically sorted list! It\'s the bees knees.' | |
puts unsorted.sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment