Last active
September 22, 2015 14:05
-
-
Save Joeventures/52e74181ea4116a0cd95 to your computer and use it in GitHub Desktop.
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
# This does more than you were asking for. | |
# Two fundtions: | |
# => get_array has the user input each array item | |
# => destutter has two conditions: the array and the mode | |
# If the mode is seq it will return the array with sequential | |
# duplicates removed. If the mode is unique it will return | |
# the array with all duplicates removed | |
def get_array | |
r = [] | |
food=nil | |
until food == "" | |
puts "Feed me, Seymour! (blank to stop feeding): " | |
food = gets.chomp | |
r.push(food) unless food == "" | |
end | |
r | |
end | |
def destutter (ary, mode) | |
r = [] | |
# user_input = nil // From a previous revision | |
if mode == "seq" | |
previous = "" | |
ary.each do |x| | |
r.push(x) unless x == previous | |
previous = x | |
end | |
r | |
elsif mode == "unique" | |
ary.uniq! | |
ary | |
else | |
puts "ERROR: Expecting mode 'seq' or 'unique'. YOU LOSE." | |
end | |
end | |
the_array = get_array | |
puts "Type 'seq' to eliminate sequential duplicates." | |
puts "Type 'unique' to eliminate all duplicates." | |
input = gets.chomp | |
destuttered = destutter(the_array, input) | |
puts destuttered |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment