Getting any programming language necessary for a few exercises

  Copyright Notice: This article blogger windows (Colin Cai) original, welcome to reprint. To repost, must indicate the original URL 

  http://www.cnblogs.com/Colin-Cai/p/11073938.html 

  Author: windows 

  QQ / micro letter: 6,679,072 

  E-mail: [email protected]

  Whenever learning a computer language, we have to do some exercises in order to become familiar with. With the course of our support for this programming language itself abstract means of understanding, the following problems, which can be completed in the course of almost every programming language learning, these languages ​​may include, but are not limited to, C, C ++, Shell, awk, Python, JavaScript, Java, Scala, Ruby, Lisp (Common Lisp, Scheme, Clojure), Prolog, Haskell and so on.

  

  Tower of Hanoi (Hanoi Tower)

 test

  

  Tower of Hanoi has three columns, beginning in ascending order all the n disks, each disk may be moved in a first pillar, and only small cap on top of the tape. Q. How can I put these discs to move from the first pillar second pillar.

  This is basically all examples of learning the language when learning recursion is bound to contact, to achieve this, it is basically the study of recursive languages ​​have a preliminary understanding.

  We can issue as a Hanoi (l-> 2, 3, n) , the n-th symbol read as the disc is moved from column 1 to the column number 2, number 3 is the remainder of a column by column.

  It is easy to break the big problem down into three small problems:

  Hanoi (1-> 2, 3, n)  =>  Hanoi (1-> 3, 2, n-1) ,  Hanoi (1-> 2, 3, 1) ,  Hanoi (3-> 2,1, n- 1)

  I.e. the uppermost first disc from the n-1 No. 3 No. 1 is moved to a column by column, then the largest tray number No. 2 is moved to a column by column, and finally moving the tray number of n-1 from the column 3 to the most column No. 2,

  So it reached the effect of recursion.

 

  Factoring / integral coefficient of Polynomial (factorization)

  

  Factorization is input into a product of individual positive integer prime number, for example:

  $300 = 2^{2}\times{3}\times{5^{2}}$

  Under ordinary circumstances the factorization algorithm is not complicated, just a simple elementary number theory to prove.

 

  The factoring polynomials with integer coefficients may even many complex than the above, such as:

  $2x^{6}+7x^{5}+13x^{4}+15x^{3}+11x^{2}+5x+1 = (x^{2}+x+1)^{2}\times(2x^{2}+3x+1)$

  Whether this is a process-oriented or object-oriented or functional programming are all worth doing a do, if you can, you can also try to break down in the attempt Galois field polynomial ring.

 

  Prime table (prime number list)

 

  Prime number is a suitable table, you can use several methods.

  The simplest, we can turn every number from 2 start determination, for each of the number N 2 ~ N-1 is determined whether or submultiples thereof, wherein if it is not a divisor, for the prime number.

  Of course, this can improve efficiency, we know that for any positive integer, if it is a composite number, then there exists an integer divisor is less than its square root. We then determined from 2 ~ N-1 reduced to $ 2 \ widetilde \ sqrt {N} $.

  Beyond that further, we find a prime number $ 1 ~ N $ can be attributed to seek $ 1 \ widetilde \ sqrt (N) $ primes. So, which we can introduce a recursion.

  In addition, there are various types of screening method is no longer go into detail, free to google.

  So the above may be familiar with the programming language you learned from all angles.

 

  Arrangement / combination (permutation and combination)

 

  A combination of mathematical knowledge should have been learned in high school, we launched a permutation / combination of the world through the principle of addition and multiplication principle (the principle of multiplication is in fact launched by the addition principle).

  Here, we can try to write all permutations / combinations of a set.

  For example {1,2,3} $ $ $ arranged all {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3, 1,2}, {3,2,1} $, all combinations of the two elements has $ {1,2}, {1,3}, {2,3} $.

  

  There are many ways to achieve all the permutations of a set of output:

  First of all, many languages ​​are related to the library supports permutations and combinations, such as Python's itertools library, a lot of time to write a formal program or directly with the library.

  For example $ {1, ... n-1} $ all arranged to $ {1, ... n} $ presence of all permutations of a recursive combination similarly.

  Moreover, we can turn the output of arrangement / combination needed a dictionary arrangement, the arrangement is just a little under a slightly larger / combinations can be slightly tricky to find how.

  Then, we can also use the number of each of permutation / combination correspondence, theoretically corresponding to various methods.

  Even, we can turn the output of all permutations / combinations based on the exchange, of course, here requires some knowledge of abstract algebra.

 

  In short, we have various implementations arrangement / combination.

 

  Game of Life (Conway's game of life)

 

  1970 Game of Life Conway's invention.

  MxN Ituri, all the cabinets come with a state, living / dead.

  Each time there is a state transition FIG whole, must look at each grid lattice student number around 8 / death.

  All next-generation grid status is determined by the following rules:

  1. If the number of lives around the grid is less than 2, then the next state of this box for inanimate (interpreted as too alone).

  2. If the number of lives around the grid of more than 3, the next generation of the grid for the inanimate (interpreted as too much life around, powerful resource consumption).

  3. If the number of lives around the grid equal to 2, the next generation of the grid state continues to maintain the current status.

  4. If the number of lives around the lattice is equal to 3, the state of generation of the grid life.

 

  24点(Count 24 points)

 

  When we were basically played 24 points, four cards is calculated using the arithmetic 24.

  This implementation is a bit of a challenge with the program, we consider how to traverse all possible, then turn calculated, equal to 24 to see if, in addition, we might also consider the score, such as the following classic title 5,5,5,1, calculation method It is (5-1 / 5) * 5.

  

  再者,我们要按照平常的使用习惯,考虑把多余的括号去掉,比如((a*b)-c)/d其实应该是(a*b-c)/d。

  另外,我们要考虑是否有结构等价,比如a*b+c*d和d*c+b*a,我们如何判断并只保留一种。

  

  以上面的为例,可以有很多答案,比如2*8+3*3, 3*3+(8*2), 3*8*(3-2), (3-2)*(8*3), (3*8)/(3-2),(2+3/3)*8, (3/3+2)*8...但我们只考虑计算结构的唯一以及去掉多余括号,合理的只剩下四个答案:2*8+3*33*8*(3-2)3*8/(3-2)(2+3/3)*8

  当然,我们还要考虑更多的牌,更多的运算来计算任意数字。总之,24点这个问题或许不是那么容易,在某些语言下的实现尤其有技巧性。

 

  自输出程序(Quine)

 

  解释一下,所谓自输出程序(Quine),就是程序的输出和程序的代码一模一样,直接用哲学家Quine命名。

  这样的程序也需要写?怎么感觉是在学习写病毒呢?

  病毒的确可能需要自输出这样的技术,但是技术这个东西本身就是双刃剑,手术刀是用来救人的,但它依然可以拿来当凶器。

  每一种编程语言只要是图灵等价的(当然,其实这个条件很基本),就可以通过不动点存在定理推出Quine是一定存在。记载中,上世纪60年代诞生了第一个Quine,用Atlas Autocode编写。

  对于Scheme,可能的最短的Quine如下:

  ((lambda (x) `(,x ',x)) '(lambda (x) `(,x ',x)))

 

  标准库的部分实现

 

  Thinking achieve some standard library of language learning, but also improve an important means. For example, C ++, STL, when we can learn C ++ STL may be to think of how to achieve, this is very helpful to the object-oriented C ++, generic (by using the mask) understanding.

  And, very often achieve the same semantics library has a variety of implementations, we can consider a variety of different implementations. Scheme example, a data process is completely mixed with the language, there are many different functions substantially achieve very exaggerated.

  If the Scheme, Common Lisp, Clojure which several Lisp has learned, can also be combined together, compared to the school, I think the other is how to achieve. Several Lisp, after all, brotherhood, there is a great similarity, this can be extended to a similar or even between different languages ​​in the same programming paradigm, they still have many places to communicate, which can be associated with contrast. For example, two kinds of design from the outset directed multi-paradigm away support JavaScript, Python, and many other languages ​​can resonate, we will try to think about how other languages ​​are implemented in the realization of certain libraries.

 

  Conclusion

 

  Computer language learning is always gradual, in short, in line with more thinking, more contrast, never let the newly acquired knowledge of the formation of knowledge silos, and let all the knowledge closely linked to each other, this will continue to progress, and more inspired creation.

Guess you like

Origin www.cnblogs.com/Colin-Cai/p/11073938.html