Skip to content

Commit 22da4c1

Browse files
committed
added challenge 15
1 parent 9f31a09 commit 22da4c1

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
10-gaussianCurve/kek.zip
2+
.idea/

015-hexToDecimal/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Welcome @Challengers to the 15th #code4bytes challenge!
2+
3+
The first challenge of 2021!
4+
Happy New Year, Merry Christmas, yada yada.
5+
Sorry for the immensely long delay but I've been very with a bunch of stuff. You know how it is.
6+
7+
Today's challenge is about converting hexadecimals to integers.
8+
In the file `tests.json` you find a bunch of test cases that you will need to write a function for.
9+
I think it's pretty self-explanatory. `input` is the one and only parameter of the function (also a string), `result` is the expected result (integer) and `fails` is if whether the function should return an error in case there is an invalid character in the input (represented by a boolean, true for should return an error. In the case of an error the result is 0).
10+
11+
Write a function that converts a string representing a hexadecimal number to a decimal integer.
12+
Run through all the examples, and print each one out (or throw an error if there is a mismatch. Make it extra visibile so I don't have to spend 3 hours checking all the results thanks haha)
13+
14+
No external libraries can be used to do the converting for you (or if your language happens to have a function that does it for you), no points will be rewarded if you do! What is allowed is libraries that convert strings to integers, maths functions, logging, etc. Also try not to inmediately google how to convert hex to decimal, otherwise where is the fun in coming up with your own algorithm. Direct copying of code is also forbidden per the rules (and a ban-able offence might I add.)
15+
16+
All reasonable languages are allowed (no brainfuck, assembly, etc).
17+
Submit your results and source code to me to receive your points!
18+
19+
- 1st place: 300.000 bytes!
20+
- 2nd place: 200.000 bytes!
21+
- 3rd place: 150.000 bytes!
22+
- Runner up: 75.000 bytes!
23+
24+
Good luck, have fun and don't cheat!

015-hexToDecimal/base_solution.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"math"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
type Test struct {
15+
Input string `json:"input"`
16+
Result int `json:"result"`
17+
Fails bool `json:"fails"`
18+
}
19+
20+
var hexMap = map[string]int{
21+
"A": 10,
22+
"B": 11,
23+
"C": 12,
24+
"D": 13,
25+
"E": 14,
26+
"F": 15,
27+
}
28+
29+
func hexToDecimal(hex string) (decimal int, err error) {
30+
split := strings.Split(hex, "")
31+
length := len(split)
32+
33+
for i, char := range split {
34+
var num int
35+
num, strConvErr := strconv.Atoi(char)
36+
if strConvErr != nil {
37+
if num = hexMap[char]; num == 0 {
38+
return 0, errors.New(fmt.Sprintf("hex: %v not valid", hex))
39+
}
40+
}
41+
42+
decimal += num * int(math.Pow(16, float64(length-i+-1)))
43+
}
44+
45+
return decimal, nil
46+
}
47+
48+
func readFile(filename string) []byte {
49+
b, err := ioutil.ReadFile(filename)
50+
if err != nil {
51+
log.Println(err)
52+
}
53+
54+
return b
55+
}
56+
57+
func main() {
58+
59+
var tests []Test
60+
61+
b := readFile("tests.json")
62+
err := json.Unmarshal(b, &tests)
63+
if err != nil {
64+
fmt.Println(err)
65+
}
66+
67+
for _, test := range tests {
68+
r, err := hexToDecimal(test.Input)
69+
70+
if err != nil && test.Fails == true {
71+
fmt.Printf("Testcase: '%v' | Expected: err, got: err\n", test.Input)
72+
} else {
73+
fmt.Printf("Testcase: '%v' | Expected: %v, got: %v\n", test.Input, test.Result, r)
74+
}
75+
76+
}
77+
}

015-hexToDecimal/main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"os"
7+
"testing"
8+
)
9+
10+
func TestHexToDecimal(T *testing.T) {
11+
12+
var tests []Test
13+
14+
f, err := os.Open("tests.json")
15+
if err != nil {
16+
log.Println(err)
17+
}
18+
19+
var b []byte
20+
_, err = f.Read(b)
21+
if err != nil {
22+
log.Println(err)
23+
}
24+
25+
err = json.Unmarshal(b, &tests)
26+
27+
for _, test := range tests {
28+
r, err := hexToDecimal(test.Input)
29+
if err != nil && test.Fails == true {
30+
continue
31+
}
32+
33+
if err != nil {
34+
T.Fatalf(`Testcase: "%v" failed with error: %v`, test.Input, err)
35+
}
36+
37+
if r != test.Result {
38+
T.Fatalf(`Testcase: "%v" failed. Expected: %v, got: %v`, test.Input, test.Result, r)
39+
}
40+
}
41+
}

015-hexToDecimal/tests.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[
2+
{
3+
"input":"0",
4+
"result":0,
5+
"fails":false
6+
},
7+
{
8+
"input":"01",
9+
"result":1,
10+
"fails":false
11+
},
12+
{
13+
"input":"1",
14+
"result":1,
15+
"fails":false
16+
},
17+
{
18+
"input":"9",
19+
"result":9,
20+
"fails":false
21+
},
22+
{
23+
"input":"11",
24+
"result":17,
25+
"fails":false
26+
},
27+
{
28+
"input":"99",
29+
"result":153,
30+
"fails":false
31+
},
32+
{
33+
"input":"FFF",
34+
"result":4095,
35+
"fails":false
36+
},
37+
{
38+
"input":"383AFB",
39+
"result":3685115,
40+
"fails":false
41+
},
42+
{
43+
"input":"AA",
44+
"result":170,
45+
"fails":false
46+
},
47+
{
48+
"input":"12346789ABCDEF",
49+
"result":5124168876805615,
50+
"fails":false
51+
},
52+
{
53+
"input":"FEDCBA987654321",
54+
"result":1147797409030816545,
55+
"fails":false
56+
},
57+
{
58+
"input":"ABCDEFG012345789",
59+
"result":0,
60+
"fails":true
61+
},
62+
{
63+
"input":"&##$})!}@!{}",
64+
"result":0,
65+
"fails":true
66+
},
67+
{
68+
"input":"1 2",
69+
"result":0,
70+
"fails":true
71+
}
72+
]

0 commit comments

Comments
 (0)