LeetCode Contest 42

今天晚上在朋友家做好饭已经过了比赛时间了,我们是边吃着饭边喝着酒来写代码。牛肉炖土豆是一如既往的美味,新尝试的豆腐煮白菜也很不错。这次朋友买了Habanero,辣味惊人,我是打着喷嚏做菜的😢

第一题Set Mismatch

题目大意:给定一个长度为n数值在1到n之间的数组。已知其中一个数字出现一个,另一个数字没有出现。找出这两个数字。

简单的统计。

class Solution(object):
    def findErrorNums(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        ans = [0, 0, 0]
        cnt = [0] * len(nums)
        for i in nums:
            cnt[i-1] += 1
        for i, c in enumerate(cnt):
            ans[c] = i+1
        return [ans[2], ans[0]]

Read More

Lambda Calculus

This post is my note for What is seminar on Lambda Calculus.

Lambda calculus was created by Alonzo Church in the 1930s, and was used by him to solve Entscheidungsproblem in 1936, which is related to Hilbert's tenth problem. In the same year, Alan Turing independently solved Entscheidungsproblem using his invention Turing machine. Shortly after, Turing realized that these two models are actually equivalent as models of computation.

In this note, I will first give the formal definition of lambda expressions. Then with the help of Python, I am going to show how to do Boolean algebra and basic arithmetic using lambda calculus, which to some extend gives an illustration that Turing machine and lambda calculus are equivalent.

Definition

Lambda calculus consists of lambda expressions and operations on them. There are three basic elements in Lambda expression:

  1. variables: x, y, z, ...
  2. symbols in abstraction: λ and .
  3. parentheses for association: ()

Read More

maupassant: a Pelican theme

我现在用的博客生成软件是Hexo。这个软件可以快速将Markdown格式的文章转成html格式,并且包含发布到github page的工具。Hexo是基于Nodejs,所以某天我就在想有没有一个基于Python的博客生成软件。为什么我会这样想?因为

人生苦短,我用Python。

当然会有基于Python的博客生成软件:Pelican是最为突出的一个。事实上,我很喜欢Pelican!其中一个亮点是它的主题都是基于Jinja,这种简单统一的模版格式让用户很容易设计自己喜欢的主题。出于练手的原因,我将我喜欢的一个Hexo主题maupassant移植到Pelican了。

Read More

Cassini Ovals

This post is my note for What is seminar on Cassini Ovals.

Definition

An ellipse is a geometric object formed by locus of points which have fixed sum of distances to two fixes foci.

Ellipse

In the above figure, we can express the definition of ellipses in one simple equation:

$$|PF_1|+|PF_2|=c,$$


for some \(c>0\). What if we change the addition in the above equation to multiplication? This comes the definition of Cassini ovals.

Read More

Leetcode Contest 38

这次比赛我表现得比较差,只能做出第一题和第三题。第四题比赛结束之后很快也写出来了,而第二题确实花了我一个多小时才能想出来。

第一题Maximum Product of Three Numbers

给定一个长度不小于3的数组,问任选三个数得到的最大乘积是多少。

先将数组排序,那么最大值必然在下面四种情况中:

  1. 第一个数 X 第二个数 X 第三个数
  2. 倒数第一个数 X 倒数第二个数 X 倒数第三个数
  3. 第一个数 X 倒数第一个数 X 倒数第二个数
  4. 第一个数 X 第二个数 X 倒数第一个数
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return max(nums[0]*nums[1]*nums[2],
                    nums[-1]*nums[-2]*nums[-3],
                    nums[0]*nums[-1]*nums[-2],
                    nums[0]*nums[1]*nums[-1])

Read More

Rademacher functions

This post is my note for What is…? seminar on Rademacher function.

For a real number \(x\) in \([0, 1]\), let \((a_1(x), a_2(x), \cdots, a_n(x), \cdots)\) be its binary representation. That is to say, \[\begin{equation} x = \sum_{n=1}^\infty \frac{a_n(x)}{2^n}. \label{20170623-1} \end{equation}\]

For some \(x\), there might be two possible binary representation. Take \(\frac{1}{2}\) for example, it can be represented as \((1, 0, 0, \cdots)\) or \((0, 1, 1, \cdots)\). In this situation, we always prefer the former representation, in which all terms become \(0\) eventually.

Definition 1. Let \(n \ge 1\) be an integer. The \(n\)-th Rademacher function \(r_n: [0, 1] \to \{-1,1\}\) is defined to be \[r_n(x) = 1 - 2a_n(x).\]

Read More

Lüroth expansion

This post is my note for What is…? seminar on Lüroth expansion.

Definition 1. A Lüroth expansion of a real number \(x \in (0, 1]\) is a (possibly) infinite sequence \((a_1, a_2, \cdots, a_n, \cdots)\) with \(a_n \in \N\) and \(a_n \ge 2\) for all \(n \ge 1\) such that \[\begin{equation} x = \frac{1}{a_1} + \frac{1}{a_1(a_1-1)a_2} + \cdots + \frac{1}{a_1(a_1-1)\cdots a_{n-1}(a_{n-1}-1)a_n} + \cdots \end{equation}\]

By abuse of notation, we will something write the right hand side of Definition 1 as \((a_1, a_2, \cdots, a_n, \cdots)\).

Given a system of representation of real numbers in \((0, 1]\), we need to determine whether these representations are 1 to 1 corresponding the numbers in \((0, 1]\). Aslo we should study the (Lüroth) shift map: \[\begin{equation} T: (a_1, a_2, \cdots, a_n, \cdots) \mapsto (a_2, \cdots, a_n, \cdots). \label{20170620-2} \end{equation}\]

Read More

Raspberry Pi Zero W无屏设置

我最近入手了一个Raspberry Pi Zero W,作为平时消遣的一个小玩具。因为我主要是想在它上面运行一些小程序,所以我希望将它设置为最简单的服务器,然后通过ssh来访问。这篇文章记录了我从网上搜来的设置步骤,方便下次设置。

制作SD启动盘

首先说明一下:我的系统是Mac OSX。在制作SD启动盘过程中,我们需要修改启动盘的内容,Mac OSX是不支持启动盘的格式的,所以我们需要借助其他途径。我是用VitualBox的Ubuntu来修改启动盘的内容。

第一步:下载raspbian lite,解压得到一个后缀名为img的镜像文件。我得到的是:2017-04-10-raspbian-jessie-lite.img

Read More

Leetcode Contest 37

第一题Maximum Distance in Arrays

给定一个包含数组的数组,每个数组都已经递增排序。我们要做的是从两个数组中各选一个数,使得这两个数的差的绝对值最大。问这个最大的值是多少?

对于第i数组中,取出最大值a,最小值b,然后将(a, i)和(b, i)放进一个数组m里。将数组m按照第一个分量排序,那么对我们有用的只是数组m前面两个元素和后面两个元素。如果第一个元素和最后一个元素来自不一样的数组(第二个分量不一样),那么答案就是这两个元素的第一个分量的差。否则,我们要比较两种情况:第一个元素与倒数第二个元素,第二个元素与最后一个元素。

class Solution(object):
    def maxDistance(self, arrays):
        """
        :type arrays: List[List[int]]
        :rtype: int
        """
        m = []
        for i, a in enumerate(arrays):
            m.append((a[0], i))
            m.append((a[-1], i))
        m.sort(key=lambda x: x[0])
        if m[0][1]!=m[-1][1]:
            return m[-1][0]-m[0][0]
        return max(m[-2][0]-m[0][0], m[-1][0]-m[1][0])

Read More

Leetcode Contest 35

第一题Can Place Flowers

给定一个01数组代表一排花盆,0代表是可以种花,1代表不可以。种花的时候我们要确保左右两边的花盆是空的。问能不能种n株花。

简单的贪心。

class Solution(object):
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        cnt = 0
        for i in xrange(len(flowerbed)):
            if flowerbed[i] == 1:
                continue
            if i-1>=0 and flowerbed[i-1]==1:
                continue
            if i+1<len(flowerbed) and flowerbed[i+1]==1:
                continue
            flowerbed[i] = 1
            cnt += 1
        return cnt>=n

Read More