Skip to content

Commit

Permalink
Add file based variation and use a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
allentv committed Mar 21, 2019
1 parent c88e0bf commit 9194f14
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 58 deletions.
5 changes: 5 additions & 0 deletions beginners/Exercise01/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"go.autocompleteUnimportedPackages": true,
"go.formatTool": "gofmt",
"go.inferGopath": true
}
70 changes: 12 additions & 58 deletions beginners/Exercise01/src/app/main.go
Original file line number Diff line number Diff line change
@@ -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 == "" {
Expand All @@ -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)
}
54 changes: 54 additions & 0 deletions beginners/Exercise01/src/app/utils/util.go
Original file line number Diff line number Diff line change
@@ -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)
}
42 changes: 42 additions & 0 deletions beginners/Exercise01/src/app/variation/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions beginners/Exercise01/src/app/variation/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is some sample text for reading from a file.

More information .. 1
More information .. 2
More information .. 3

0 comments on commit 9194f14

Please sign in to comment.