Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Created October 3, 2017 21:02
Show Gist options
  • Save barbchoy/83fd8baba94abb08693cfcbc06f8c297 to your computer and use it in GitHub Desktop.
Save barbchoy/83fd8baba94abb08693cfcbc06f8c297 to your computer and use it in GitHub Desktop.
most-common-letter created by barbchoy - https://repl.it/Br7Y/2889
# Write a method that takes in a string. Your method should return the
# most common letter in the array, and a count of how many times it
# appears.
#
# Difficulty: medium.
def most_common_letter(string)
i=0
j=0
frequency=0
highest_freq=0
letter=""
highest_freq_letter=""
while i<string.length
letter=string[i]
while j<string.length
if string[j]==letter
frequency+=1
end
if frequency>highest_freq
highest_freq=frequency
highest_freq_letter=letter
end
j+=1
end
frequency=0
j=0
i+=1
end
return [highest_freq_letter,highest_freq]
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #most_common_letter")
puts("===============================================")
puts(
'most_common_letter("abca") == ["a", 2]: ' +
(most_common_letter('abca') == ['a', 2]).to_s
)
puts(
'most_common_letter("abbab") == ["b", 3]: ' +
(most_common_letter('abbab') == ['b', 3]).to_s
)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment