English translations of selected articles from http://weijr-note.blogspot.com
Sunday, December 22, 2013
Thursday, October 31, 2013
Great Python Challenge
Just like "The Great Giraffe Challenge", let us take the Great Python Challenge.
The answer will be a piece of very short, one-liner like Python code. So it won't take too much of your time.
As usual, if you failed, changed your profile to a python related picture until, say, you find out the answer.
Are you ready?
The answer will be a piece of very short, one-liner like Python code. So it won't take too much of your time.
As usual, if you failed, changed your profile to a python related picture until, say, you find out the answer.
Are you ready?
at
7:34 PM
Wednesday, April 24, 2013
The reflect package of Go 1.1 adds functions SliceOf, ChanOf,and MakeFunc.
With the help of these new functions, we can do things similar to Python's map, list, iterator comprehension, and decorator in Go.
The followings is a simple demonstration of functionality similar to Python's map function and list comprehension.
With the help of these new functions, we can do things similar to Python's map, list, iterator comprehension, and decorator in Go.
The followings is a simple demonstration of functionality similar to Python's map function and list comprehension.
at
10:50 PM
Monday, April 15, 2013
Math Proofs of Google Code Jam Qualification Round 2013 Problems

Problems of Google Code Jam Qualification Round 2013 can be found at http://code.google.com/codejam/contest/2270488/dashboard
1 Problem A. Tic-Tac-Toe-Tomek
No math involved, but complex number is builtin in python and go, so I use
to represent “ “, T, O, X. So if
, then some one won.
2 Problem B. Lawnmower
Theorem.
is a possible pattern if
for every
.
Proof.
Can be easily proved by induction on
, where
.
3 Problem C. Fair and Square
Let
, where
and
. Denote by
the
-th digit of
, then we have
.
Assume
is a palindrome, i.e.,
for every
.
Let
be the coefficient of
of
that is,
Theorem.
is also a palindrome iff
for all
.
Proof.
(
)
Trivial.
(
)
Observe that
for all
. We prove the statement by mathematical induction on
for
.
Case
:
Since
and
it is easy to see that
. So
.
Moreover, we also know that
has
digits and hence
for all
.
Case
:
Suppose
for all
, we want to show
.
By the induction hypothesis, we know that
for all
. So
On the other hand,
Therefore,
, hence
Proof.
.
(
)
for every
by Cauchy-Schwarz inequility.
(
)
Trivial.
In particular, if
, then
.
4 Problem D. Treasure
Let
be the set of all key types. Assume
.
Construct a directed graph
as following:
- iff we starts with at least one key of type .
- If , then iff there is a key of type in a box of type . for every palidrone by Cauchy-Schwarz inequility.
Denote by
the number of boxes of type
and
the total number of keys of type
(including the keys you start with and keys in boxes).
Theorem. All box can be opened iff for every
,
and there is a directed path from
to
in graph
.
Proof.
(
)
If
, then we don’t have enough keys of type
to open all boxes of type
.
If there is no path from
to
, then it means we are unable to get any key of type
.
(
)
We prove this by induction on the number of boxes. Denote by
the number of boxes.
Assume for every
,
and there is a directed path from
to
in graph
.
Case
:
Then all vertices in
are leaves except
and the type of the only box. So there must be an edge from 0 to the type of the box.
Case
:
Suppose the theorem holds for the case with
boxes.
There are at least one
. Consider we open a box of type
(that contains a key of type
if possible). Assume we destroy the key and the box just opened, then we have a setting of
boxes. In this setting, we still have
for all
. Let
be the directed graph of this
-box setting.
If
, then all children of
in
become children of
,
can still reach all other types in
.
Assume
.
If
, then we can open a box of type
that contains a key of type
.
Otherwise, because
, we still hold at least one key of type
after we open a box of type
(and destroy the key).
Either way,
. Therefore, for every child
of
in
, either
1.
(if a key of type
is in the box we just opened), or
2.
and so there is a path from
to
and then to
.
So
can still reach very vertex in
.
By induction hypothesis, the remaining
can also be opened.
By the proof of the theorem, there is a simple algorithm to open all boxes: open any box unless you have just one key of that type and box does not contain a key that can open itself.
However, this algorithm may not yield a "lexicographically smallest" way to open the boxes.
The solve the problem, you should open the box with smallest number such that either this box is the last one of type
or there is still a way to get a type
key.
Document generated by eLyXer 1.2.5 (2013-03-10) on 2013-04-16T11:23:46.409386
at
6:37 AM
Tuesday, March 19, 2013
Py Girl 3rd try
Py Girl impersonate (or perhaps it counts as cosplay) Haruhi Suzumiya.
![]() |
| Anime style. |
![]() |
| Black white manga style. |
![]() |
| Blender renderd and edited in gimp |
at
9:17 AM
Tuesday, March 12, 2013
Project Euler Problem 198
Original post in Chinese: 2008/06/project-euler-198.html
Project Euler Problem 198 asek you to find all ambiguous numbers between 0~0.01 with denominator no more than 10^8, where by definition, a real number x ambiguous, if there is at least one denominator bound m for which x possesses two best rational approximations. For example, let x=9/40 and m=6, then 1/5 and 1/4 are both best rational approximation of x with denominator no more than 6. Clearly, an ambiguous number is necessarily rational.
This seems to be difficult if you don't have certain number theory background knowledge. On the other hand, if you do have the certain knowledge, you may think 10^8 is kind of too small for a Euler project problem. IIRC, you can find a nice algorithm in Knuth's The Art of Computer Programming.
However, it is pretty straightforward to solve it by recursion using Haskell:
With the help of psyco, it is fairly fast, but is not very readable by human and can not be considered as a straightforward interpretation of the mathematics behind the problem.
On the contrary, doing some easy tasks in Haskell is not so elegant. For example, suppose we want to find the integer part of the square root of an integer n, and then print it on the screen. Doing this task in python is as simple as you can imagine.
Sure Haskell can make complicate tasks simple and make simple tasks complicate.
Project Euler Problem 198 asek you to find all ambiguous numbers between 0~0.01 with denominator no more than 10^8, where by definition, a real number x ambiguous, if there is at least one denominator bound m for which x possesses two best rational approximations. For example, let x=9/40 and m=6, then 1/5 and 1/4 are both best rational approximation of x with denominator no more than 6. Clearly, an ambiguous number is necessarily rational.
This seems to be difficult if you don't have certain number theory background knowledge. On the other hand, if you do have the certain knowledge, you may think 10^8 is kind of too small for a Euler project problem. IIRC, you can find a nice algorithm in Knuth's The Art of Computer Programming.
However, it is pretty straightforward to solve it by recursion using Haskell:
As easy as 7 lines of code, yet the code is a pretty straightforward translation of the mathematics behind it. However, using Python to solve it is a totally different story. The above recursion exceeds the limit of numbers of function calls in python. The following is a loop based solution:
f a b l m | a*b>l || (a==1 && b > m) = 0 | otherwise = (f a c l m)+1+(f c b l m) where c=a+b main = putStrLn $ show result where result= (f 1 100 l2 m) + 49+l2-m l = 10^8 l2 = l `div` 2 m = floor (sqrt (fromIntegral l2)
The code is directly copy from the code I gave on Project Euler website, so it does not look good.def p198(M=10**8, z=100, cnt=0): M2=M/2 a=m=int(M2**0.5) stack=range(z,m) while stack: b=stack[-1] if a*b>M2: a=stack.pop() else: cnt+=1 stack.append(a+b) return cnt+M2-z/2
With the help of psyco, it is fairly fast, but is not very readable by human and can not be considered as a straightforward interpretation of the mathematics behind the problem.
On the contrary, doing some easy tasks in Haskell is not so elegant. For example, suppose we want to find the integer part of the square root of an integer n, and then print it on the screen. Doing this task in python is as simple as you can imagine.
print int(x**0.5).To do this in Haskell, you will have to convert the integer n to a float point number, then take square root, then take floor, then covert it to an integer, and then convert it to a text before it is ready to print on screen.
putStrLn.show $ (floor . sqrt) (fromIntegral n)If there are a lot of float point numbers and integers involved, then you probably need to do the coversion every here and there in your code. I know that those conversions exist for a good reason, however, sometimes you just fell like to do a quick and simple computation.
Sure Haskell can make complicate tasks simple and make simple tasks complicate.
at
10:40 PM
Sunday, March 10, 2013
Py Girl, 2nd try
Looks a bit better than the first try, but still not quite there.
It is created using blender + freestyle like before. I also use gimp to auto balance the color. The original blender rendered image can be found below.
The background image features a landscape painting of Fan Kuan.
The .blend file can be downloaded at Blend Swap.
at
9:24 AM
Subscribe to:
Posts (Atom)








