Skip to content

Instantly share code, notes, and snippets.

View nicholaskajoh's full-sized avatar
👨‍💻
Debugging

Nicholas Kajoh nicholaskajoh

👨‍💻
Debugging
View GitHub Profile
@nicholaskajoh
nicholaskajoh / sql.php
Last active March 15, 2019 13:29
Keyword search
<?php
$search_query = "cattle rearing"; // example
$search_words = explode(" ", $search_query);
$sql = "SELECT * FROM _index WHERE ";
for ($i = 0; $i < count($search_words); $i++) {
$sql .= "CONTAINS(keywords, '{$search_words[$i]}')";
if ($i < count($search_words) - 1) $sql .= " AND ";
}
echo "SQL Query: " . $sql;
@nicholaskajoh
nicholaskajoh / round_robin.py
Last active February 20, 2019 12:11
Round Robin Scheduling Algorithm
import random
import matplotlib.pyplot as plt
import numpy as np
def generateWorkload():
n = 20 # size of workload
inter_arrival_time_range = (1, 15) # nanoseconds
burst_time_range = (70, 130) # nanoseconds
processes = []
for i in range(n):
/* calculate the difference in minutes between 2 date times
given that only the minutes within a specified time range
every day should be counted
e.g: say the time range is 6AM to 6PM
if datetime1 = 4AM and datetime2 = 5AM, difference = 0 minutes
this is because 4AM to 5AM doesn't fall within the range of
6AM to 6PM so the 60 minutes isn't counted
e.g 2: say we have same time range and,
datetime1 = 5AM today and datetime2 = 7PM tomorrow,
difference = 1440 minutes
@nicholaskajoh
nicholaskajoh / log.js
Created July 3, 2018 11:36
Log stack trace for unhandled promise rejection warning.
process.on('unhandledRejection', r => console.log(r));
@nicholaskajoh
nicholaskajoh / mocha-sails.sh
Last active June 27, 2018 17:54
Test single file in Mocha (Sails.js)
# assuming we want to run the tests in User.test.js
# remember to add test/bootstrap.test.js to mocha.opts
NODE_ENV=test node_modules/.bin/mocha test/integration/model/User.test.js
@nicholaskajoh
nicholaskajoh / k-means.py
Last active May 1, 2018 08:01
K Means algorithm in Python
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[4, 3], [0, 0], [2, 4], [3, 4], [5, 4], [-2, 1], [-3, 0], [-3, -3], [8, 12], [11, 11], [9, 10]
])
K = 3
tol = 0.001
max_iter = 25
import numpy as np
# data
heights = np.array([6.3, 5.9, 5.1, 5.6, 5.1])
weights = np.array([50.2, 79.7, 61.4, 47.1, 59.8])
classes = np.array(["Male", "Female", "Female" , "Male", "Female"])
# P(Class)
males_count = 0
main {
max-width: 1200px;
margin: 0 auto;
}
section {
display: flex;
flex-wrap: wrap;
}
article, aside { flex-basis: 100%; }
@media (min-width: 576px) {
@nicholaskajoh
nicholaskajoh / tribonacci_triangle.py
Created November 6, 2017 09:09
Python function to generate n rows of a Tribonacci Triangle.
def tribonacci_triangle(n):
# n <- number of rows to generate
# a <- tribonacci triangle
a = [[1], [1, 1]]
if n == 1:
return [[1]]
elif n == 2:
return a
else:
# generate rows 3 to n
@nicholaskajoh
nicholaskajoh / form_submit.php
Created September 9, 2017 16:03
Form submission -- CDC
<?php
$fields = [];
$fields['name'] = $_POST['name'] ?? NULL;
$fields['sex'] = $_POST['sex'] ?? NULL;
$fields['q1'] = $_POST['q1'] ?? NULL;
$fields['q2'] = $_POST['q2'] ?? NULL;
$fields['q3'] = $_POST['q3'] ?? NULL;
// validation
$errors = [];