Skip to content

Instantly share code, notes, and snippets.

@tinylamb
tinylamb / Kmeans.py
Last active December 22, 2015 19:29
Kmeans algorithm
#coding=utf-8
import random,math,pylab
def dis(p1,p2):#compute distance between p and c
f=lambda (x,y):(x-y)**2
return sum(map(f,zip(p1,p2)))
def update_center(sets):#sets=[(),(),...]
n=len(sets)
x,y=zip(*sets)
@tinylamb
tinylamb / KNN.py
Created September 11, 2013 06:59
KNN algorithm
#coding=utf-8
#KNN algorithm
#describe:given a Classification Class={ci|i=1...n} and ci={sample(j)|j=1...n}
#input test_sample
#output which class the test_sample in/ie.test_sample ∈ C?
import random
def dis(p1,p2):
f=lambda (x,y):(x-y)**2
return sum(map(f,zip(p1,p2)))
@tinylamb
tinylamb / Sqlist.c
Last active December 23, 2015 15:39
DataStucture :Sqlist
#include <stdio.h>
#include <stdlib.b>
#define INITSIZE 100
#define INCREMENT 10
#define TRUE 1
#define FALSE 0
#define ElemType int //根据需要改变ElemType指代的类型
/*...ADT Sequence List...*/
@tinylamb
tinylamb / LinkedList.c
Last active December 23, 2015 16:59
Linked list
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
/*...ADT Linked List...*/
/*数据集的定义 */
typedef struct Lnode{
ElemType data;
struct Lnode *next;
@tinylamb
tinylamb / Contiguous vs. Linked Data Structures
Last active December 23, 2015 16:59
Data Structures(1)
1.为什么我们要了解常用的数据结构?
Changing the data structure does not change the correctness of the program, since we presumably
replace a correct implementation with a different correct implementation.
However, the new implementation of the data type realizes different tradeoffs
in the time to execute various operations, so the total performance can improve dramatically.
2.学习数据结构的建议
In data structures, as with most subjects, it is more important to really understand
the basic material than have exposure to more advanced concepts. We
will focus on each of the three fundamental abstract data types (containers, dictionaries,
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
@tinylamb
tinylamb / binarytree.c
Created September 24, 2013 03:19
binary search tree
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
typedef struct tree{
ElemType item;
tree *left;
tree *right;
}tree;
/*
@tinylamb
tinylamb / Chapter 1.c
Last active December 23, 2015 21:29
"The C Programming Language", 2nd edition, Kernighan and Ritchie Answers to Exercises
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/
/*
Exercise 1-1. Run the ``hello, world'' program on your system. Experiment with
leaving out parts of the program, to see what error messages you get.
*/
#include <stdio.h>
int main(){
printf("hello world.\n");
return 0;
}
@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(){
@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){