From 9194f149a59050448dcb8965094550cd243b2a30 Mon Sep 17 00:00:00 2001 From: Allen Thomas Varghese Date: Thu, 21 Mar 2019 21:34:47 +0000 Subject: [PATCH] Add file based variation and use a separate package --- beginners/Exercise01/.vscode/settings.json | 5 ++ beginners/Exercise01/src/app/main.go | 70 ++++--------------- beginners/Exercise01/src/app/utils/util.go | 54 ++++++++++++++ .../Exercise01/src/app/variation/main.go | 42 +++++++++++ .../Exercise01/src/app/variation/sample.txt | 5 ++ 5 files changed, 118 insertions(+), 58 deletions(-) create mode 100644 beginners/Exercise01/.vscode/settings.json create mode 100644 beginners/Exercise01/src/app/utils/util.go create mode 100644 beginners/Exercise01/src/app/variation/main.go create mode 100644 beginners/Exercise01/src/app/variation/sample.txt diff --git a/beginners/Exercise01/.vscode/settings.json b/beginners/Exercise01/.vscode/settings.json new file mode 100644 index 0000000..71647e6 --- /dev/null +++ b/beginners/Exercise01/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "go.autocompleteUnimportedPackages": true, + "go.formatTool": "gofmt", + "go.inferGopath": true +} \ No newline at end of file diff --git a/beginners/Exercise01/src/app/main.go b/beginners/Exercise01/src/app/main.go index 82b428b..ac25afe 100644 --- a/beginners/Exercise01/src/app/main.go +++ b/beginners/Exercise01/src/app/main.go @@ -1,27 +1,15 @@ +// Reading data from the command line and then print out the summary + package main import ( + "app/utils" "flag" - "fmt" "log" "os" "strings" ) -func main() { - inputStr := flag.String("text", "", "Text block to parse") - flag.Parse() - - cleanedStr := cleanupInput(*inputStr) - - vowels := findVowels(cleanedStr) - digits := findDigits(cleanedStr) - symbols := findSymbols(cleanedStr) - words := findWords(cleanedStr) - - printReport(vowels, digits, symbols, words) -} - // cleanupInput runs all the validation and bleaching to be applied on the input string func cleanupInput(inputStr string) string { if inputStr == "" { @@ -31,50 +19,16 @@ func cleanupInput(inputStr string) string { return strings.ToLower(inputStr) } -// findVowels will check if the character is one of a, e, i, o, u -func findVowels(s string) int { - result := 0 - for _, v := range s { - if strings.ContainsAny("aeiou", string(v)) { - result++ - } - } - return result -} - -// findDigits will check only for digits from 0 to 9 -func findDigits(s string) int { - result := 0 - for _, v := range s { - if strings.ContainsAny("1234567890", string(v)) { - result++ - } - } - return result -} +func main() { + inputStr := flag.String("text", "", "Text block to parse") + flag.Parse() -// findSymbols will count common symbols appearing on the number keys -func findSymbols(s string) int { - result := 0 - for _, v := range s { - if strings.ContainsAny("!@€£#$%^&*()", string(v)) { - result++ - } - } - return result -} + cleanedStr := cleanupInput(*inputStr) -// findWords will count the number of words without surrounding spaces -func findWords(s string) int { - return len(strings.Fields(s)) -} + vowels := utils.FindVowels(cleanedStr) + digits := utils.FindDigits(cleanedStr) + symbols := utils.FindSymbols(cleanedStr) + words := utils.FindWords(cleanedStr) -// printReport will display a summary of results -func printReport(vowels, digits, symbols, words int) { - fmt.Println("Analysis Summary") - fmt.Println("-------- -------") - fmt.Println("vowels :", vowels) - fmt.Println("digits :", digits) - fmt.Println("symbols :", symbols) - fmt.Println("words :", words) + utils.PrintReport(vowels, digits, symbols, words) } diff --git a/beginners/Exercise01/src/app/utils/util.go b/beginners/Exercise01/src/app/utils/util.go new file mode 100644 index 0000000..4bdba23 --- /dev/null +++ b/beginners/Exercise01/src/app/utils/util.go @@ -0,0 +1,54 @@ +package utils + +import ( + "fmt" + "strings" +) + +// FindVowels will check if the character is one of a, e, i, o, u +func FindVowels(s string) int { + result := 0 + for _, v := range s { + if strings.ContainsAny("aeiou", string(v)) { + result++ + } + } + return result +} + +// FindDigits will check only for digits from 0 to 9 +func FindDigits(s string) int { + result := 0 + for _, v := range s { + if strings.ContainsAny("1234567890", string(v)) { + result++ + } + } + return result +} + +// FindSymbols will count common symbols appearing on the number keys +func FindSymbols(s string) int { + result := 0 + for _, v := range s { + if strings.ContainsAny("!@€£#$%^&*()", string(v)) { + result++ + } + } + return result +} + +// FindWords will count the number of words without surrounding spaces +func FindWords(s string) int { + return len(strings.Fields(s)) +} + +// PrintReport will display a summary of results +func PrintReport(vowels, digits, symbols, words int) { + fmt.Println("Analysis Summary") + fmt.Println("-------- -------") + fmt.Println("vowels :", vowels) + fmt.Println("digits :", digits) + fmt.Println("symbols :", symbols) + fmt.Println("words :", words) +} diff --git a/beginners/Exercise01/src/app/variation/main.go b/beginners/Exercise01/src/app/variation/main.go new file mode 100644 index 0000000..503414d --- /dev/null +++ b/beginners/Exercise01/src/app/variation/main.go @@ -0,0 +1,42 @@ +// Reading data from a file and then print out the summary +// Path to "sample.txt" => src/app/variation/sample.txt relative to project root + +package main + +import ( + "app/utils" + "flag" + "io/ioutil" + "log" + "os" +) + +// cleanupInput runs all the validation and bleaching to be applied on the input string +func cleanupInput(inputFileName string) string { + if inputFileName == "" { + log.Fatal("Input file name not provided!") + os.Exit(1) + } + + data, err := ioutil.ReadFile(inputFileName) + if err != nil { + log.Fatal("Error => ", err.Error()) + os.Exit(1) + } + + return string(data) +} + +func main() { + inputFileName := flag.String("file", "", "File to parse") + flag.Parse() + + cleanedStr := cleanupInput(*inputFileName) + + vowels := utils.FindVowels(cleanedStr) + digits := utils.FindDigits(cleanedStr) + symbols := utils.FindSymbols(cleanedStr) + words := utils.FindWords(cleanedStr) + + utils.PrintReport(vowels, digits, symbols, words) +} diff --git a/beginners/Exercise01/src/app/variation/sample.txt b/beginners/Exercise01/src/app/variation/sample.txt new file mode 100644 index 0000000..33f1f58 --- /dev/null +++ b/beginners/Exercise01/src/app/variation/sample.txt @@ -0,0 +1,5 @@ +This is some sample text for reading from a file. + +More information .. 1 +More information .. 2 +More information .. 3