Skip to content

Instantly share code, notes, and snippets.

@Joeventures
Last active September 22, 2015 14:05
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 Joeventures/52e74181ea4116a0cd95 to your computer and use it in GitHub Desktop.
Save Joeventures/52e74181ea4116a0cd95 to your computer and use it in GitHub Desktop.
# 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