Skip to content

Instantly share code, notes, and snippets.

@vgarro
Created February 21, 2023 02:16
Show Gist options
  • Save vgarro/9850d29e2a6a529210d69cd5abed6044 to your computer and use it in GitHub Desktop.
Save vgarro/9850d29e2a6a529210d69cd5abed6044 to your computer and use it in GitHub Desktop.
# Given a string with key value pairs like this : "key=ABCD, age=68, key=AIAS, age=66",
# figure out which of the pairs has age grwater than 50
# Removes the key=anything first, then commas, then whitespaces leaving age=
# Then split age=[Number] in order to get the list of numbers
# count the ones greater than 50
def count_ages_greather_than(str, age_limit = 50)
ages = str.gsub(/key=\S*/, '').strip.gsub(',', '').strip.gsub(' ', '').split("age=")
# output array = ["68", "66", "68", "50", "68", "66", "68", "66"]
ages_count = 0
ages.each do |age|
ages_count += 1 if age.to_i > 50
end
return ages_count
end
str = "key=ABCD, age=68, key=AIAS, age=66, key=ABCD, age=68, key=AIAS, age=50, key=ABCD, age=68, key=AIAS, age=66, key=ABCD, age=68, key=AIAS, age=66"
puts count_ages_greather_than(str, 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment