News Algorithms Exposed!

Mar132009
1 评论

Y Combinator's Hacker News:











Formula:
(p - 1) / (t + 2)^1.5
Description:
Votes divided by age factor
p = votes (points) from users.
t = time since submission in hours.
p is subtracted by 1 to negate submitters vote.
age factor is (time since submission in hours plus two) to the power of 1.5.

Reddit:











Formula:

Reddit Algorithm

Description:

First of all, the time 7:46:43 am on December 8th 2005 is a constant used to determine the relative age of a submission. (It is likely the time the site launched but I have not been able to confirm this) The time the story was submitted minus the constant date is ts. ts works as the force that pulls the stories down the frontpage.
y represents the relationship of up votes to down votes.
45000 is the amount of seconds in 12.5 hours. This constant is used in combination with yts to "water down" votes as they are made farther and farther from the time the article was submitted.
log10 is also used to make early votes carry more weight than late votes. In this case, the first 10 votes have exactly as much weight as votes 11 through 101.

StumbleUpon:














Formula:
(Initial stumbler audience / # domain) + ((% stumbler audience / # domain) + organic bonus – nonfriend) – (% stumbler audience + organic bonus) + N

Description:
The initial stumbler "power" (Audience of the initial stumbler divided by the amount of times that stumbler has stumbled the given domain) is added to the sum of all the subsequent stumbler's powers.
Subsequent stumbler power is ((Percentage of audience stumbler makes up divided by the number of times given stumbler has stumbled domain) + a predetermined power boost for using the toolbar - a predetermined power drain if stumblers are connected) + (% of the stumbler audience + a predetermined boost for using the toolbar)
N is a "safety variable" so that the assumed algorithm is flexible. It represents a random number.

C语言运算符表

Mar072009
0 评论

C语言运算符

运算符按照优先级大小由上向下排列,在同一行的运算符具有相同优先级。第二行是所有的一元运算符。

运算符
解释
结合方式
() [] -> .括号(函数等),数组,两种结构成员访问
由左向右
! ~ ++ -- + -

* & (类型) sizeof

否定,按位否定,增量,减量,正负号,

间接,取地址,类型转换,求大小

由右向左
* / %乘,除,取模
由左向右
+ -加,减
由左向右
<< >>左移,右移
由左向右
< <= >= >小于,小于等于,大于等于,大于
由左向右
== !=等于,不等于
由左向右
&按位与
由左向右
^按位异或
由左向右
|按位或
由左向右
&&逻辑与
由左向右
||逻辑或
由左向右
? :条件
由右向左
= += -= *= /=

&= ^= |= <<= >>=

各种赋值
由右向左
,逗号(顺序)
由左向右

bits mirror

Mar062009
0 评论

0 000 000 000 0
1 001 010 100 4
2 010 100 010 2
3 011 110 110 6
4 100 001 001 1
5 101 011 101 5
6 110 101 011 3
7 111 111 111 7
// data: array
// N:size of array

void mirror(char *data, const int N)
{
int target = 0;
for (int position = 0; position< N; position++) {
if (target > position)
swap(data[position], data[target];
int Mask = N;
while (target & Mask >>= 1){
// unset
target &= ~Mask;
}
target |= Mask;
}
}

a linux kernel macro

Mar032009
0 评论

/*
* Macro used to acces an element in a list.
* Type casting a '0' to type* then accessing its member....?
*/

//this is a common trick for calculating the offset of a member in a
//struct. The offsetof() macro works this way too.

#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

//you cheat the compiler, telling it to treat a NULL pointer as a pointer
//to the struct. Then you try to access a member of through that pointer
//and store its address. Because the we used NULL, the result is the
//number of bytes between the struct's beginning and the member's address.
//This whole story is expressed in this expression: &((type *)0)->member))
//Now since we got the offset of a specific member in a struct, we need to
//subtract it from the enclosing struct to get that enclosing struct's
//address: (char *)(ptr)-(unsigned long). Finally, we cast the result to
//the desired type.

/*
* Example of its use
*/
struct super_block {
...
struct list_head s_files;
...
} *sb = &some_super_block;

struct file {
...
struct list_head f_list;
...
} *file;

struct list_head *p;

for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
struct file *file = list_entry(p, struct file, f_list);
do something to 'file'
}

'#' in Gcc

Mar022009
0 评论

#define __A(v) abcd#v
#define __B(v) abcd##v
#define __C(v) v##abcd
这三个区别是:
__A(test1)展开后变成: abcd "test1" (加双引号并插入空格)
__B(test2)展开后变成: abcdtest2
__C(test3)展开后变成: test3abcd

A bit of Code style!

Mar012009
0 评论

> +static int format_decode(const char *fmt, enum format_type *type, int *flags,
> + int *field_width, int *base, int *precision,
> + int *qualifier)

To

struct printf_spec {
enum format_type type;
int flags, field_width, base, precision, qualifier;
};

and then pass things around like

static int format_decode(const char *fmt, struct printf_spec *spec)
{
..

I suspect it would make the code look nicer too (instead of doing
"*base = x", you'd see "spec->base = x" and it would look less like
line noise in the callee, an the caller could justdo a single "struct
format_spec spec = { 0, }" to initialize that thing).