Skip to content

Instantly share code, notes, and snippets.

@tinylamb
tinylamb / findk.c
Created November 24, 2013 06:36
find k-th small number
/*input :A[n]
* output:k-th large number
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define LEN(a) (int)(sizeof(a)/sizeof(a[0]))
#define SWAP(a,b) do{int temp;temp=a;a=b;b=temp;}while(0)
int searchk(int a[],int left,int right,int k);
void print(int a[],int start,int end);
@tinylamb
tinylamb / random_file.c
Created November 20, 2013 13:41
<The practice of Programming>markov链.keywords: hash table
/*
#include <stdio.h>
#define FILEPATH "/Users/tinylamb/Documents/coding/santi.txt"
int main(){
FILE *fp;
fp=fopen(FILEPATH,"r");
char c;
while((c=fgetc(fp))!=EOF)
printf("%c",c);
fclose(fp);
@tinylamb
tinylamb / Crawler.py
Last active December 28, 2015 18:29
抓取豆瓣日志信息的爬虫,信息包括 日志名称,URL,评论数,发布时间
#encoding:utf-8
from Tkinter import *
import urllib2
from lxml import html
import os,sys
class App:
def __init__(self,master):
self.label=Label(master,text='输入日记首页地址:')
self.label.pack(side='left',padx=5)
@tinylamb
tinylamb / ch6.c
Last active December 27, 2015 10:39
<The C Programming language> -chap6 Structures
/*Exercise 6-1. Our version of getword does not
* properly handle underscores, string constants,
* comments, or preprocessor control lines. Write a better version.
*/
#include <stdio.h>
#include <ctype.h>
#define LIMIT 20
#define STRCONSTANT 0
#define COMMENT 1
#define WORD 2
@tinylamb
tinylamb / sort_algorithm.c
Last active December 26, 2015 00:19
Sort Algorithm
/*
quick sort v1
http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F
*/
#include<stdio.h>
void qsort(int a[],int left,int right);
int findp(int v[],int left,int right);//find pivot position
void swap(int *,int *);
int main(){
int a[]={5,4,3,1,4,5,6,8,9,0};
@tinylamb
tinylamb / ch5.c
Last active December 25, 2015 11:39
<The C Programming language> -chap5 Pointer and Array 15/20
/*
Exercise 5-1. As written, getint treats a + or - not followed
by a digit as a valid representation of zero.
Fix it to push such a character back on the input.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXSIZE 100
int getch();
@tinylamb
tinylamb / Stack.c
Last active December 25, 2015 08:49
ADT stack
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
#define MAXSIZE 100
/*...ADT stack...*/
/*数据集定义*/
typedef struct Stack{
int stacksize;
@tinylamb
tinylamb / chapter 4.c
Last active December 24, 2015 11:09
<The C Programming language>-chapter 4 Functions and Program Structure undo 8/9/10/11.keywords: 字符串匹配
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/
/*
Exercise 4-1. Write the function strindex(s,t) which returns the position of
the first occurrence of t in s, or -1 if there is none.
*/
#include <stdio.h>
#define SIZE 100
int strindex(char s[] , char p[]);//return (p in s)?index:-1
@tinylamb
tinylamb / Chapter3.c
Last active December 24, 2015 07:19
<The C Programming language> -Ch3 Control Flow 6/6
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/
/*
Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the price of
more tests outside.) Write a version with only one test inside the loop and measure the difference in runtime.
*/
#include <stdio.h>
int binarysearch(int a[],int x,int n){
int low=0,high=n-1;
int mid=(low+high)/2;
while(low<=high && a[mid]!=x){
@tinylamb
tinylamb / chapter2.c
Last active December 24, 2015 06:39
<The C Programming language> -Ch2 Types, Operators and Expressions10/10
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/
/*
Exercise 2-1. Write a program to determine the ranges of char, short, int, and long variables, both
signed and unsigned, by printing appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of the various floating-point types.
*/
/*********too slow***************
#include <stdio.h>
#define ElemType signed short
int main(){