Python learning algorithm and python Learning Algorithm

Source: Internet
Author: User

Python learning algorithm and python Learning Algorithm

I am a loyal fan of Microsoft. I have been in the C # language,. net platform, and asp.net framework since I entered the codenong industry. When I came home from my New Year's home, my younger siblings had been promoted to junior high school, but I still wanted to ask them to learn a programming language, so I didn't want to bring them into the trap. They always wanted me to be their teacher and teach them to learn. Ah, it seems that I have brought myself into the trap, but I agreed for their learning path. I finally decided to teach them the latest popular programming languages and some policy information to learn Python, just as I changed my mind and learned the language that coders have always admired.

In fact, learning also requires a certain degree of purpose. For a programmer, it will further improve his own programming technology (my personal thoughts ). Recently I am studying algorithms to give myself a daily class of algorithms. So I use python to implement various algorithms, so that I can learn this language and achieve my goal.

Merge Sorting: This is my first algorithm question today. Principle:

The basic idea of the merge algorithm is to solve the problem based on the partitioning method. 1. decomposition: splits the sequence whose length is n into n/2. 2. Governance: Calls Merge Sorting for each subsequence to perform recursive operations. When the sub-sequence length is 1, the sequence itself is ordered and recursion is stopped. 3. Merge: merge each sorted subsequence.

The most important thing is code.

1 class MergeAlgorithm: 2 # merge two ordered sequences. 3 def mergearray (self, first, last): 4 temp = [] 5 I = 0 6 j = 0 7 8 while j <len (first) and I <len (last): 9 if first [j] <last [I]: 10 temp. append (first [j]) 11 j ++ = 112 else: 13 temp. append (last [I]) 14 I + = 115 16 if j = len (first): 17 for h in last [I:]: 18 temp. append (h) 19 else: 20 for h in first [j:]: 21 temp. append (h) 22 23 return temp24 25 def mergesort (self, lists): 26 if len (lists) <= 1:27 return lists28 mid = int (len (lists)/2) 29 left = mergesort (lists [0: mid]) 30 right = mergesort (lists [mid: len (lists)]) 31 return mergearray (left, right)

This method is to define the class according to the C # method and then call the class, but there is a problem.

 

What a ghost cannot even use his own method? Well, it seems that I am in a deep way of thinking. When we look at the function defined above, we find that there is a self in the parameter, which is automatically displayed by using the IDE pycharm, I was very depressed and finally checked the intention of this keyword. "self represents the instance of the class, representing the address of the current object, and self. class indicates the class. For more information, see http://www.runoob.com/python/python-object.html. I haven't understood this low-IQ codenon for a long time. I added "MergeAlgorithm." in front of the method based on the reported error, but I clicked "run" again,

If you try again to find out the reason, you still cannot solve the problem. Finally, return to the sentence "self represents the class instance, representing the address of the current object, and self. class points to class, so the class is changed to "self. ", run again, oh, my day is actually successful. And output correct results

I 've been suffering from such a thing for a long time. It seems that I am still a code farmer. The road ahead is still under further research, and it is a huge project to teach children. If you accidentally miss your children, you must be careful and conscientious, just as we treat every job.

 

 

Related Article

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.