commit dcac1c8b1bbd296fc363857aff22a6d369901990 Author: bel Date: Sun Oct 15 10:32:09 2023 -0600 ok diff --git a/0_setup/db.db b/0_setup/db.db new file mode 100644 index 0000000..4cfce5a Binary files /dev/null and b/0_setup/db.db differ diff --git a/0_setup/go.mod b/0_setup/go.mod new file mode 100644 index 0000000..51e3620 --- /dev/null +++ b/0_setup/go.mod @@ -0,0 +1,5 @@ +module 0_setup + +go 1.18 + +require github.com/mattn/go-sqlite3 v1.14.15 diff --git a/0_setup/go.sum b/0_setup/go.sum new file mode 100644 index 0000000..252cb10 --- /dev/null +++ b/0_setup/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/0_setup/main.go b/0_setup/main.go new file mode 100644 index 0000000..df48234 --- /dev/null +++ b/0_setup/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "database/sql" + "flag" + "fmt" + "log" + "strings" + + _ "github.com/mattn/go-sqlite3" +) + +func main() { + dbpath := flag.String("db", "./db.db", "path to db file") + q := flag.String("q", "show tables", "query to execute") + flag.Parse() + + query := strings.Trim(strings.TrimSpace(*q), ";") + if len(query) == 0 { + panic("refusing empty query") + } + query += ";" + log.Printf("%s", query) + + db, err := sql.Open("sqlite3", *dbpath) + if err != nil { + panic(err) + } + + if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(query)), "SELECT") { + rows, err := db.Query(query) + if err != nil { + panic(err) + } + defer rows.Close() + + columns, err := rows.Columns() + if err != nil { + panic(err) + } + + ptrs := make([]any, len(columns)) + for i := range ptrs { + var v interface{} + ptrs[i] = &v + } + + for rows.Next() { + if err := rows.Scan(ptrs...); err != nil { + panic(err) + } + result := make([]string, len(columns)) + for i := range ptrs { + ptr := ptrs[i].(*interface{}) + result[i] = fmt.Sprintf("%s:%+v", columns[i], *ptr) + } + log.Println(result) + } + } else { + stmt, err := db.Prepare(query) + if err != nil { + panic(err) + } + + result, err := stmt.Exec() + if err != nil { + panic(err) + } + + affected, err := result.RowsAffected() + log.Printf("%d: %v", affected, err) + } + +} diff --git a/0_setup/mvp.sh b/0_setup/mvp.sh new file mode 100644 index 0000000..a310e7a --- /dev/null +++ b/0_setup/mvp.sh @@ -0,0 +1,51 @@ +#! /bin/bash + +main() { + set -e + set -o pipefail + + local bin=./0_setup + local cmds=( + 'DROP TABLE IF EXISTS a' + 'CREATE TABLE a( + x INT, + y VARCHAR(20), + PRIMARY KEY (x) + )' + 'DROP TABLE IF EXISTS b' + 'CREATE TABLE b( + x INT, + y VARCHAR(20), + PRIMARY KEY (x) + )' + 'SELECT * FROM a' + 'INSERT INTO a + (x, y) + VALUES + (4, "a") + ' + 'SELECT * FROM a' + 'SELECT * FROM b' + 'INSERT INTO b + (x, y) + VALUES + (5, "b") + ' + 'SELECT * FROM b' + 'UPDATE b + SET x=4 + WHERE y="b" + ' + 'SELECT * FROM b' + 'SELECT * FROM a JOIN b WHERE a.x == b.x' + 'SELECT a.y, b.y FROM a JOIN b WHERE a.x == b.x' + ) + + for cmd in "${cmds[@]}"; do + echo ================= + $bin -q "$cmd" + echo + done +} + +main "$@" diff --git a/1_/go.mod b/1_/go.mod new file mode 100644 index 0000000..d47f79b --- /dev/null +++ b/1_/go.mod @@ -0,0 +1,3 @@ +module 1_ + +go 1.18 diff --git a/2_/go.mod b/2_/go.mod new file mode 100644 index 0000000..ac141eb --- /dev/null +++ b/2_/go.mod @@ -0,0 +1,3 @@ +module 2_ + +go 1.18 diff --git a/3_/db.db b/3_/db.db new file mode 100644 index 0000000..e52c3d0 Binary files /dev/null and b/3_/db.db differ diff --git a/3_/mvp.sh b/3_/mvp.sh new file mode 100644 index 0000000..71c7512 --- /dev/null +++ b/3_/mvp.sh @@ -0,0 +1,31 @@ +#! /bin/bash + +main() { + set -e + set -o pipefail + + local bin=../0_setup/0_setup + local cmds=( + 'DROP TABLE IF EXISTS events' + ' + CREATE TABLE events ( + sensor_id integer not null, + event_type integer not null + ); + ' + 'INSERT INTO events VALUES (2, 2)' + 'INSERT INTO events VALUES (2, 4)' + 'INSERT INTO events VALUES (2, 2)' + 'INSERT INTO events VALUES (3, 2)' + 'INSERT INTO events VALUES (2, 3)' + "$@" + ) + + for cmd in "${cmds[@]}"; do + echo ================= + $bin -q "$cmd" + echo + done +} + +main "$@" diff --git a/4_/go.mod b/4_/go.mod new file mode 100644 index 0000000..5dd14ea --- /dev/null +++ b/4_/go.mod @@ -0,0 +1,3 @@ +module 4_ + +go 1.18 diff --git a/Test Summary - Codility.pdf b/Test Summary - Codility.pdf new file mode 100644 index 0000000..2d14afa Binary files /dev/null and b/Test Summary - Codility.pdf differ diff --git a/reflection.txt b/reflection.txt new file mode 100644 index 0000000..3c54a9c --- /dev/null +++ b/reflection.txt @@ -0,0 +1,4 @@ +task1: ezpz +task2: that one was like "if you turn on bulbs in order X, Y, Z; then how many seconds are all at+before the current bulb active?" I left a comment explaining "this is probably the google one where you have O(1) space by doing negatives; that'll do". Looks like my performance was bad (as I said in comment n**2). No surprise given it's space efficient. Looks like time complexity is Codility's priority. Surprised I failed 1 of 5 correctness, but that should be fatal. +task3: I LITERALLY DIDNT SORT YET GOT 100% CORRECT that'd disgusting +task4: I missed a multi-choice question. Bullshit. They were so fuckin easy and I bothered to double check. I don't trust proctor here.