Skip to content

Instantly share code, notes, and snippets.

@Highstaker
Highstaker / mysql-check.go
Created May 20, 2019 17:41
Just some experiments and attempts to abstract MySQL quering boilerplate away.
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"math/rand"
"runtime"
"strings"
@Highstaker
Highstaker / c_lpthreads_example.c
Created February 10, 2018 13:10
An example of threading in C. Shows how one can pass results (via return or pointer argument)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* t_routine(void* args){
unsigned int i = 0;
int a;
printf("Running thread!\n");
for(i=0;i<4000000000;i++){
a = 2*2;
@Highstaker
Highstaker / heapsort.c
Created March 29, 2017 15:39
Generates a list of random integers, makes a heap, and then performs heapsort.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <assert.h>
// #define ARRAY_SIZE 100
#define MAX_NUMBER 100
#define bool char
#include <stdlib.h>
#include <stdio.h>
int compare_int(void* va, void* vb){
int* a = va;
int* b = vb;
return *b - *a;
}
@Highstaker
Highstaker / reverse_words.c
Created December 1, 2016 07:01
reverses a string of space-separated words terminated by newline
// reverses a string of space-separated words terminated by newline
#include <stdio.h>
#include <stdlib.h>
void swap(char* arr, int i, int j){
char t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
@Highstaker
Highstaker / limited_maximum.py
Created November 30, 2016 17:20
A function that takes a list and a number. Returns the maximum number that is no larger than def_max.
"""
A function that takes a list and a number. Returns the maximum number that is no larger than def_max.
"""
def bad_definedMax(in_array, def_max):
"""
O(N) (not counting the sorting)
"""
@Highstaker
Highstaker / ip_validator.py
Created November 17, 2016 16:13
Simple IP validation with Python and regex
import re
number_pattern = r"(([1-9]?[0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))"
IP_validator = re.compile(r'^{0}(\.{0}){1}$'.format(number_pattern, "{3}"))
def is_valid_IP(strng):
return bool(IP_validator.match(strng))
is_valid_IP("12.34.56.708")
@Highstaker
Highstaker / ascii85.py
Created November 17, 2016 11:31
Ascii85 encoding-decoding
from itertools import groupby
def split_list(alist,max_size=1):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(alist), max_size):
yield alist[i:i+max_size]
def toAscii85(data):
result = []
for chunk in split_list(data, max_size=4):
@Highstaker
Highstaker / atod.c
Created November 3, 2016 13:15
A function that transforms strings into a double. Supports E notation.
#include <ctype.h>
#include <stdio.h>
#include <math.h>
double atod(char * s)
{
int i;
for(i=0;isspace(s[i]);i++);
int sign = 1;
@Highstaker
Highstaker / find_string_pattern.c
Created November 3, 2016 12:29
Looks for a pattern in string.
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int findPatternRight(char* s, char* pat){
bool flag = true;
int i, j, k, left;
int len_s = strlen(s);