Latest stories

Binary Search Trees | 5kyu

要点

  • __str__() 的几种定义方法:如 "%s" % value'{}'.format()
  • A and B and C
  • ifelse 单行语句
  • forin 单行语句
  • join() 函数
  • str() 函数与 __str__() 方法

Details

A Tree consists of a root, which is of type Node, and possibly a left subtree of type Tree and possibly a right subtree of type Tree. If the left subtree is present, then all its nodes are less than the parent tree’s root and if the right tree is present, then all its nodes are greater than the parent tree’s root. In this kata, classes Tree and Node have been provided. However, the methods __eq__, __ne__, and __str__ are missing from the Tree class. Your job is to provide the implementation of these methods. The example test cases should provide enough information to implement these methods correctly.

As an illustrative example, here is the string representation of a tree that has two nodes, ‘B’ at the root and ‘C’ at the root of the right subtree. The left subtree is missing and the right subtree is a leaf, i.e., has no subtrees:

'[_ B [C]]'

This tree is obtained by evaluating the following expression:

Tree(Node('B'), None, Tree(Node('C'))))

Notice in particular that when one subtree, but not both, is missing, an underscore is in its place, a single space separates the root node from the subtrees, and when both subtrees are missing, the root node is enclosed in brackets.

Born to be chained | 4kyu

  • 对于 Object 的遍历;

Details

Function composition is a powerful technique. For example:

function sum(x, y) {
  return x + y;
}

function double(x) {
  return sum(x, x);
}

function minus (x, y) {
  return x - y;
}

function addOne(x) {
  return sum(x, 1);
}

double(sum(2, 3)); // 10

But in complex expressions, composition may be difficult to understand. For example:

double(double(addOne(sum(7, minus(sum(5, sum(4, 5)), 4))))); // 72

In this kata, we will implement a function that allows us to perform this by applying a fluid style:

c.sum(4, 5).sum(5).minus(4).sum(7).addOne().double().double().execute(); // 72

Your job is implement the chain function:

function chain(fns) {
}

var c = chain({sum: sum, minus: minus, double: double, addOne: addOne});

As you can see, this function receives the methods to be chained and returns an object that allows you to call the chained methods. The result is obtained by calling the executemethod.

Chained functions receive an arbitrary number of arguments. The first function in the chain receives all its arguments. In the other functions, the first argument is the result of the previous function and then it only receives the remainder arguments (second, third, etc.). The tests always pass the appropriate arguments and you do not have to worry about checking this.

Note that the chain can be reused (the internal state is not stored):

象棋 (Xiangqi/Chinese Chess) Board Validator | 4kyu

Details

Your friend recently has shown you some chess puzzles he found somewhere. It’s not your ordinary chess: but the mysterious, distinct relative of chess, the legendary 象棋, also known as Xiangqi/Chinese chess! As a Chinese, you know that your friend doesn’t know anything in Chinese besides recognizing a few sigils (for example, he doesn’t know what is 七步擒士 or 雙馬飲泉), so it’s probably likely that the “puzzles” he got is actually bogus, and the pieces are not placed properly. However, you don’t want to actually teach him Xiangqi either (yet), so the best tactic you could come up with is to write a validator program and let your friend use it instead of using you.


You will be given an ASCII board. Xiangqi board is 9 tiles wide and 10 tiles high, with the pieces placed as follows:

Example (the starting Xiangqi board):

車馬象士將士象馬車
   |X|   
 砲 +-+ 砲 
卒 卒 卒 卒 卒
---------
---------
兵 兵 兵 兵 兵
 炮 +-+ 炮 
   |X|   
俥傌相仕帥仕相傌俥

The bottom and top half corresponds to red and black pieces, respectively.
Note that red and black have different characters to distinguish the same piece of both sides.

Your function, chessValidator, should determine whether the pieces in argument board are placed legally. Unlike the chess you’re familiar with, there are restrictions as to where the various chess pieces can be at:

Argument Mapper | 5kyu

Details

As part of a broader functionality you need to develop an argument mapper.

The function receives a function object as first parameter and an unknown number of arguments [zero to many]. You have to return an associative array that maps the name of an argument and its related value.

The usage is:

function func1(arg1, arg2) { ... }

var map = createArgumentMap(func1,'valueOfArg1', 'valueOfArg2');
console.log(map['arg1']);  // writes 'valueOfArg1'
console.log(map['arg2']);  // writes 'valueOfArg2'

The passed values are in the same order as they appear in the function object.

Invalid inputs, e.g. non-function objects, or wrong number of arguments, are not considered.

Hajime!

Default Arguments | 4kyu

Details

