From c11c23dd9893b6dd4b7e78835d26791a1d394d30 Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Thu, 8 Aug 2024 14:53:11 +0300 Subject: [PATCH] fix encoding to zero --- CHANGELOG.md | 5 ++++ README.md | 2 +- abcex.go | 65 ++++++++++++++++++++++++++++----------------------- abcex_test.go | 22 ++++++++++++----- 4 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b323f73 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# v1.0.1 + +2024-08-08 + +* fix encoding to zero diff --git a/README.md b/README.md index c3d496c..e37d017 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ [![Build Status](https://drone.arns.lt/api/badges/zordsdavini/abcex/status.svg)](https://drone.arns.lt/zordsdavini/abcex) -Computing system based on the number 36 \ No newline at end of file +Computing system based on the number 36 diff --git a/abcex.go b/abcex.go index c56804f..8cb012d 100644 --- a/abcex.go +++ b/abcex.go @@ -1,53 +1,60 @@ package abcex -import "fmt" -import "math" +import ( + "fmt" + "math" +) var decToAbcex = map[int]string{ 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 16: "g", 17: "h", 18: "i", 19: "j", 20: "k", 21: "l", 22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s", - 29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z" } + 29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z", +} var abcexToDec = flipMap() func reverse(s string) string { - runes := []rune(s) - for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { - runes[i], runes[j] = runes[j], runes[i] - } - return string(runes) + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) } func flipMap() map[string]int { - flipped := make(map[string]int) - for i, val := range decToAbcex { - flipped[val] = i - } - - return flipped -} - -func Encode(number int64) string{ - result := "" - - for number > 0 { - result = fmt.Sprintf("%s%s", decToAbcex[int(number % 36)], result) - number /= 36 + flipped := make(map[string]int) + for i, val := range decToAbcex { + flipped[val] = i } - return result + return flipped +} + +func Encode(number int64) string { + if number == 0 { + return "0" + } + + result := "" + + for number > 0 { + result = fmt.Sprintf("%s%s", decToAbcex[int(number%36)], result) + number /= 36 + } + + return result } func Decode(str string) int64 { - var result int64 + var result int64 result = 0 - str = reverse(str) + str = reverse(str) - for i, c := range str { - result = int64(math.Pow(36, float64(i))) * int64(abcexToDec[string(c)]) + result - } + for i, c := range str { + result = int64(math.Pow(36, float64(i)))*int64(abcexToDec[string(c)]) + result + } - return result + return result } diff --git a/abcex_test.go b/abcex_test.go index c43e868..9c7f646 100644 --- a/abcex_test.go +++ b/abcex_test.go @@ -1,14 +1,24 @@ package abcex -import "fmt" -import "testing" +import ( + "fmt" + "testing" +) func TestEncode(t *testing.T) { - fmt.Println(Encode(12345)) - // Output: 9ix + fmt.Println(Encode(12345)) + // Output: 9ix } func TestDecode(t *testing.T) { - fmt.Println(Decode("9ix")) - // Output: 12345 + fmt.Println(Decode("9ix")) + // Output: 12345 +} + +func TestZero(t *testing.T) { + fmt.Println(Encode(0)) + // Output: 0 + + fmt.Println(Decode("0")) + // Output: 0 }