楽しい

按理说,我们二十五六岁,买车买房,工作体面,不能再抱怨了。但是房,车,体面,这些都是大路商品。我有,其他人可能有我的一百倍,一千倍还多。但是十年的青春时光,每个人都只有一次。

我希望看到这里的人,可以记得未来很重要,但是二十多岁一定要快意,可以努力奋斗也可以玩,可以富有也可以贫穷,但不能扭曲,要快意。

当你26岁决定自己要不要成为30岁的博士时,要记得这件事。

To be honest, we have a decent job, house, car at the age of 25, should not complain more. However, cars/houses/good jobs, all of them, are general commodities, others may have them of ten times or even of hundred times than of what I have. But the ten years of youth, everyone has only one time.

So, please remember this thing, when you are 26 years old and decide whether you want to be a 30-year-old Doctor.

Machine learning, can?

其实我们都已经意识到了,机器学习正在朝着超大规模参数的趋势发展。以后决定机器学习准确率的是GPU的数量和数据的使用权,而不再是研究机器学习的人。

Actually, we have been aware of the trending of massive scale parameters in machine learning. The number of CPUs and the access of data determines the final performance, but not the person who researches machine learning algorithms.

Mitchell Approximation

A method of computer multiplication and division is proposed which uses binary logarithms. The logarithm of a binary number may be determined approximately from the number itself by simple shifting and counting. A simple add or subtract and shift operation is all that is required to multiply or divide.

#include<stdio.h>

int main() {
    float a = 12.3f;
    float b = 4.56f;
    int c = *(int*)&a + *(int*)&b - 0x3f800000;
    printf("Approximate result:%f\n", *(float*)&c);
    printf("Accurate result:%f\n", a * b);
    return 0;
}

Semiring

import numpy as np
import networkx as nx
from functools import reduce
import matplotlib.pyplot as plt

connect_graph = np.array([[0, 1, 0, 0, 0], 
                          [0, 0, 0, 1, 0], 
                          [0, 0, 0, 1, 0], 
                          [0, 0, 0, 0, 1], 
                          [0, 0, 1, 0, 0]])

def ring_add(a, b):
    return a or b

def ring_multi(a, b):
    return a and b

def dot_product(i, j):
    row = connect_graph[i]
    column = connect_graph[:,j]
    return reduce(ring_add, [ring_multi(a, b) for a, b in zip(row, column)])

def next_generation(connect_graph):
    candidate_number = connect_graph.shape[0]

    new_connect_graph = np.zeros((candidate_number, candidate_number))

    for i in range(candidate_number):
        for j in range(candidate_number):
            new_connect_graph[i][j] = dot_product(i,j)
            
    return new_connect_graph

new_connect_graph = next_generation(connect_graph)

def draw_graph(connect_graph):
    G = nx.DiGraph()
    
    candidate_number = connect_graph.shape[0]
    
    node_name = list(range(candidate_number))
    G.add_nodes_from(node_name)
    
    for i in range(candidate_number):
        for j in range(candidate_number):
            if connect_graph[i][j]:
                G.add_edge(i, j)

    nx.draw(G, with_labels=True)

    plt.show()

draw_graph(new_connect_graph)

Heap sort

class Test():
    def heap_sort(self, nums):
        i, l = 0, len(nums)
        self.nums = nums
        
        # 构造大顶堆,从非叶子节点开始倒序遍历,因此是l//2 -1 就是最后一个非叶子节点
        for i in range(l//2-1, -1, -1): 
            self.build_heap(i, l-1)
        
        print(nums)
        
        # 上面的循环完成了大顶堆的构造,那么就开始把根节点跟末尾节点交换,然后重新调整大顶堆  
        for j in range(l-1, -1, -1):
            nums[0], nums[j] = nums[j], nums[0]
            self.build_heap(0, j-1)

        return nums
    
    def build_heap(self, i, l): 
        """构建大顶堆"""
        nums = self.nums
        left, right = 2*i+1, 2*i+2 ## 左右子节点的下标
        
        large_index = i 
        
        if left <= l and nums[i] < nums[left]:
            large_index = left

        if right <= l and nums[left] < nums[right]:
            large_index = right
 
        # 通过上面跟左右节点比较后,得出三个元素之间较大的下标,如果较大下表不是父节点的下标,说明交换后需要重新调整大顶堆
        if large_index != i:
            nums[i], nums[large_index] = nums[large_index], nums[i]
            self.build_heap(large_index, l)

Is a Balanced Binary Tree

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

This problem is an easy-level at Leetcode. I probably did it more than five times, once and once again. Just like a muscle memory.

However, I found an interesting solution today, which literally changed my mind about Python…

Here is the code:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:        
    def isBalanced(self, root: TreeNode, h = 1) -> bool:
        if not root: return h
        l = self.isBalanced(root.left, h + 1)
        r = self.isBalanced(root.right, h + 1)
        return abs(l - r) <= 1 and max(l, r)

I am very confused at the last line, the max(l, r) part.

I thought that max(l, r) should be converted as a bool value even it returns a integer type value, because as the second component of the operation AND, max(l, r) should represent as a bool variable.

Following by my worst idea, I supposed that the function isBalanced would return either 1 (True) or 0 (False). However, I found a crazy truth after experiments, that Python executor actually return a integer value (the maximum value of l and r) if abs(l – r) <= 1 is matched.

So, it really makes sense. Gain new knowledge of Python 🙂