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 new file mode 100644 index 0000000..9bac552 --- /dev/null +++ b/beginners/Exercise01/src/app/main.go @@ -0,0 +1,35 @@ +// Reading data from the command line and then print out the summary + +package main + +import ( + "app/utils" + "flag" + "log" + "os" + "strings" +) + +// cleanupInput runs all the validation and bleaching to be applied on the input string +func cleanupInput(inputStr string) string { + if inputStr == "" { + log.Fatal("Input string not provided!") + flag.PrintDefaults() + os.Exit(1) + } + return strings.ToLower(inputStr) +} + +func main() { + inputStr := flag.String("text", "", "Text block to parse") + flag.Parse() + + cleanedStr := cleanupInput(*inputStr) + + 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/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..ff54223 --- /dev/null +++ b/beginners/Exercise01/src/app/variation/main.go @@ -0,0 +1,43 @@ +// 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!") + flag.PrintDefaults() + 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