The principle and process of writing a red envelope in python include random. For the usage of lambda, see randomlam.pdf.

Source: Internet
Author: User

The principle and process of writing a red envelope in python include random. For the usage of lambda, see randomlam.pdf.
The principle of writing a red packet in Python is the knowledge point to be used first. The first thing to say is the extended package random. The random module is generally used to generate a random number.

Today we will use the unirom method in ramdom to generate a random floating point number within the specified range. Let's take a look at the following figure:

The value range is 10 ~ Floating Point number between 20.

 

The lambda expression is an anonymous function and another expression of the function. The following describes the effect:

The t function has three values, and returns the sum of three numbers. f is a lambda expression and returns only three numbers. def is similar to lambda, t is similar to f, (x, y, z): similar to x, y, z:, return x + y + z. Similar to: x + y + z, the function is called to return the result. However, it is very complicated to remind the function, lambda is not easy to understand because it is complicated to write, and the code is easy to understand first.

 

Go to the red envelope theme

Now, let's briefly introduce the tool to be used today. Let's start with the topic and talk about the idea of red envelopes. When you send a red envelope, you need to fill in two important parameters: the amount of the red envelope. I name it cash, when thinking about the number of people, it is important to think about the maximum and minimum values of the red envelope. There is no red envelope to receive 0. Generally, 0.01 is the minimum value. Why is there a maximum value, if you want to grab the first half of a ten-piece red packet and six others get the first half, then there will be only 9.99 remaining for the second, and there will be no better people behind it. This is not a problem. So the maximum value should be the total amount of red packets minus the minimum value multiplied by the number of people (10-0.01x6) to ensure that everyone has to compete. Of course, the most greedy way is 10-0.01x5. Someone may ask why it is 5, the first person has been ruled out, and the remaining five worst endings are 0.01. You can write the code as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import randomdic ={} lis = ['kelan', 'monkey', 'dexter ', 'Superman', 'Iron man', 'Robin '] def redpacket (cash, person, index): if cash> 0 and person! = 1: n = round (random. uniform (0.01, cash-(0.01 * person), 2) dic [lis [index] = n print str (n ). ljust (4, "0") person-= 1 cash-= n index + = 1 redpacket (cash, person, index) else: dic [lis [index] = round (cash, 2) print str (cash ). ljust (4, "0") redpacket (10, len (lis), 0) print dicprint "Best luck:", max (dic. items (), key = lambda x: x [1])

Here I use a descending method to recursively calculate the remaining amount of money and the number of people, and add a judgment to prevent infinite recursion. As long as the money cash is greater than 0 and the remaining number of persons is not 1, continue to go down: n is a random floating point number, the range is from the minimum 0.01 to the current amount, round is to take the decimal point and keep 2 digits, this dictionary is used to store the amount of Red Packets snatched by the corresponding person, and then the number of people decreases by 1, amount auto-reduction the amount currently snatched index is a sequential value from the name of the person (of course, the actual order is not obtained, here is just to conceal the introduction process), and then recursively call the remaining money cash, the remaining number of people is person, and the person in the new position re-transmits the parameter. If the last person is left (tested for 50 times, no amount less than 0 is found, so only the person is described here) then the rest of the money will be his and added to the dictionary, print the output, because the else does not call its own recursion, the final print dictionary contains the corresponding amount of people and snatching red packets, finally, we need to allow the best man to use max to retrieve the maximum dic from the list. items () returns the list [(person, cash money)] in the form of tuples. lambda writes the result of comparing the values of cash by taking the 1 position of each element, max out The largest tuples in the list.

Let's take a look at the final print effect as follows:

Is it simple and clear? In the end, you can try more results. Here I just share my ideas with you. Of course, if you have good suggestions for modifying or optimizing the methods, I will leave a message later, learn from each other. Thank you for watching.

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.