Skip to content

Instantly share code, notes, and snippets.

View hallazzang's full-sized avatar

Hanjun Kim hallazzang

  • MilkyWay
  • Seoul, Republic of Korea
  • 16:02 (UTC +09:00)
View GitHub Profile
@hallazzang
hallazzang / periodic_events.c
Created February 26, 2017 07:42
Implementing periodic events in C (Windows)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
typedef struct {
void (*handler)(char *);
char parameter[128];
int interval, last_emitted;
} Event;
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#define MAX_BULLETS_COUNT 100
typedef struct {
int alive;
char ch;
int x, y, color;
@hallazzang
hallazzang / is_leap_year.py
Created August 25, 2016 16:56
Python - is_leap_year
from datetime import date
def is_leap_year(year):
try: date(year, 2, 29)
except ValueError: return False
else: return True
@hallazzang
hallazzang / fibonacci.py
Created April 15, 2016 10:30
python fibonacci number generator code in less than 40 bytes
f=lambda a,b,n:f(b,a+b,n-1)if n>1else b # simple and fast
# for test
import sys
sys.setrecursionlimit(5001) # as default, it was 1000 in my computer
print(f(0,1,5000)) # 0 1 1 2 3 5 8 13 21 ...
print(f(1,1,5000)) # 1 1 2 3 5 8 13 21 34 ...
@hallazzang
hallazzang / drawline.py
Created October 26, 2015 15:43
Pygame line drawing example using Bresenham Algorithm
import pygame
from pygame.locals import *
WIDTH, HEIGHT = 100, 100
PIXEL_SIZE = 5 # should be bigger than 1
SCREEN_WIDTH, SCREEN_HEIGHT = WIDTH * PIXEL_SIZE, HEIGHT * PIXEL_SIZE
surf = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
surf.fill((0, 0, 0))
@hallazzang
hallazzang / slicetime.py
Created October 21, 2015 14:01
Tricky time representation in Python
class SliceTime(object):
@property
def hour(self):
return self._hour
@hour.setter
def hour(self, value):
if not isinstance(value, (int, long)) or not 0 <= value <= 23:
raise ValueError('hour must be an integer value in 0 ~ 23')
else:
@hallazzang
hallazzang / bot.py
Last active August 29, 2015 14:21
Kakaostory Comment Bot Example
# coding: utf-8
import requests
import time
from urlparse import parse_qsl
from bs4 import BeautifulSoup
class KakaostoryAPI(object):
def __init__(self, raw_cookie):
self.session = requests.Session()
@hallazzang
hallazzang / example.py
Created May 12, 2015 16:32
Kakaostory like bot example
# coding: utf-8
import requests
import time
class KakaoStoryAPI(object):
def __init__(self, cookie):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0',
'Accept': 'application/json',
@hallazzang
hallazzang / locker.py
Last active August 29, 2015 14:20
Python object locker
try:
import cPickle as pickle
except ImportError:
import pickle
import zlib
from Crypto.Cipher import AES
from Crypto.Hash import MD5
from Crypto import Random
from base64 import b64encode, b64decode
@hallazzang
hallazzang / formatted.py
Last active August 29, 2015 14:19
Python string interpolation example (Like Swift)
import re
import inspect
FORMAT_REGEX = re.compile(r'\\\(([a-zA-Z_]\w*)\)')
def formatted(string, silent=False):
to_be_replaced = FORMAT_REGEX.findall(string)
variables = inspect.stack()[1][0].f_locals
for item in to_be_replaced:
if item in variables:
string = string.replace('\({})'.format(item), str(variables[item]))