-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b711e0d
commit 67134c0
Showing
3 changed files
with
29 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
class LetterCounter | ||
def initialize(text) | ||
@text = text | ||
end | ||
|
||
def calculate_most_common() | ||
counter = Hash.new(1) | ||
binding.irb | ||
most_common = nil | ||
most_common_count = 1 | ||
@text.chars.each do |char| | ||
next unless is_letter?(char) | ||
counter[char] = (counter[char] || 1) + 1 | ||
if counter[char] > most_common_count | ||
most_common = char | ||
most_common_count += counter[char] | ||
end | ||
def initialize(text) | ||
@text = text | ||
end | ||
|
||
def calculate_most_common | ||
counter = Hash.new(0) | ||
most_common = nil | ||
most_common_count = 1 | ||
@text.chars.each do |char| | ||
next unless is_letter?(char) | ||
counter[char] = (counter[char] || 1) + 1 | ||
if counter[char] > most_common_count | ||
most_common = char | ||
most_common_count += 1 | ||
end | ||
return [most_common_count, most_common] | ||
end | ||
|
||
private | ||
|
||
def is_letter?(letter) | ||
return letter =~ /[a-z]/i | ||
end | ||
return [most_common_count, most_common] | ||
end | ||
|
||
private | ||
|
||
def is_letter?(letter) | ||
return letter =~ /[a-z]/i | ||
end | ||
|
||
counter = LetterCounter.new("Digital Punk") | ||
p counter.calculate_most_common | ||
|
||
# Intended output: | ||
# [2, "i"] | ||
end | ||
|
||
counter = LetterCounter.new("Digital Punk") | ||
p counter.calculate_most_common | ||
|
||
# Intended output: | ||
# [2, "i"] |