05. constructor logic

Construction program logic

After completing a few chapters earlier, I think it is necessary here to take you to do some exercises to consolidate the knowledge learned before, although so far we learn the contents of Python's just tip of the iceberg, but these elements have been enough for us to build a program in logic. For beginners programming language, in after learning the Python core language elements (variables, types, operators, expressions, branched structure, cyclic structure, etc.), you must do one thing is to try to use what they have learned solve real-world problems, in other words training algorithm (method to solve the problem and steps) translated into Python code their own ability to use human natural language, and this issue must in order to reach through a lot of practice.

We are all in this chapter compiled some classic examples and exercises, I hope these examples, on the one hand knowledge of Python before you help to consolidate what they have learned on the other hand to help you learn how to build a program and how to apply some simple logic algorithms solve real-world problems.

The classic example

  1. Looking for a few daffodils .

    Description : narcissistic number is also known as super variables not fully digital, narcissistic number, since a power of, Armstrong number, which is a 3-digit number, each digit of the digital sum is exactly equal to the cube itself, e.g. : 1 3 + 5 3 + 3 3 = 153 1^3 + 5^3+ 3^3=153

    """
    找出所有水仙花数
    
    Version: 0.1
    Author: 骆昊
    """
    
    for num in range(100, 1000):
        low = num % 10
        mid = num // 10 % 10
        high = num // 100
        if num == low ** 3 + mid ** 3 + high ** 3:
            print(num)
    

    In the above code, we divisible by modulo operation, respectively, and had a bit to find a three-digit, ten and one hundred such tips in the actual development is still commonly used. In a similar manner, we can also achieve a reverse positive integer, for example: 54321 to 12345 into, as shown in the code below.

    """
    正整数的反转
    
    Version: 0.1
    Author: 骆昊
    """
    
    num = int(input('num = '))
    reversed_num = 0
    while num > 0:
        reversed_num = reversed_num * 10 + num % 10
        num //= 10
    print(reversed_num)
    
  2. One hundred money one hundred chicken problem.

    Description : one hundred money one hundred chickens ancient Chinese mathematician Zhang Qiu Jian in mathematical problem "was considered" a book made of: a chicken Weng worth five chickens mother a valuable three chicks a valuable three. One hundred to buy one hundred chicken, chicken and asked Weng, mother chicken, chicks each geometry? Modern culture is translated into: 5 yuan a rooster, a hen 3 yuan, one yuan three chicks, one hundred dollars to buy 100 chickens, ask rooster, hens, chicks have how many?

    """
    《百钱百鸡》问题
    
    Version: 0.1
    Author: 骆昊
    """
    
    for x in range(0, 20):
        for y in range(0, 33):
            z = 100 - x - y
            if 5 * x + 3 * y + z / 3 == 100:
                print('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z))
    

    The above method is called a brute-force method , also called the violence a search method , this approach by all possible candidates listed one by one alternative solution and check the description of each candidate meets the problem, finally get solution of the problem. This approach looks awkward, but computing power for very powerful computers, it is usually a viable even is a good choice, but if there is solution of the problem, this method will be able to find it.

  3. CRAPS casino games .

    Description : CRAPS also known as Craps, Las Vegas is a very popular one table gambling game. The game uses two dice, game players earn points by shaking two dice. The rules are simple: Players roll the dice if the first roll out of the 7:00 or 11:00, the player wins; if the players shake out first 2:00, 3:00 or 12:00, Zhuang Jiasheng; other points the players continue to roll the dice, If the player shake out the 7:00, Zhuang Jiasheng; if the players shake out the first shake of points, the player wins; other points, players continue to die until a winner.

    """
    Craps赌博游戏
    我们设定玩家开始游戏时有1000元的赌注
    游戏结束的条件是玩家输光所有的赌注
    
    Version: 0.1
    Author: 骆昊
    """
    from random import randint
    
    money = 1000
    while money > 0:
        print('你的总资产为:', money)
        needs_go_on = False
        while True:
            debt = int(input('请下注: '))
            if 0 < debt <= money:
                break
        first = randint(1, 6) + randint(1, 6)
        print('玩家摇出了%d点' % first)
        if first == 7 or first == 11:
            print('玩家胜!')
            money += debt
        elif first == 2 or first == 3 or first == 12:
            print('庄家胜!')
            money -= debt
        else:
            needs_go_on = True
        while needs_go_on:
            needs_go_on = False
            current = randint(1, 6) + randint(1, 6)
            print('玩家摇出了%d点' % current)
            if current == 7:
                print('庄家胜')
                money -= debt
            elif current == first:
                print('玩家胜')
                money += debt
            else:
                needs_go_on = True
    print('你破产了, 游戏结束!')
    

### useful exercise

  1. Generating a Fibonacci number before the number 20.

    Description : Fibonacci number (Fibonacci sequence), also known as golden columns, the Italian mathematician Leonardo Fibonacci (Leonardoda Fibonacci) put forward in the "calculation of the book" in a rabbit under ideal assumptions the growth rate of the problem and to introduce the number of columns, so that the series is also dubbed the "rabbit series." Fibonacci number characterized by the first two columns are the number 1, starting from the third number, each number is preceded by two numbers, like this: 1, 1, 2, 3, 5 , 8, 13, 21, 34, 55, 89, 144, .... Have direct application Fibonacci number in the fields of modern physics, quasi-crystalline structure, chemistry.

  2. 10000 to find out within the perfect number .

    Description : Perfect who is called a perfect number or complete number, and it's all true factor (ie, in addition to its own factor) of (that is, the factor function) is exactly equal to itself. For example: 6 ( 6 = 1 + 2 + 3 6=1+2+3 ) and 28 ( 28 = 1 + 2 + 4 + 7 + 14 28=1+2+4+7+14 ) is the perfect number. Perfect number has many magical properties, we are interested can inform themselves about.

  3. The output of all primes less than 100 .

    Description : prime number refers only divisible by a positive integer and itself (not including 1).

Exercises in this chapter refer to the answer above the corresponding code directory, if you need help, please refer the reader to see the answer.


I welcome the attention of the public number, reply keyword " Python ", there will be a gift both hands! ! ! I wish you a successful interview ! ! !

发布了95 篇原创文章 · 获赞 0 · 访问量 3072

Guess you like

Origin blog.csdn.net/weixin_41818794/article/details/104243611