Skip to content

Instantly share code, notes, and snippets.

View jasonknight's full-sized avatar

Jason Martin jasonknight

View GitHub Profile
@jasonknight
jasonknight / foobar.rb
Last active July 29, 2019 14:48
Trilogy Challenge: FooBar (FizzBuzz)
tests = [
{
:data => 1,
:expect => 1
},
{
:data => 2,
:expect => 2
},
{
class Node
attr_accessor :val,:nxt
def initialize(val,nxt)
@val = val
@nxt = nxt
end
end
def print_nodes(head)
while head do
puts head.val
function flatten(array) {
// Because we are going to do this recursively,
// we start with the "end", in this case, it's any
// none-array value, we put it into an array, because
// we are recursively "merging"
if ( ! Array.isArray(array) )
return [array];
var results = [];
for ( var i in array ) {
results = results.concat(
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char** argv) {
int *x = (int*)malloc(sizeof(int));
*x = 3;
int *y = x;
printf("x: %d, y:%d\n",*x,*y);
free(x);
printf("x: %d, y:%d\n",*x,*y);
free(y);
<?php
function create_connection() {
$mysqli = new mysqli("localhost", "root", "xxx");
if ( $mysqli->connect_errno )
die( $mysqli->connect_error );
$mysqli->set_charset('utf8');
return $mysqli;
}
$conn = create_connection();
$create_table = "
<?php
namespace Codeable;
function map_with_keys($cb,$a) {
$na = [];
foreach ($a as $k=>$v) {
list($nk,$nv) = call_user_func_array($cb,[$k,$v]);
$na[$nk] = $nv;
}
return $na;
}
function splitNumber(n,l) {
if ( l == 1 ) {
return [n];
}
var guess = Math.random(n / l + 1) * (((n/l) / 10 ) * 10);
return [guess].concat(splitNumber(n - guess,l - 1))
}
// Test Code
for ( var i = 0; i < 10; ++i ) {
var test_case = Math.random();
@jasonknight
jasonknight / interview_question_1.php
Last active August 2, 2018 06:18
Technical Interview Question #1
<?php
function sum_csv($str,$sep,$col) {
return floatval(array_reduce(array_slice(explode("\n",$str),1),
function ($a,$l) use ($col,$sep) {
return $a + explode($sep,$l)[$col];
}));
}
@jasonknight
jasonknight / tcp_timeout.go
Created May 16, 2018 16:08 — forked from hongster/tcp_timeout.go
Golang example on handling TCP connection and setting timeout.
package main
import (
"fmt"
"net"
"time"
"bufio"
)
func handleConnection(conn net.Conn) {
@jasonknight
jasonknight / Hx2o-8.lua
Last active May 14, 2017 22:19
Lua Object as a Function- https://repl.it/Hx2o/8
-- Lua object implemented with just functions.
function make(a,b)
return function(msg)
if msg == "a" then return a end
if msg == "b" then return b end
if msg == "mul" then
return function()
return a * b
end