Created
August 27, 2021 23:22
-
-
Save gogvale/cd3a5e143252d7e87f3efb66868cf68f to your computer and use it in GitHub Desktop.
Compression challenge (https://corgicorporation.medium.com/if-software-engineering-is-in-demand-why-is-it-so-hard-to-get-a-software-engineering-job-c043a964e463)
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
def generate_code(number) | |
charset = Array('A'..'Z') + Array('a'..'z') | |
Array.new(number) { charset.sample }.join | |
end | |
org_string = ARGV.first || generate_code(100) | |
solution = '' | |
string = org_string.split('') | |
string.each_with_index do |letter, index| | |
next if index.zero? | |
if letter == string[index - 1][-1] | |
string[index] += string[index - 1] | |
else | |
solution += "#{string[index - 1][-1]}#{string[index - 1].size if string[index - 1].size > 1}" | |
end | |
end | |
puts "Original:\t#{org_string}" | |
print "Compressed:\t" | |
solution += "#{string[-1]}#{string[-1].size if string[-1].size > 1}" | |
puts solution |
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
def generate_code(number) | |
charset = Array('A'..'Z') + Array('a'..'z') | |
Array.new(number) { charset.sample }.join | |
end | |
string = ARGV.first || generate_code(100) | |
solution = {} | |
string.split('').each do |letter| | |
solution[letter] = if solution.keys.include? letter | |
solution[letter] + 1 | |
else | |
1 | |
end | |
end | |
puts "Original:\t#{string}" | |
puts "Compressed:\t#{solution.sort_by { |letter, _| letter }.to_a.flatten.map(&:to_s).join}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment