COCO Programming (1) - Take off from Python

zhaozj2021-02-11  216

Coco is a smart child, and it is also a computer software professional, so she said that I am very happy when she will teach her to write the program. After teaching a section, I feel really earned a smart student.

Hydon once said, "I thought it would be proud of teaching this young people." Later, this young people named Beethoven really made a big business.

However, before COCO, before this, almost never touched the real computer, plus I can only teach her through the Internet, so I still have some jokes. Of course, she learned how much than I was.

I have opened a very category study plan for COCO: C & python. In the early days, Python is mainly, slowly plus C , then look at it, then add some Java (I always feel that I have to learn in front of it, Java, C #, what kind of self-study can also get up very quickly). Of course, I am most familiar with the database system, so I will definitely teach her knowledge of the database. Temporarily, learn this before you learn. It is mainly to develop the style of design and encoding, the foundation. I am not her class teacher after all, many lessons will be better in class.

COCO's English is good, so after loading the Python kernel and Pythonwin (I am the Python IDE), I gave her a week to read Python's documentation, self-study Python's basic knowledge. As a school student who only studied C language in the textbook, her first week's progress is still very satisfied. In addition to individual use problems, I need my help, and I don't have anything else. Now the most basic syntax is fully grasped, and also know some simple features of Pythonwin. The old birds don't laugh, for a student who has just had your own computer, this progress is not slow. Now, I am going to let her write some small programs to practice. Just start from her compulsory class - "Algorithm and Data Structure". Anyway, Python has ready-made containers, and the list dictionary is available, and it is easier to start from the foundation algorithm. So, today's QQ, we start from sort:

I: Coco, you are now learning the algorithm.

Coco: Yes, but the textbook is C , the teacher is all C.

I: For this course, what language is not the most important - actually very important, I personally still feel that C is best:) - What is the most achieved. Look at the C program you sent to me last time ... I think you still start from Python.

Coco: Why? Isn't that program not running?

I: Yes, but even indentation, this kind of code, I will not be able to see it at all, don't say debugging and improvement. Practice with Python, forced you to learn a good writing style again. Then what pointers are referenced, if they are stirring with the algorithm, they can't learn more. Python's syntax is simple, don't worry.

COCO: So I said that I have no place to be ...

I: I don't, the following lines can also be retained:

#include

int main ()

{

Return 0;

}

Coco: 555 ......

(Coco is anger, breaking the course for five minutes, ^ _ ^)

I: Ok, now we start class, first, you have to know the linear container of Python - Link. Coco: Well, I know, the easiest way, array = [] can declare a linked list. It can also be assigned to it here.

Me: OK, then with this container, we can start learning the most basic sort algorithm. How do you sign it if you give you some poker?

COCO: Of course, the simplest way is to take a first on the desktop, then take out the second, put it in a suitable location. Then the third, fourth ... The situation is inserted into a suitable location in the queue in a certain end or queue.

I: I am completely correct, we have a similar way to the data, we call it directly inserted.

Coco: Direct insertion sorting seems to be such a process, first putting this data all here, then one of the order is not right, and insert them into the appropriate location.

I: Yes, we use this method in the program, now give you this list, Array = [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 18, 0 13, 3, 17, 12, 2, 8, 7], we write the function code to which it is sorted. Let's take a statement of this function, just call it DRTINSSORT (THEARRY), give the test code:

Array = [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 18, 0, 13, 3, 17, 12, 2, 8, 7]

Print Array

DRTINSSORT (ARRAY)

Print Array

The first step, let's find out which positions have appeared in reverse, can you write this program?

COCO: I try ...

Def DrtinsSort (THEARRY):

For i in range (1, len (thea "):

IF THEARRAY [I]

Print "I:", I, "THEARRAY [I-1]:", "THEARRAY [i]:", THEARRAY [i]

Return

I: Very good, this program is good, the code is also very clear, if you want to turn it into code that can be sorted to array, do you know how to improve?

Coco: You should change the "print" line into the code of the mobile element, let me write, this code should be changed ...

Def DrtinsSort (THEARRY):

For i in range (1, len (thea "):

IF THEARRAY [I]

TMP = THEARRAY [i]

Del

THEARRAY [i]

For J in Range (i):

IF THEARRAY [J]> TMP:

THEARRAY.INSERT (J, TMP)

Break

Return

Me: Very good, I noticed that in your code, find the loop of the insertion point is for J in Range (i), can you talk about why is for J in in (Len (THEARRY)? Also, you can remember the code of the different function blocks next time, it is best to write a comment or separately separately, improve readability :).

Coco: This is because the code of the search reversed point is starting from the starting point of the linked list. If I-1 and I are in reverse order, it means that the previous elements have been processed, which is already a good order. However, the elements greater than I have not been compared yet, and cannot be guaranteed. Therefore, we should only find an insertion point in the determined proceeds. I: Yes, then do you talk about this algorithm Is there any improvement place?

COCO: The teacher is mentioned when the search algorithm is mentioned. The time complexity of this sequential search is linear, then the time used by the search insertion point will become longer as the time grows, the time used in the search insertion point will grow. If you change to a more efficient search algorithm, such as a ... you can find it.

I: I have a good idea, then when you improve it, there are several places to pay attention:

1. Your previous code is all in a function, never divided into a child, this is not a habit. The insertion code in the sort can be clearly separated from the entire sort, and there may be a variety of combined schemes, and the code to be inserted into a sub-function should be separated;

2. According to the theoretical demonstration on the book, the folded semi-insert is always recursive. However, in this case, it is necessary to judge the boundary conditions at every time, it is more troublesome, but it will reduce efficiency, so practical practices are to use sequential search directly when search is reduced to a certain extent. So, your order is still useful (but you should already understand this);

3, in order to adjust the performance, I hope that the critical length of the sequential search for the sequential search, that is, the smallest length of the 50-semi-search, which should be convenient. This way, you do it as one of the parameters of the search function, so that I am also convenient.

Coco: Ok, I will try it (ask so much @ _ @) ......

(COCO has been multi-twist, finally took out such a version)

#Direct insert

Def DRTIS (THEARRAY, N, E):

For J in Range (N):

IF THEARRAY [J]> E:

THEARRAY.INSERT (J, E)

Break

Return

#Half search.

DEF HLFINS (THEARRAY, LOW, TOP, E, LMT):

IF (Top - Low)

DRTIS (THEARRAY, TOP, E)

Return

#This variable is the marker insert point.

INSP = (Low TOP) / 2

IF THEARRAY [INSP-1]

THEARRAY.INSERT (INSP, E)

Return

Elif E

HLFINSRE (THEARRAY, LOW, INSP, E, LMT)

Return

Elif THEARRAY [INSP]

HLFINSRE (THEARRAY, INSP, TOP, E, LMT)

Return

Else:

Return

# Direct Sort Method.

Def DrtinsSort (THEARRY):

#The variable tell the Half Serach Insert MedoTh

# uSE HALF SERACH IN How long area.

Lowlmt = 8

For i in range (1, len (thea "):

IF THEARRAY [I]

TMP = THEARRAY [I] DEL

THEARRAY [i]

# C kice a medoth you like in ipen ity The direct insert mott.

HLFins (THEARRAY, 0, I, TMP, LOWLMT)

Return

I: Yes, it is worth encouraging! I want to ask, why your order search insert function DRTINS wants to declare to DEF DRTIS (THEARRAY, N, E)? In this way, every time you search, you will start from THEARRAY [0], our purpose can be basically not reached.

Coco: I understand, you should improve DRTISS, let it start to adjust ... this will be changed ...

I: Also, you wrote a recursive algorithm now, and the recursive is of course its benefits, such as the code is simple. But if the number of recursives is too much, it will occupy a lot of resources, as well as the stack overflow, now in addition to the recursive algorithm, you have to write an iterative version.

Coco: Good (tired) ......

(The following is the final code of COCO)

#Direct inert to the list. If it is just directly buy in the

#sort, you can use the sample version:

#def drtins (THEARRAY, TOP, E):

# For j in inregation:

# ...

Def DRTIS (THEARRAY, LOW, TOP, E):

For J in Range (Low, TOP):

IF THEARRAY [J]> E:

THEARRAY.INSERT (J, E)

Break

Return

#Half search insert method, it is the recursively version.

DEF HLFINSRE (THEARRAY, LOW, TOP, E, LMT):

IF (Top - Low)

DRTIS (THEARRAY, LOW, TOP, E)

Return

#This variable is the marker insert point.

INSP = (Low TOP) / 2

IF THEARRAY [INSP-1]

THEARRAY.INSERT (INSP, E)

Return

Elif E

HLFINSRE (THEARRAY, LOW, INSP, E, LMT)

Return

Elif THEARRAY [INSP]

HLFINSRE (THEARRAY, INSP, TOP, E, LMT)

Return

Else:

Return

#HALF Search Insert Method, It is The ITERATIVE VERSION

DEF HLFINSIT (THEARRAY, LOW, TOP, E, LMT):

IF (Top - Low)

DRTIS (THEARRAY, LOW, TOP, E)

Return

ilow = low

ITOP = TOP

#This variable is the mark of insert point.

INSP = (iLow ITOP) / 2

While (E E): IF E

ITOP = INSP

Else:

ilow = INSP

IF ITOP - ILOW

DRTIS (THEARRAY, LOW, TOP, E)

Return

INSP = (iLow ITOP) / 2

Else:

THEARRAY.INSERT (INSP, E)

Return

# Direct Sort Method.

Def DrtinsSort (THEARRY):

#The variable tell the half search insert Medo

# uSE HALF SEARCH IN How Long Area.

Lowlmt = 8

For i in range (1, len (thea "):

IF THEARRAY [I]

TMP = THEARRAY [i]

Del

THEARRAY [i]

#You can Choice a Medoth you like in ity

#or recursively, Even Choice The Direct Insert Mod.

HLFinsit (THEARRAY, 0, I, TMP, LOWLMT)

Return

#Follow is the demo of direct sort.

Array = [6, 16, 10, 9, 15, 5, 11, 1, 19, 4, 14, 18, 0, 13, 3, 17, 12, 2, 8, 7]

Print Array

DRTINSSORT (ARRAY)

Print Array

I: Ok, good, beautiful.

COCO: I have a question, we do what we do here is the thearay parameters for functions, why will the results will be directly reflected in Array?

I: The so-called "transmitted value parameters" and "reference parameters", you have heard of it.

Coco: Know.

Me: In Python, simple type data, such as integer, real numbers, in function calls, are all called, and more complex types, such as linked lists, are reference calls, this is for efficiency considerations. .

Coco: understand ...

I: You have learned very quickly today, not bad. This way, in order to express encouragement, you will take a taxi right away, I invite you to eat.

Coco: It's better to do it.

PS: The procedure for several years, always feel new, from the entrance to the comprehension, is a very difficult process. Getting started with Python, I like Python's own document, and the devil blows a series of articles published on the "programmer" (I just read those articles to determine learning Python). But further guidance, it is better to find it. I tried to write my personal learning notes in this kind of dialogue, and I have been throwing bricks. In fact, I am still too shallow to Python, and I will ask yourself to take out your own experience.

Coco is a character of my virtual. The whole story is purely fictitious. If there is a similarity ... nor is it a coincidence, there are some small tidbits in this, it is true that I have happened to me, I hope that the true story is the owner, don't It's good to play me, huh, huh. Sometimes I feel that the two people have a self-speaking self saying, this is normal, because I am in my own selfie ... If there is a code in the words, I apologize, I apologize. This html typeset is really a helpless ... In order to be better, I have to give up OpenOffice and start using word ...

If there is another typographic chaos, the subsequent code, I have to map ...

转载请注明原文地址:https://www.9cbs.com/read-4586.html

New Post(0)