Python and C # basic algorithm implementation comparison

Source: Internet
Author: User

Recently in the study of Python, many of the introductory examples are written again, basically C # and Python are written once again, the comparison found that the language is really connected ah, but the syntax is different.

Python development is also used with the VS, very easy to use, especially the code snippet run, select a python code, ctrl+e+e execution results come out, with installed Testdriven write C # also almost, very good!!! 2016-03-23 22:31:21

python3.4 string Formatting I found the same usage as C #, and then no, the string, whatever it is, writes "{0}+{1}={2}". Format (i,j,k), hey hey heh ...

Join comparison Pit, must be a string array

Python program has Chinese words, in the beginning of the document to encode the gbk,# CODING=GBK

The following is a basic basic algorithm case:

1. Fibonacci numbers

Python

1 deffib (n):2     if(n = = 1)orn = = 2:3         return14     returnFIB (n-1) + fib (n-2)5 Print(FIB (10))6 7 8 deffib (n):9     ifn = = 1:Ten         return[1] One     ifn = = 2: A         return[1, 1] -fibs = [1, 1] -      forIinchRange (2, N): theFibs.append (Fibs[-1] + fibs[-2]) -     returnfibs - Print(FIB (10))

Output results: 55,[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

C#

1  Public intFib (intN)2 {3     if(N <=0)4     {5         returnN;6     }7     if(n = =1|| n = =2)8     {9         return 1;Ten     } One     returnFib (N-1) + FIB (N-2); A } -  Public stringFibarr (intN) - { the     if(N <=0) -     { -         returnn.tostring (); -     } +     if(n = =1|| n = =2) -     { +         return " the"; A     } at  -list<int> fiblist =Newlist<int> () {1,1 }; -      for(inti =2; I < n; i++) -     { -Fiblist.add (Fiblist[i-1] + fiblist[i-2]); -     } in     return string. Join (",", fiblist); - } to  + voidTest () - { theConsole.WriteLine (Fib (Ten)); *Console.WriteLine (Fibarr (Ten)); $}

Output Result:

55
1,1,2,3,5,8,13,21,34,55

2. Number of daffodils

Python

1   for  in range (100,1000):2     i = x/1003      j = x% 100/104      k = x% 10
   
    5      
    if(i * * 3 + J * * 3 + k * * 3 = =
     x):
    6          
    print(x)
   

C#

1  for(inti = -; I < +; i++)2 {3     inth = I/ -;4     intt = i% -/Ten;5     ints = i%Ten;6     if(Math.pow (H,3) + Math.pow (t,3) + Math.pow (s,3) ==i)7     {8 Console.WriteLine (i);9     }Ten}

3. Factoring

Python

Example: 12=2*2*3

1 #CODING=GBK2 3n = Int (input ("Enter Number:"))4 Print("n={0}". Format (n))5s =""6  forXinchRange (2,n + 1):7     whileX! =N:8        if(n% x = =0):9s+="{0}". Format (x)Tens+="*" Onen = n/x A        Else: -              Break -s+="{0}". Format (int (n)) the Print(s)

4. Calculating primes

Python

1Count =02Pmarr = []3ISPM =True4 5  fromMathImportsqrt6  forXinchRange (101,201):7K =Int (sqrt (x))8      forIinchRange (2,k + 1):9         ifX% i = =0:TenISPM =False One              Break AISPM =True -     if(ISPM): -Pmarr.append ("%d"%x) theCount+=1 - Print(Pmarr) - Print(count)

C#

1 BOOLISPM =true;2 intCount =0;3  for(inti = -; I < $; i++)4 {5     intj = (int) math.sqrt (i) +1;6      for(intK =2; K < J; k++)7     {8         if(i% k = =0)9         {TenISPM =false; One              Break; A         } -ISPM =true; -     } the     if(ISPM) -     { -Console.Write ("{0}", i); -count++; +     } - } +Console.WriteLine ("total: {0}", count);

Python may not perform as efficiently or linguistically as C #, but dynamic language features and cross-platform aspects are indeed much better than C #.

In the crawler development efficiency is really fast, is looking at the Python crawler algorithm, ready to write a version of C # out

The habit of learning to take notes has been lost for a long time, hoping to pick up slowly later.

Python and C # basic algorithm implementation comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.