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

Solution to Exercise01 #1

Open
wants to merge 2 commits 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
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
}
35 changes: 35 additions & 0 deletions beginners/Exercise01/src/app/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
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)
}
43 changes: 43 additions & 0 deletions beginners/Exercise01/src/app/variation/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
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