Skip to content

Commit

Permalink
Add the first prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
dnozdrin committed May 31, 2020
1 parent ddd9987 commit 4ac722a
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2.1
orbs:
codecov: codecov/[email protected]
jobs:
build:
docker:
- image: circleci/golang:1.14.3

working_directory: /go/src/github.com/dnozdrin/vowels-coder
steps:
- checkout
- run:
name: "Setup Environment Variables"
command: |
echo "export CODECOV_TOKEN=55930313-e085-47c4-b996-54e2210012c1" >> $BASH_ENV
- run:
name: "Create a temp directory for artifacts"
command: |
mkdir -p /tmp/artifacts
- run: go get -v -t -d ./...
- run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
- run: go build
- store_artifacts:
path: /tmp/artifacts

- codecov/upload:
file: coverage.txt
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.idea/
/vowels-coder
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# vowel-coder
<p align="center">
<img alt="vowels-coder logo" src="assets/logo.svg" height="150"/>
<h3 align="center">vowels-coder</h3>
<p align="center">Vowels encoder / decoder</p>
</p>

---

`vowels-coder` encodes and decodes a given string in English by replacing vowels according to the next map:

| Letter | Number |
| --- | --- |
| a | 1 |
| e | 2 |
| i | 3 |
| o | 4 |
| u | 5 |

## Badges
[![dnozdrin](https://circleci.com/gh/dnozdrin/vowels-coder.svg?style=shield)](https://circleci.com/gh/dnozdrin/vowels-coder)
[![codecov](https://codecov.io/gh/dnozdrin/vowels-coder/branch/master/graph/badge.svg)](https://codecov.io/gh/dnozdrin/vowels-coder)
[![dnozdrin](https://goreportcard.com/badge/github.com/dnozdrin/vowels-coder)](https://goreportcard.com/report/github.com/dnozdrin/vowels-coder)
[![License](https://img.shields.io/github/license/dnozdrin/vowels-coder)](/LICENSE)
[![Release](https://img.shields.io/github/release/dnozdrin/vowels-coder.svg)](https://github.com/dnozdrin/vowels-coder/releases/latest)

## Installation
- Clone the repository
- In the main directory run `go run build` to build the project

## Usage
- Run `go run build` to build the project.
- To encode a string, run `./vowels-coder`, enter the text that must be encoded.
- To decode a string, run `./vowels-coder -d`, enter the text that must be decoded.
1 change: 1 addition & 0 deletions assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dnozdrin/vowels-coder

go 1.14
55 changes: 55 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
)

var encoderMap = []string{
"a", "1",
"e", "2",
"i", "3",
"o", "4",
"u", "5",
}

var decoderMap = []string{
"1", "a",
"2", "e",
"3", "i",
"4", "o",
"5", "u",
}

func main() {
var result string

decode := flag.Bool("d", false, "Decode.")
flag.Parse()

fmt.Println("Please enter the text to process:")
s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanLines)
if s.Scan() {
if *decode {
result = processText(decoderMap, s.Text())
} else {
result = processText(encoderMap, s.Text())
}

fmt.Println(result)
}
if err := s.Err(); err != nil {
log.Printf("reading input: %s", err)
}
}

func processText(mapping []string, text string) string {
r := strings.NewReplacer(mapping...)
lowerCase := strings.ToLower(text)
return r.Replace(lowerCase)
}
40 changes: 40 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import "testing"

var encodeTests = []struct {
in, out string
}{
{"aeiou", "12345"},
{"hello", "h2ll4"},
{"hi there", "h3 th2r2"},
{"Hello", "h2ll4"},
{"HI THERE", "h3 th2r2"},
}

func TestEncode(t *testing.T) {
testTextProcessing(t, encodeTests, encoderMap)
}

func testTextProcessing(t *testing.T, samples []struct{ in, out string }, mapping []string) {
for _, tt := range samples {
t.Run(tt.in, func(t *testing.T) {
s := processText(mapping, tt.in)
if s != tt.out {
t.Errorf("got %q, want %q", s, tt.out)
}
})
}
}

var decodeTests = []struct {
in, out string
}{
{"12345", "aeiou"},
{"h2ll4", "hello"},
{"h3 th2r2", "hi there"},
}

func TestDecode(t *testing.T) {
testTextProcessing(t, decodeTests, decoderMap)
}

0 comments on commit 4ac722a

Please sign in to comment.