Skip to content

Instantly share code, notes, and snippets.

@cnocon
Created September 12, 2013 13:33
Show Gist options
  • Select an option

  • Save cnocon/6537391 to your computer and use it in GitHub Desktop.

Select an option

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
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