3.λ.形而下技术博客

这里只关注技术实现,用代码说话。

如何获取下载源

作为一个downloader开始下载资源首先要向tracker宣布自己的存在,同时获得其他downloader的地址。 这里的downloader我们统一称为对等节点(peer),因此接下来要做的事情就是与tracker通信,获取peers。

阅读全文 »

bt种子文件

bt通过种子文件分享已经是一个过去时了,2009年btChina就已经关闭了。现在一般都是 使用磁力链接来分享文件。那么为什么种子文件分享不再流行了呢?为什么要用磁力链接呢? 磁力链接怎么实现的呢?

嗯这是这个系列要研究的问题。但是要研究磁力链接的实现原理,最好先从种子文件开始。

阅读全文 »

再来一个例子

看看以下运行的结果是什么呢?

1
2
3
4
(defmacro a_macro [x]
(list '+ 1 x))

(macroexpand-1 '(a_macro (+ 1 2)) ) ;; => ?

如果是一个函数呢?

1
2
3
4
(defn a_func [x]
(list '+ 1 x))

(a_func (+ 1 2)) ;; => ?
阅读全文 »

宏是怎么扩展的呢?

比较流行的编程语言中,Java,Python都没有定义宏的功能。c语言中的宏限制比较大, 没有Lisp中的宏强大。以下提到的宏,指的是Lisp方言中的宏。

宏的求值可以分为两个步骤

  1. 扩展(expand)–编译期
  2. 求值(eval) –运行期

这看起来很简单,但是实际写macro的时候,就会有很多的困惑。这里的主要矛盾就是理解 以下两个过程,

  1. 在一个宏的内部调用另一个函数
  2. 在一个宏的内部调用另一个宏

不同的lisp方言有不同的实现。以下用Clojure来研究宏的用法。

阅读全文 »

题目概述

原题链接

Given a data stream input of non-negative integers a1, a2, …, an, …, summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, …, then the summary will be:

[1, 1] [1, 1], [3, 3] [1, 1], [3, 3], [7, 7] [1, 3], [7, 7] [1, 3], [6, 7]

阅读全文 »

题目概述

原题链接

Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list – whose elements may also be integers or other lists. Input: [[1,1],2,[1,1]] Output: [1,1,2,1,1]

阅读全文 »

题目概述

原题链接

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
1
2
3
4
5
6
7
Example 1:
3
/ \
2 2
\ \
4 1
Maximum amount of money the thief can rob = 3 + 4 + 1 = 8.
阅读全文 »

问题

这是一个经典题,面试的时候经常会遇到。 例如:文件的内容如下

26329
46184
94842
9036
96555
40954
38187
15548
51452
861
51010
8721
13666
69837 

每行一个数字,找到出现次数最多的数。通常的解法是遍历文件一遍,用hashmap维护一个(number => freq), 然后用最小堆找到最大的10个数。

阅读全文 »

题目概述

312原题链接

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

examples:
nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167
阅读全文 »
0%