Comparison between Python and C # Basic Algorithms,
Recently I have been studying python, and I have written many examples again. Basically, C # And Python have both been written. The comparison shows that the language is really the same, but the syntax is different.
Python is also used for development. It is very useful, especially for Running code segments. After selecting a piece of python code, the result of Ctrl + E is displayed, which is similar to writing C # With TestDriven installed, great !!! 22:31:21
I found the same usage as C # In string formatting of python3.4, and then there is no more. No matter what the string is, it writes "{0} + {1 }={ 2 }". format (I, j, k), ......
Join comparison pitfall, must be a String Array
If the python program contains Chinese characters, mark the file as gbk, # coding = gbk
The following is a basic algorithm example:
1. Fibonacci count
Python
1 def fib(n): 2 if(n == 1) or n == 2: 3 return 1 4 return fib(n - 1) + fib(n - 2) 5 print(fib(10)) 6 7 8 def fib(n): 9 if n == 1:10 return [1]11 if n == 2:12 return [1, 1]13 fibs = [1, 1]14 for i in range(2, n):15 fibs.append(fibs[-1] + fibs[-2])16 return fibs17 print(fib(10))
Output result: 55, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
C #
1 public int Fib(int n) 2 { 3 if (n <= 0) 4 { 5 return n; 6 } 7 if (n == 1 || n == 2) 8 { 9 return 1;10 }11 return Fib(n - 1) + Fib(n - 2);12 }13 public string FibArr(int n)14 {15 if (n <= 0)16 {17 return n.ToString();18 }19 if (n == 1 || n == 2)20 {21 return "1,1";22 }23 24 List<int> fibList = new List<int>() { 1, 1 };25 for (int i = 2; i < n; i++)26 {27 fibList.Add(fibList[i - 1] + fibList[i - 2]);28 }29 return string.Join(",", fibList);30 }31 32 void Test()33 {34 Console.WriteLine(Fib(10));35 Console.WriteLine(FibArr(10));36 }
Output result:
55
2. daffodils
Python
1 for x in range(100,1000):2 i = x / 1003 j = x % 100 / 104 k = x % 105 if(i ** 3 + j ** 3 + k ** 3 == x):6 print(x)
C #
1 for (int i = 100; i < 1000; i++) 2 { 3 int h = i / 100; 4 int t = i % 100 / 10; 5 int s = i % 10; 6 if (Math.Pow(h, 3) + Math.Pow(t, 3) + Math.Pow(s, 3) == i) 7 { 8 Console.WriteLine(i); 9 }10 }
3. factorization
Python
Example: 12 = 2*2*3
1 # coding = gbk 2 3 n = int (input ("input number:") 4 print ("n = {0 }". format (n) 5 s = "" 6 for x in range (2, n + 1): 7 while x! = N: 8 if (n % x = 0): 9 s + = "{0 }". format (x) 10 s + = "*" 11 n = n/x12 else: 13 break14 s + = "{0 }". format (int (n) 15 print (s)
4. Evaluate the prime number
Python
1 count = 0 2 pmarr = [] 3 ispm = True 4 5 from math import sqrt 6 for x in range(101,201): 7 k = int(sqrt(x)) 8 for i in range(2,k + 1): 9 if x % i == 0:10 ispm = False11 break12 ispm = True13 if(ispm):14 pmarr.append("%d" % x)15 count+=116 print(pmarr)17 print(count)
C #
1 bool ispm = true; 2 int count = 0; 3 for (int I = 100; I <200; I ++) 4 {5 int j = (int) Math. sqrt (I) + 1; 6 for (int k = 2; k <j; k ++) 7 {8 if (I % k = 0) 9 {10 ispm = false; 11 break; 12} 13 ispm = true; 14} 15 if (ispm) 16 {17 Console. write ("{0}", I); 18 count ++; 19} 20} 21 Console. writeLine ("Total: {0}", count );
Python execution efficiency or language features may not be as good as C #, but dynamic language features and cross-platform features are indeed much better than C.
The crawler development efficiency is indeed very fast. Currently, we are looking at the python crawler algorithm and preparing to write a version of C #.
The habit of taking notes has been lost for a long time. I hope I can pick it up later.