Created
March 28, 2021 07:37
-
-
Save nandhasuhendra/319c47c2eb61ff669e6ed8b321e0f598 to your computer and use it in GitHub Desktop.
UpKoding challenge https://www.upkoding.com/proyek/menghitung-total-huruf-dari-kalimat-18/nandhasuhendra/
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 counter(string) | |
{}.tap do |result| | |
string.each_char { |chr| result[chr] ? result[chr] += 1 : result[chr] = 1 } | |
end | |
end | |
def format_output(hash) | |
hash.each do |key, total| | |
puts "Huruf #{key} : #{total}" | |
end | |
end | |
format_output counter('UUPpKooding') | |
# RESULT: | |
# Huruf U : 2 | |
# Huruf P : 1 | |
# Huruf p : 1 | |
# Huruf K : 1 | |
# Huruf o : 2 | |
# Huruf d : 1 | |
# Huruf i : 1 | |
# Huruf n : 1 | |
# Huruf g : 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment