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

Оптимизация CalcRWords #14

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
42 changes: 22 additions & 20 deletions calc/calc.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
package calc

import (
"strings"
"unicode"
)

type CharacterType int

const (
CHAR_OTHER = 0
CHAR_LETTER = 1
CharNotLetter CharacterType = 0
CharLetter CharacterType = 1
)

func CalcRWords(txt, letter string) int {
num_r_words := 0
current_char_type := CHAR_OTHER
word := ""
func CalcRWords(txt string, letter rune) int {
lower := letter
upper := letter
currentCharType := CharNotLetter
if unicode.IsLower(letter) {
upper = unicode.ToUpper(letter)
} else {
lower = unicode.ToLower(letter)
}

foundRWords := 0
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
if currentCharType != CharLetter {
currentCharType = CharLetter
if l == lower || l == upper {
foundRWords += 1
}
}
current_char_type = CHAR_OTHER
} else {
currentCharType = CharNotLetter
}
}

return num_r_words
return foundRWords
}

12 changes: 6 additions & 6 deletions calc/calc_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package calc

import (
"testing"
"gotest.tools/assert"
)
"testing"
)

func TestCalcRWords(t *testing.T) {
assert.Equal(t, CalcRWords("ааа ррр ббб", "р"), 1)
assert.Equal(t, CalcRWords("ааа ррр ббб\nробот\nработа твоя", "р"), 3)
assert.Equal(t, CalcRWords("ааа ррр ббб", 'р'), 1)
assert.Equal(t, CalcRWords("ааа ррр ббб\nробот\nработа твоя", 'р'), 3)
assert.Equal(t, CalcRWords(`
Главное, любой бы засмущался, находясь
на вечеринке в штанах на пять размеров больше. А
этому – все по-барабану. Взял еще пива, попросил
бумажку с ручкой и стал что-то быстро писать на
листочке.
`, "р"), 2)
}
`, 'р'), 2)
}
4 changes: 2 additions & 2 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)
fmt.Printf("The text %s contains %d words starting from letter '%s'\n", FILENAME, num, string(LETTER))
}