program logic configured python

This article included in the Python series of articles from entry to the master series

  After completing previous sections, bloggers feel the need 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 program 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.

  Bloggers in this chapter as we put together some classic examples and exercises, we 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 create program logic and how to apply some simple algorithm to solve real-world problems.

  Warning Warning: This very burning brain, it is recommended to watch over the age of eighteen; under eighteen years of age, parental guidance is recommended! ! ! (Ha, joke ~)

1. The classic example

1.1 Number of daffodils looking for.

  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. : $ 5 + 1 ^ 3 ^ 3 ^ 3 + 3 = $ 153.

"""
Find all the number of daffodils

Version: 0.1
Author: along
"""
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(low,mid,high)
        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.

 

1.2 a positive integer reverse

  In a similar manner, we can also achieve a reverse positive integer, for example: 54321 to 12345 into, as shown in the code below.

"""
Positive integer reversal

Version: 0.1
Author: along
"""
num = int(input('num = '))
reversed_num = 0
while num > 0:
    reversed_num = reversed_num * 10% + 10 whether 
    whether // = 10
     # print (reversed_num, whether) 
Print (reversed_num)

 

1.3 Baiqianbaiji 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?

"""
"Hundred money one hundred chicken" problem

Version: 0.1
Author: along
"""
for x in range(0,20):
    for y in range(0,33):
        Z = 100 - X - Y
         IF 100 ==. 3. 5 * X + Y * Z + /. 3 :
             Print ( ' cock:% d only hens:% d only, chicken:% d only ' % (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.

 

1.4 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 Casino Games
1000-dollar bet when we set the player to start the game
The end of the game that players lose all bets

Version: 0.1
Author: along
"""
from random import randint

Money = 1000
 the while Money> 0:
     Print ( ' Your total assets: ' , Money)
    needs_go_on = False
    while True:
        Debt = int (INPUT ( ' please bets: ' ))
         IF 0 <Debt <= Money:
             BREAK 
    First = the randint (. 1,. 6) the randint + (. 1,. 6 )
     Print ( ' players roll a point% d ' % First)
     IF First == 7 or First == 11 :
         Print ( ' the player wins! ' )
        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 = the randint (. 1,. 6) the randint + (. 1,. 6 )
         Print ( ' players roll a point% d ' % Current)
         IF Current ==. 7 :
             Print ( ' Zhuangjia Sheng ' )
            Money - = Debt Total
         elif Current == First:
             Print ( ' the player wins ' )
            money += debt
        else:
            needs_go_on = True
 Print ( ' you went bankrupt, the end of the game! ' )

Note: This is too brain burning, and bloggers has gone bankrupt (laugh cry ~)

 

2. useful exercise

2.1 before generating the Fibonacci number column 20.

Description: The 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.

Answer:

"""
Fibonacci number before the output 20 of Number of
1 1 2 3 5 8 13 21 ...
Version: 0.1
Author: along
"""
a = 0
b = 1
for _ in range(20):
    a, b = b, a + b
    print(a, end=' ')

 

2.2 find the perfect number less than 10,000.

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) and 28 (= 1 + 2 $ 28 + $ 14 + 7 + 4) is the number of perfect. Perfect number has many magical properties, we are interested can inform themselves about.

"""
Find all the perfect numbers between 1 to 9999
Is the perfect number except itself all other factors and this number is exactly equal to the number itself
For example: 6 = 1 + 2 + 3, 2 + 1 = 28 + 4 + 7 + 14
Version: 0.1
Author: along
"""
import math

for num in range(1, 10000):
    result = 0
    for factor in range(1, int(math.sqrt(num)) + 1):
        if num % factor == 0:
            result += factor
            if factor > 1 and num // factor != factor:
                result += num // factor
    if result == num:
        print(num)

 

2.3 the output of all primes less than 100.

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

"""
Output primes between 2 to 99

Version: 0.1
Author: along
"""
import math

for num in range(2, 100):
    is_prime = True
    for factor in range(2, int(math.sqrt(num)) + 1):
        if num % factor == 0:
            is_prime = False
            break
    if is_prime:
        print(num, end=' ')

 

Guess you like

Origin www.cnblogs.com/along21/p/11845978.html