Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions calc/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,17 @@ import (
"unicode"
)

const (
CHAR_OTHER = 0
CHAR_LETTER = 1
)
func WordsBeginsByLetter(txt string, letter rune) int {
cnt := 0
letter = unicode.ToLower(letter)
lastControl := true

func CalcRWords(txt, letter string) int {
num_r_words := 0
current_char_type := CHAR_OTHER
word := ""
for _, l := range txt {
if unicode.IsLetter(l) {
if current_char_type == CHAR_LETTER {
word += string(l)
} else {
current_char_type = CHAR_LETTER
word = string(l)
}
} else {
if current_char_type == CHAR_LETTER {
if strings.HasPrefix(strings.ToLower(word), strings.ToLower(letter)) {
num_r_words += 1
}
}
current_char_type = CHAR_OTHER
for _, r := range strings.ToLower(txt) {
if lastControl && unicode.ToLower(r) == letter {
cnt++
}
lastControl = !unicode.IsLetter(r)
}

return num_r_words
return cnt
}

12 changes: 7 additions & 5 deletions calc/calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
)

func TestCalcRWords(t *testing.T) {
assert.Equal(t, CalcRWords("ааа ррр ббб", "р"), 1)
assert.Equal(t, CalcRWords("ааа ррр ббб\nробот\nработа твоя", "р"), 3)
assert.Equal(t, CalcRWords(`
assert.Equal(t, WordsBeginsByLetter("ааа ррр ббб", 'р'), 1)
assert.Equal(t, WordsBeginsByLetter("ааа ррр ббб\nробот\nработа твоя", 'р'), 3)
assert.Equal(t, WordsBeginsByLetter(`
Главное, любой бы засмущался, находясь
на вечеринке в штанах на пять размеров больше. А
этому – все по-барабану. Взял еще пива, попросил
бумажку с ручкой и стал что-то быстро писать на
листочке.
`, "р"), 2)
}
`, 'р'), 2)
assert.Equal(t, WordsBeginsByLetter("нужная буква в скобках: (р)", 'р'), 1)
assert.Equal(t, WordsBeginsByLetter("буква заглавная Р", 'р'), 1)
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ This script calculates the number of words in the text
*/

const FILENAME = "Exler.txt"
const LETTER = "р"
const LETTER = 'р'

func main() {
txt, err := ioutil.ReadFile(FILENAME)
if err != nil {
log.Fatalf("Failed to open text file: %s", err.Error())
}
num := calc.CalcRWords(string(txt), LETTER)
fmt.Printf("The text %s contains %d words starting from letter '%s'\n", FILENAME, num, LETTER)
num := calc.WordsBeginsByLetter(string(txt), LETTER)
fmt.Printf("The text %s contains %d words starting from letter %q\n", FILENAME, num, LETTER)
}