Created
September 12, 2013 13:33
-
-
Save cnocon/6537391 to your computer and use it in GitHub Desktop.
My shuffle exercise for Ch. 10 in "Learning to Program" by Chris Pine
This file contains hidden or 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
| start_array = [] | |
| puts "Enter as many words as you like, pressing Enter after each word has been input. type 'stop' to finish entering words." | |
| while true | |
| x = gets.chomp.downcase #had to put this within the while loop for code to work, not outside while loop | |
| if x != "stop" | |
| start_array.push x | |
| else | |
| def do_shuffle start_array | |
| shuffle start_array, [] | |
| end | |
| def shuffle unshuffled, shuffled | |
| if unshuffled.length <= 0 | |
| puts shuffled | |
| else | |
| random = unshuffled.sample | |
| shuffled << random | |
| unshuffled.delete(random) | |
| shuffle unshuffled, shuffled | |
| end | |
| end | |
| do_shuffle(start_array) | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment