Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 71260cd

Browse files
author
Holger Lösken
committed
Add task1 in go
1 parent 919837a commit 71260cd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/go/t1/s1.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func Maskify(cc string) string {
11+
var masked = make([]string, len(cc))
12+
var splitted = strings.Split(cc, "")
13+
14+
for idx, item := range splitted {
15+
// Exclude first and last four items
16+
if idx == 0 || idx >= len(splitted) - 4 {
17+
masked = append(masked, item)
18+
continue
19+
}
20+
21+
// Check if is numeric
22+
_, err := strconv.Atoi(item)
23+
24+
if err != nil {
25+
masked = append(masked, item)
26+
continue
27+
}
28+
29+
masked = append(masked, "#")
30+
}
31+
32+
return strings.Join(masked, "")
33+
}
34+
35+
func main() {
36+
cc := ""
37+
38+
if len(os.Args) > 1 {
39+
cc = os.Args[1]
40+
} else {
41+
os.Exit(0)
42+
}
43+
44+
// Do no mask if cc less than 6 or empty string
45+
if len(cc) < 6 {
46+
fmt.Println(cc)
47+
os.Exit(0)
48+
}
49+
50+
fmt.Printf(Maskify(cc))
51+
}

src/go/t1/s1_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestMaskify(t *testing.T) {
9+
type test struct {
10+
input string
11+
want string
12+
}
13+
14+
tests := []test{
15+
{input: "12345", want: "12345"},
16+
{input: "1234-4567-8901", want: "1###-####-8901"},
17+
{input: "123456789", want: "1####6789"},
18+
}
19+
20+
for _, tc := range tests {
21+
got := Maskify(tc.input)
22+
if !reflect.DeepEqual(tc.want, got) {
23+
t.Fatalf("expected: %v, got: %v", tc.want, got)
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)