Getting started with Python(14)

Getting started with Python (14/18)
Section 14****Application Cases (2)

Hello everyone! Following yesterday's case analysis, today we continue to explain new cases.

Case 4. Calculation of bonus based on sales

demand:

(Network case) An enterprise implements a tiered sales performance plan. The rules for calculating bonuses based on performance are as follows:

(1) For the part of sales less than or equal to 100,000, 10% shall be accrued;

(2) The amount of sales exceeding 100,000 but less than or equal to 200,000 is accrued 7.5%;

(3) For the part of sales exceeding 200,000 but less than or equal to 400,000, 5% shall be accrued;

(4) 3% shall be accrued for the part of sales exceeding 400,000 but less than or equal to 600,000;

(5) For the part of sales exceeding 600,000 but less than or equal to 1 million, 1.5% shall be accrued;

(6) For the part of sales exceeding 1 million, 1% shall be accrued.

Project requirements: Enter someone's sales and immediately get the bonus that should be paid.

demand analysis:

(1) What we need to pay special attention to is that the bonus calculation rules are designed according to performance segments, which means that the calculation of performance bonuses across tiers needs to be summed up in stages, rather than calculated based on the highest standard.

(2) After understanding the problem of subsection summation, the solution is: according to the scope of performance coverage, calculate in subsections, and then add.

(3) To judge the scope of performance coverage, branch statements must be used.

Calculate bonus source code based on sales:

**Code analysis: **

(1) The first line, needless to say, is used to receive a performance number entered by the user. What does float() do? This (embarrassing)...Data type conversion. Didn’t it mean that input() received all strings? What we have to deal with is a number, and it is probably a decimal. After all, it is in ten thousand yuan. The 1 after the decimal point is 1000 oceans.

(2) Know that the branch statements are all if...elif...elif... but where does the first if start? Of course, start from the smallest first segment indicator within 100,000. If the condition is met, then the performance of this segment will be accrued 10%, OK? The next paragraph, of course, is elif. If it is judged to be more than 100,000 and less than 200,000, the conditions are met, then the performance of this paragraph is accrued 7.5%. Is it over? Of course it can't be done! Otherwise, it's better to only sell for less than 10, high-ranking performance means that it must cover (including) the previous one's performance. This is not controversial, right? Therefore, the previous performance bonus must also be required! This must not be ignored. If you miss it, you will die (it is hard-earned money)!

