Skip to content

Instantly share code, notes, and snippets.

@Highstaker
Highstaker / bubblesort.c
Last active October 28, 2016 06:27
Sorting algorithms in different languages
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ARR_LEN 100
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
@Highstaker
Highstaker / InfiniteList.py
Created October 13, 2016 15:44
Pretty much a regular list, but when iterated over, it does not stop in the end. Instead, it iterates indefinitely.
class InfiniteListEmptyError(Exception):
pass
class InfiniteList(list):
"""
Pretty much a regular list, but when iterated over, it does not stop in the end.
Instead, it iterates indefinitely.
"""
from random import SystemRandom
r=SystemRandom()
"""
TASK.
An array A of integers is given. Find all the combinations of indexes i,j,k, so A[i]+A[j]+A[k]==0. Preferably in O(N^2).
It's a variant of 3SUM puzzle.
"""
def bad_algorithm(INPUT):
from sys import argv
IN = float(argv[1])
PRECISION = 0.000000001
if IN == 1:
i = 1
elif IN > 1:
i = IN/2
@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);
@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 / 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 / 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 / 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 / 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;
}