Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed May 17, 2023
0 parents commit 2101349
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go package

on:
push:
tags:
- '*'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Ville Vesilehto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# go-thespine

The greatest death metal album of all time is "Nespithe" (1993) by Demilich from Kuopio, Finland.

This Go library presents encoding and decoding facilities for the anagrams presented by this album.

## Links

- [Official band website](https://demilich.band/)
- [How Demilich’s Only Album Became a Death Metal Cult Classic (Bandcamp, 2023)](https://daily.bandcamp.com/features/demilich-nespithe-interview)
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/thevilledev/go-thespine

go 1.20
46 changes: 46 additions & 0 deletions thespine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package thespine

import "strings"

const THE_SIZE = 3

func Decode(s string) string {
l := len(s)
g := make([]string, 0)
gc := l / THE_SIZE
if l%THE_SIZE != 0 {
gc++
}
for i := 0; i < gc; i++ {
si := l - (i+1)*THE_SIZE
ei := l - i*THE_SIZE
if si < 0 {
si = 0
}
gs := s[si:ei]
g = append(g, gs)
}
return strings.Join(g, "")
}

func Encode(s string) string {
l := len(s)
g := make([]string, 0)
gc := l / THE_SIZE
if l%THE_SIZE != 0 {
gc++
}
for i := 0; i < gc; i++ {
si := i * THE_SIZE
ei := (i + 1) * THE_SIZE
if ei > l {
ei = l
}
gs := s[si:ei]
g = append(g, gs)
}
for i, j := 0, len(g)-1; i < j; i, j = i+1, j-1 {
g[i], g[j] = g[j], g[i]
}
return strings.Join(g, "")
}
44 changes: 44 additions & 0 deletions thespine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package thespine

import "testing"

func Test_EncodeDecode(t *testing.T) {
tests := []struct {
str string
name string
want string
}{
{
str: "",
name: "empty string",
want: "",
},
{
str: "erecshyrinol",
name: "the song",
want: "nolyricshere",
},
{
str: "nespithe",
name: "the album",
want: "thespine",
},
{
str: "kubernetes",
name: "the tech",
want: "tesrneubek",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Decode(test.str)
if got != test.want {
t.Fatalf("decode got: %s\nwant: %s\n", got, test.want)
}
got = Encode(test.want)
if got != test.str {
t.Fatalf("encode got: %s\nwant: %s\n", got, test.str)
}
})
}
}

0 comments on commit 2101349

Please sign in to comment.