Skip to content

Instantly share code, notes, and snippets.

@alvinkatojr
Created June 7, 2017 05:17
Show Gist options
  • Select an option

  • Save alvinkatojr/b7f711526bd265c48163ed6071b825d2 to your computer and use it in GitHub Desktop.

Select an option

Save alvinkatojr/b7f711526bd265c48163ed6071b825d2 to your computer and use it in GitHub Desktop.
scroll = File.read('data.txt').chomp
input_string = "abcdefghijklmnopqrstuvwxyz_"
# Sort the input string by the number of characters that appear in the scroll text
#
# I - string
# P
# O - string
# Kindergaten steps
# 1. Take the first character in the input string
# 2. Search for how many times it appears in the scroll
# 3. Create a hash to store the results of the iteration
# 4. Store the count and the character in a hash
# 5. Repeat the process for all
# 6. Loop over the hash, get the max key
# 7. Use it to retrieve the value/character it represents
# 8. Store that character in an empty string
# 9. Delete that max key from the hash
# 10. Repeat the process
results_hash = {}
input_string.each_char do |character|
occurrence = scroll.count(character)
results_hash[occurrence] = character
end
puts results_hash
# {93=>"a", 100=>"b", 96=>"c", 86=>"d", 85=>"e", 89=>"f", 87=>"g", 82=>"h",
# 99=>"i", 81=>"j", 80=>"k", 94=>"l", 88=>"m", 98=>"n", 97=>"o", 84=>"p",
# 79=>"q", 92=>"r", 91=>"s", 83=>"t", 95=>"u", 78=>"v", 77=>"w", 76=>"x",
# 75=>"y", 45=>"z", 90=>"_"}
sorted_string = ''
loop do
break if results_hash.empty?
highest_count = results_hash.keys.max
current_character = results_hash[highest_count]
sorted_string += current_character
results_hash.delete(highest_count)
end
puts sorted_string
# binoculars_fmgdepthjkqvwxyz
puts sorted_string.gsub!(/\_[a-z]+/, '')
# binoculars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment