Skip to content

Instantly share code, notes, and snippets.

View zhangys-lucky's full-sized avatar

Yansheng Zhang zhangys-lucky

View GitHub Profile
@zhangys-lucky
zhangys-lucky / max.c
Created August 24, 2015 01:17
There are two int variables: a and b, don’t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.
int max(int a,int b)
{
return ( ( a + b ) + abs( a – b ) ) / 2;
}
@zhangys-lucky
zhangys-lucky / quicksort.py
Created August 19, 2015 06:29
quick sort implement using python
#!/usr/bin/python
# -*- coding: utf-8 -*-
def sub_sort(array,low,high):
key = array[low]
while low < high:
while low < high and array[high] >= key:
high -= 1
while low < high and array[high] < key:
array[low] = array[high]
@zhangys-lucky
zhangys-lucky / project_euler_31_coin_sum.py
Created August 19, 2015 00:48
project_euler_31_coin_sum using dynamic programming
#! /usr/bin/python
MAX_COIN = 200
arr = [i * 0 for i in range(201)]
arr[0] = 1
for coin in [1,2,5,10,20,50,100,200]:
j = coin
while j <= MAX_COIN:
@zhangys-lucky
zhangys-lucky / simple_generate_prime_table.c
Last active August 29, 2015 14:27
simple way to generate prime table using C
#define N (1<<22)+200
long long prime[N];
void get_prime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<N;i++)
{
if(prime[i]==0) prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&(i*prime[j]<N);j++)
@zhangys-lucky
zhangys-lucky / generate_prime_table.py
Created August 19, 2015 00:44
generate prime table with some tricks
#! /usr/bin/python
from is_prime import is_prime
prime = [i * 0 for i in range(1000)]
prime[0] = 1
prime[1] = 2
prime[2] = 3
@zhangys-lucky
zhangys-lucky / isprime.py
Created August 19, 2015 00:43
check prime
#! /usr/bin/python
from sys import argv
from math import sqrt
def is_prime(num):
s = sqrt(num)
step = 4
if num <= 3:
return True
@zhangys-lucky
zhangys-lucky / pi.c
Created August 19, 2015 00:40
print PI using C
#include <stdio.h>
long a=10000,b,c=2800,d,e,f[2801],g;
int main() {
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
return 0;
}
@zhangys-lucky
zhangys-lucky / trie.c
Created August 19, 2015 00:39
Trie Tree implement with C
struct TrieNode {
unsigned char v;
struct TrieNode* children[26];
};
#define CACHE_SIZE 100000
static struct TrieNode node_cache[CACHE_SIZE];
static int used_cache_size = 0;
@zhangys-lucky
zhangys-lucky / topkword.py
Last active August 29, 2015 14:27
find top k words using Trie and MinHeap(Python Code)
class TrieNode(object):
# Initialize your data structure here.
def __init__(self):
self.count = 0
self.children = {}
class Trie(object):
def __init__(self):
@zhangys-lucky
zhangys-lucky / bc.py
Created July 14, 2015 10:36
python broadcast
# Send UDP broadcast packets
MYPORT = 50000
import sys, time
from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)