Skip to content

Commit 60a2544

Browse files
committed
added challenge 10
1 parent 5372143 commit 60a2544

File tree

8 files changed

+238
-0
lines changed

8 files changed

+238
-0
lines changed

.gitignore

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

10-gaussianCurve/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:latest
2+
3+
LABEL maintainer="Mathis Van Eetvelde"
4+
5+
WORKDIR /app
6+
7+
COPY . .
8+
9+
RUN go get -d github.com/gorilla/mux
10+
11+
EXPOSE 8080
12+
13+
RUN go build -o main .
14+
15+
CMD ["./main"]

10-gaussianCurve/base_solution.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import requests
2+
import matplotlib.pyplot as plt
3+
from tqdm import tqdm
4+
import numpy as np
5+
import time
6+
7+
url = "http://codingweb.eu-central-1.elasticbeanstalk.com/golang"
8+
a = []
9+
10+
start = time.time()
11+
12+
while len(a) < 100000:
13+
print(len(a))
14+
r = requests.get(url=url)
15+
if("429" in str(r)):
16+
print("sleeping")
17+
time.sleep(.1)
18+
else:
19+
data = r.json()
20+
#print(data)
21+
for d in data:
22+
a.append(float(d))
23+
24+
#time.sleep(1)
25+
26+
print(time.time() - start)
27+
28+
plt.hist(a, 20)
29+
plt.show()
30+
31+
mean = sum(a)/len(a)
32+
33+
t = 0
34+
for num in a:
35+
t += (num - mean)**2
36+
37+
std = np.sqrt((1/len(a))*t)
38+
39+
print(mean, std)

10-gaussianCurve/kek.zip

4.03 MB
Binary file not shown.

10-gaussianCurve/main

7.67 MB
Binary file not shown.