Write a function defaultArguments. It takes a function as an argument, along with an object containing default values for that function’s arguments, and returns another function which defaults to the right values.

You cannot assume that the function’s arguments have any particular names.

You should be able to call defaultArguments repeatedly to change the defaults.

function add(a,b) { return a+b;};

var add_ = defaultArguments(add,{b:9});
add_(10); // returns 19
add_(10,7); // returns 17
add_(); // returns NaN

add_ = defaultArguments(add_,{b:3, a:2});
add_(10); // returns 13 now
add_(); // returns 5

add_ = defaultArguments(add_,{c:3}); // doesn't do anything, since c isn't an argument
add_(10); // returns NaN
add_(10,10); // returns 20

HINT: This problem requires using Fuction.prototype.toString() in order to extract a function’s argument list

ES5 Generators(i) 和闭包 closure

Kata

Description:

This is the first part of three (part2, part3).

Generators and Iterators are new ES6 features that will allow things like this:

function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;
    }
}

Using them in this way, we can do amazing things:

let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8

This is powerful, but until a few months later, ES6 will not be born.

The goal of this kata is to implement pseudo-generators with ES5.

Wrapped Function

Kata

Create a function method that allow you to wrap an existing function. The method signature would look something like this:

Usage Example:

function speak(name){
   return "Hello " + name;
}

speak = speak.wrap(function(original, yourName, myName){
   greeting = original(yourName);
   return greeting + ", my name is " + myName;
})

var greeting = speak("Mary", "Kate");

Catching Car Mileage Numbers

Kata

“7777…8?!??!“, exclaimed Bob, “I missed it again! Argh!” Every time there’s an interesting number coming up, he notices and then promptly forgets. Who doesn’t like catching those one-off interesting mileage numbers?

Let’s make it so Bob never misses another interesting number. We’ve hacked into his car’s computer, and we have a box hooked up that reads mileage numbers. We’ve got a box glued to his dash that lights up yellow or green depending on whether it receives a 1 or a 2 (respectively).

It’s up to you, intrepid warrior, to glue the parts together. Write the function that parses the mileage number input, and returns a 2 if the number is “interesting” (see below), a 1 if an interesting number occurs within the next two miles, or a 0 if the number is not interesting.

Hexo 部署到新电脑

换了新的电脑,想把在 hexo 上的部署文件转移到新电脑上。因为提前在 github 为此做了一些工作,就是按照网上一些朋友的建议,在 github 上创建两个分支,一个分支 「master」 用于 hexo 部署,另一个分支「hexo」用于存储源文件。这样当需要在一台新电脑上使用 hexo 时,只需要把源文件 clone 到本地就行了。但事情总不是一帆风顺😅。今天遇到的问题是,把源文件拉到本地以后,每次执行 hexo d 部署, 都会把所有源文件都部署到 master 分支上。按照常理,应该是只有 public 文件夹里的内容才会放在 master 分支下的。

「骑着白马入地狱,叼着纸烟进天堂」

  • 這一年在德國的生活回顧
  • 『孤獨指南』
  • 直播節目,心理療傷,自我慰藉
  • 語言學習的瓶頸
  • 當學習少了儀式感

畢業一年

來之前,沒曾想到自己會在德語考試上停留這麼久。

二〇一六年九月到二〇一七年九月。

二〇一六年九月底到德國,處理很多事情,落戶,銀行卡,房屋合同,保險,找房,還有房屋的衛生問題,自己又盲目的報名了十一月份的考試,當時由於整個大四下忙於畢設,疏於德語的學習,等我到德國時,德語退步很大,加上剛來的不適應,十一月考試結果特別差。然後十二月找房,一月份搬入新家。二月份的時候又考了一次,結果較上次有很大進步,到三月份,自己在 Tandem 的六個月語言課程結束,然後是面臨四月的考試,那時的狀態也是最焦慮,最混亂的,當時覺得如果考試考不過就全完了,巨大的壓力和焦慮也導致了考前複習的極度混亂,甚至一度的通過上網來麻痺自己,緩解焦慮,清楚的記得考前的一晚都還在看著 BiliBili 網站的視頻,想要逃避現實。四月底的那次考試考完,反倒讓我開始反思自己的學習模式,然後是五月份自己開始了極度自律的一個月,每週統計,效率上很高,直到六月二號開始,黃同學的到來,我那極度自律的狀態也隨即結束了。從六月二號到六月十四號又回到考前的極度緊張和焦慮,加之考前一天得知四月的考試結果,頂着巨大的壓力和焦慮又去考了六月那一場。雖然五月份自己極度的自律地在複習,但複習方向以聽力,口語為主,沒有很集中的背單詞和準備句式,考試最終還是在寫作上吃了虧。

分类

标签