(3) OK, and so on, continue elif, of course, all possibilities need to be judged, in case you miss which possibility, then the most probable event is the battle situation reported back by the strong sales team. Will just cover the kind of possible situation you missed, and then, what will happen? What else? There was a bug! So, what is the most important thing to write branch statements? Full coverage is the most important! This is called "exhaustive thinking". (Hurry up and make up for the "exhaustive method" mentioned in the logic class in middle school Chinese. It's a bit of an impression, right?)

(4) Needless to say at the end, show the bonus amount, show off! Experience the feeling of opening the red envelope.

Case 5, write a bubble sort function

**Requirements: **Bubble sorting method is a classic sorting algorithm model. Please use python to write a function of bubble sorting algorithm, and can support arbitrary one-dimensional sequence objects to achieve sorting, and support elements of the same size.

demand analysis:

(1) The general algorithm of bubble sorting is: traverse a sequence, take each element and compare it with all the remaining elements. If it is found to be smaller than it, replace it. After the comparison is over, the current round of loop will be obtained. A minimum, then continue to iterate, use the same method for the remaining sets, obtain the minimum in turn, add them to the new list, and finally get a list from small to large.

(2) The bubble sorting algorithm usually has two kinds of migration method and exchange method. We will demonstrate two algorithms here.

(3) It should be noted that because elements of the same size need to be supported, do not use a collection type data structure.

1、 Migration method source code

**Code analysis: **

(1) Since it is required to write a sorting function, create a function, the name... just call orderby(), why use a function? (Embarrassing!) Well, I want to emphasize it again: **A function is a code block that can be reused repeatedly, and will be executed if and only if it is called! **Brother, the importance of functions is really not easy to forget.

(2) x=list(x), what is this? Obviously, the parameter x (it must be a sortable data sequence) passed in by the function is converted into a list list. Why do you want to convert it? It's not because the first list of the four data structures mentioned earlier is extremely convenient to operate, you can do anything! Don't think about it crooked.

(3) Then, y=[], if you don’t know this, then quickly face the wall. Create a spare empty list y, and you will know its usefulness right away. You ask me how do I know to prepare a y here in advance? I’m going, it’s not my prophecy, okay, it’s not because you have run into a wall countless times and have a long memory. So, if you write code in the future and write it imperfectly, then it’s normal, because you are a human being. "Great God" always has some way to go. Don't worry, take it step by step.

(4) The next code is the part you need to focus on to interpret. First, notice that it has two levels of loop nesting, the first level of while loop, traversing our x list, this is definitely no problem, follow the bubbling The basic idea of sorting, each value should be compared with each other. OK, I got one and assign it to xi.

(5) Then start the next round of loop. This is a for loop that iterates over the current list of x. For each iteration, one element xj is taken and compared with xi. If a smaller xj is encountered, we will use it The value of is replaced in xi (assigned to xi), until the end of the for loop iteration, we have found the smallest xi in the current list of x.

(6) Then, two things: first add xi to the spare list y; second, remove the current xi value from the existing x list. At this point, the while loop ends, and the length of the list of x will be reduced by one.

(7) Of course, the program does not end, the while loop will continue until the length of x is 0, exit the loop. The list object of x seems to be empty, but is the new y list plump up? And all the elements of y come from x, but it is orderly, neatly arranged from small to large! how about it? Do you believe it or not? Anyway, I believed it.

(8) If you still don’t believe me, OK, what you wrote is a function. You can call it and try it. It’s okay to enter a string of numbers or strings. That’s right, the final code means this!

(9) Finally, I wondered why I kept the whole y=[] at the beginning. Now I understand its purpose, the routine is deep~

To summarize:

The basic algorithm principle of the migration method is to traverse an unordered list, first take the first value, and compare it with all other values, when it encounters a value smaller than it, slip it out and continue Comparison. After a round, the value that "floats to the top" is the smallest in this round, and then it is removed from the list and added to a new list. By analogy, continue to look for smaller values in the remaining sequence until the original sequence is cleared, then the new list we get is the sorted list.

2、 Exchange method source code

The algorithm principle of the exchange method is basically the same as that of the migration method. However, it does not need to define a new list to store the smaller elements that "surfaced". Instead, it simply swaps the index positions so that the smaller values are arranged in the front position, and finally sorted.

With the foundation of the migration method, it saves time. I will not analyze the code of the exchange method in detail, lest someone (like me being long-winded) wants to throw eggs! Everyone is interested in trying to figure it out for yourself. If you really don’t understand, you can leave a message to me.

In addition, let's say it, Python has its own built-in sorting function list.sort(), which can sort list objects directly. (Look, it’s not about throwing eggs this time. Someone has clenched their fists! I really don’t want to toss you. Don’t you think that you have written a set of sorting algorithm by hand, and your confidence is bursting immediately, and it feels awesome? ?)

Next, let’s talk about case 6. Oh, no, I just saw that clenched fist, so forget it, let’s stop here today. It’s not that I don’t want to talk about it. Case 6 is a bit...a little more complicated. In order not to affect everyone’s patience to figure out the exchange algorithm of bubble sorting, let’s continue tomorrow. Case 6 is a good thing. We can spend the whole time tomorrow. Come digest it, OK?

summary

Today we shared 2 small cases, one for enterprise applications and the other for programming algorithms, which are very meaningful code design and training. Please be sure to train repeatedly after class, the code will always be better and better, not just looking at it.

Any questions, remember to leave a message.

Recommended Posts

Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Getting started with Python (2)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting started with Numpy in Python
Getting started with Ubuntu
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started python learning steps
How to get started quickly with Python
python requests.get with header
Play WeChat with Python
Web Scraping with Python
Implementing student management system with python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
How to make a globe with Python
Python | Quickly test your Python code with Hypothesis
How to process excel table with python