10-gaussianCurve/main.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package main
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"github.com/gorilla/mux"
9+
"math"
10+
"math/rand"
11+
"net/http"
12+
"strconv"
13+
"strings"
14+
"time"
15+
)
16+
17+
var m map[string]GaussianInstance
18+
19+
type GaussianInstance struct {
20+
Id string `json:"id"`
21+
Hash string `json:"hash"`
22+
RandHash int64 `json:"randHash"`
23+
Mu float64 `json:"mu"`
24+
Sigma float64 `json:"sigma"`
25+
R *rand.Rand `json:"r"`
26+
Count int64 `json:"count"`
27+
Time int64 `json:"time"`
28+
}
29+
30+
31+
func newRouter() *mux.Router {
32+
router := mux.NewRouter()
33+
return router
34+
}
35+
36+
func RandomFloatRange(R *rand.Rand, min float64, max float64) float64 {
37+
return float64(min) + R.Float64()*(float64(max)-float64(min))
38+
}
39+
40+
func RandomGaussian(gi GaussianInstance) float64 {
41+
u1 := RandomFloatRange(gi.R, 0.0, 1.0)
42+
u2 := RandomFloatRange(gi.R, 0.0, 1.0)
43+
44+
z1 := (math.Sqrt(-2*math.Log(u1)) * math.Cos(2*math.Pi*u2))
45+
46+
return z1*gi.Sigma + gi.Mu
47+
}
48+
49+
func instantiate(gi GaussianInstance) GaussianInstance {
50+
gi.RandHash = convertHashToInt(gi.Hash)
51+
gi.R = rand.New(rand.NewSource(gi.RandHash))
52+
gi.Mu = RandomFloatRange(gi.R, -100.0, 100.0)
53+
gi.Sigma = RandomFloatRange(gi.R, -50.0, 50.0)
54+
gi.Count = 1
55+
gi.Time = time.Now().Unix()
56+
return gi
57+
}
58+
59+
func FilterString(input string) (filtered string) {
60+
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHGIJKLMNOPQRSTUVWXYZ0123456789"
61+
for _, c := range input {
62+
if strings.ContainsAny(chars, fmt.Sprintf("%c", c)) == true {
63+
filtered += fmt.Sprintf("%c", c)
64+
}
65+
}
66+
return filtered
67+
}
68+
69+
func endpoint(w http.ResponseWriter, r *http.Request) {
70+
vars := mux.Vars(r)
71+
var gi GaussianInstance
72+
gi.Id = FilterString(vars["id"])
73+
if len(gi.Id) >= 4 && len(gi.Id) <= 16 {
74+
b := sha256.Sum256([]byte(gi.Id))
75+
hash := hex.EncodeToString(b[:])
76+
gi.Hash = hash
77+
if (GaussianInstance{} == m[hash]) {
78+
m[hash] = instantiate(gi)
79+
}
80+
gi = m[hash]
81+
82+
fmt.Println(vars["id"])
83+
84+
if time.Now().UnixNano() - gi.Time <= 500000000 && vars["id"] != "golang" {
85+
w.WriteHeader(http.StatusTooManyRequests)
86+
return
87+
}
88+
89+
var re []string
90+
for z := 0; z < 100; z++ {
91+
re = append(re, fmt.Sprintf("%f", RandomGaussian(m[hash])))
92+
}
93+
94+
gi = m[hash]
95+
gi.Count += 1
96+
gi.Time = time.Now().UnixNano()
97+
m[hash] = gi
98+
99+
json.NewEncoder(w).Encode(re)
100+
101+
} else {
102+
w.WriteHeader(http.StatusBadRequest)
103+
return
104+
}
105+
}
106+
107+
func convertHashToInt(hash string) int64 {
108+
var returnInteger string
109+
chars := "abcdefghijklmnopqrstuvwxyz"
110+
for _, c := range hash {
111+
if strings.ContainsAny(chars, fmt.Sprintf("%c", c)) == false {
112+
returnInteger += fmt.Sprintf("%c", c)
113+
}
114+
}
115+
fmt.Println(returnInteger)
116+
i, err := strconv.Atoi(returnInteger[:10])
117+
if err != nil {
118+
fmt.Println(err)
119+
}
120+
return int64(i)
121+
}
122+
123+
func fetch(w http.ResponseWriter, r *http.Request) {
124+
vars := mux.Vars(r)
125+
b := sha256.Sum256([]byte(FilterString(vars["id"])))
126+
hash := hex.EncodeToString(b[:])
127+
var gi GaussianInstance
128+
fmt.Println(m[hash])
129+
gi = m[hash]
130+
131+
data, err := json.Marshal(gi)
132+
fmt.Println(data)
133+
134+
if err != nil {
135+
fmt.Println(err)
136+
} else {
137+
json.NewEncoder(w).Encode(gi)
138+
}
139+
}
140+
141+
func handleRequests() {
142+
router := newRouter().StrictSlash(true)
143+
router.HandleFunc("/{id}", middleware(endpoint)).Methods("GET")
144+
router.HandleFunc("/fetchmethisshit/{id}", middleware(fetch)).Methods("GET")
145+
err := http.ListenAndServe(":8080", router)
146+
if err != nil {
147+
fmt.Println(err)
148+
} else {
149+
fmt.Println("Serving: :8080")
150+
}
151+
}
152+
153+
func middleware(f http.HandlerFunc) http.HandlerFunc {
154+
return func(w http.ResponseWriter, r *http.Request) {
155+
fmt.Println(r.Host, r.URL.Path, r.Method)
156+
f(w, r)
157+
}
158+
}
159+
160+
func main() {
161+
m = make(map[string]GaussianInstance)
162+
handleRequests()
163+
}

10-gaussianCurve/normal.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
5+
mu = -4
6+
sigma = 1
7+
8+
def gaussian():
9+
u1 = np.random.rand(1)
10+
u2 = np.random.rand(1)
11+
12+
z1 = (np.sqrt(-2*np.log(u1))*np.cos(2*np.pi*u2))
13+
return z1 * sigma + mu
14+
15+
z = []
16+
for i in range(100):
17+
z.append(gaussian()[0])
18+
print(z)
19+
plt.hist(z, 50)
20+
plt.show()

9-mountains/.README.md.swp

-12 KB
Binary file not shown.

0 commit comments

Comments
 (0)