day5 logs and jobs

Posted by whit3fir3 on Sun, 20 Feb 2022 04:41:25 +0100

day5 log

Keywords: while, continue, break

System functions:

Method: ternary operator

  1. ternary operator

    • grammar
    Value 1 if Expression 1 else Value 2
     Value 1 if expression else Value 2 (python) == expression? Value 1:Value 2 (2)
    #Example: if a is greater than 0, the result is 0, otherwise it is 1.
    a = 100
    result = 0 if a > 0 else 1 
    print(rseult)
    
    • Operation rule: if the result of the expression is True, the result of the whole operation is the value 1, otherwise it is the value 2
  2. while Loop

    • grammar

      while Conditional statement:
          Code snippet
      
    • explain

      while - keyword; Fixed writing

      Conditional statement - can be any resulting expression (assignment statement)
      • Fixed writing

      Loop body - one or more statements that maintain an indent with while; The loop body is the code that will be executed repeatedly

    • Execution process

      First judge whether the conditional statement is true, and if so, execute the loop body; After executing the loop body, judge whether the conditional statement is true. If so, execute the loop body again By analogy, if the conditional statement is False, the loop ends

  3. Selection of for and while

    • If the number of cycles is determined, use the for cycle; If the number of cycles is uncertain, use while. (use for for all problems that can be solved by using for, and use while only for those that cannot be solved)
  4. Circular keyword

    • coutinue and break

      • continue

        Function: end a cycle (if continue is encountered when executing the cycle body, when the cycle ends, directly enter the next cycle)

        for x in range(3):
            print('aaa')
            print('bbb')
            if x == 1:
                continue
            print('ccc')
        
      • break

        Function: end the whole cycle (if you encounter a break when executing the cycle body, the whole cycle will end directly)

      from random import  randint
      number = randint(0,100)
      xiao = 0
      da = 100
      count = 0
      while True:
          shu = int(input('Please enter an integer(0~100):'))
          if xiao <= shu <= da:
              count += 1
              if shu > number:
                  da = shu
                  print(f'{xiao}reach{da}section')
              elif shu < number:
                  xiao = shu
                  print(f'{xiao}reach{da}section')
              else:
                  print(f'Congratulations on your guess,Have you guessed{count}second')
                  break
          else:
              print('The number you entered is not in the range')
      
  5. About else:

    • The existence of else will not affect the execution of the original loop
    • The code after else will be executed after the end of the loop (if the loop ends because of a break, it will not be executed)

task

  1. Judge how many primes there are between 101-200 and output all primes.

    print('\n Assignment 1')
    print('Method 1')
    count = 0
    for x in range(101,201):
        flag = 'prime number'
        for y in range(2,x):
            if x % y == 0:
                flag = 'Not prime'
        if flag == 'prime number':
            count += 1
            print(x,end=' ')
    print(f'\n Prime co ownership{count}individual')
    print('Method 2')
    count = 0
    for x in range(101,201):
        for y in range(2,x):
            if x % y == 0:
               break
        else:
            print(x,end=' ')
            count += 1
    print(f'\n Prime co ownership{count}individual')
    
  2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

    print('\n Assignment 2')
    sum = 0
    for x in range(1,101):
        if x % 10 != 3:
            sum += x
    print(sum)
    
  3. There is a sequence of fractions: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the 20th fraction of this sequence

    print('\n Assignment 3')
    a = 2
    b = 1
    for x in range(19):
        a , b = a+b ,a
    print(f'{a}/{b} =',a/b)
    
  4. Write a program to calculate the factorial n of n! Results

    print('\n Assignment 4')
    n = 5
    factorail = 1
    for x in range(1,n+1):
        factorail *= x
    print(factorail)
    
  5. Ask for 1 + 2+ 3!+…+ 20! And

    print('\n Assignment 5')
    n = 5
    factorail = 1
    sum = 0
    for x in range(1,n+1):
        factorail *= x
        sum += factorail
    print(sum)
    
  6. Write a program to find the result of the expression a + aa + aaa + aaaa +... Where a is a number from 1 to 9, and the number of summation items is controlled by n. (A and N can be expressed as variables)

    For example: when a is 3 and n is 5: 3 + 33 + 333 + 3333 + 33333

    print('\n Assignment 6')
    a = 3
    n = 5
    y = 0
    sum = 0
    for x in range(0,n):
        y += 10 ** x * a
        sum += y
    print(sum)
    
  7. Console output triangle

    a.according to n The corresponding shape is output according to the different values of
    n = 5 Time             n = 4
    *****               ****
    ****                ***
    ***                 **
    **                  *
    *
    print('\n Assignment 7 a')
    n = 5
    for x in range(n,0,-1):
        for y in range(x):
            print('*',end=' ')
        print()
        
    b.according to n The corresponding shape is output according to the different values of(n Odd number)
    n = 5               n = 7
      *                    *
     ***                  ***
    *****                *****
                        *******
    print('\n Assignment 7 b')
    n = 7
    for star in range(1,n+1,2):
        y = (n - star) // 2
        for z in range(y):
            print(' ',end=' ')
        for z in range(star):
            print('*',end=' ')
        print()
        
    c. according to n The corresponding shape is output according to the different values of
    n = 4
       1
      121
     12321
    1234321
    
    n = 5
        1
       121
      12321
     1234321
    123454321
    
    print('\n Assignment 7 c')
    n = 8
    line = 2 * n - 1
    for x in range(1,n+1):
        z = x - n + 1
        r = 0
        for y in range(line):
            r += 1
            if z <= 0:#Determine whether to enter a space or a number
                print(' ',end=' ')
            else:
                print(z,end=' ')
            if r < n:
                z += 1
            else:
                z -= 1
        print()
    
  8. Xiaoming's unit issued a 100 yuan shopping card. Xiaoming went to the supermarket to buy three kinds of washing and chemical products, shampoo (15 yuan), soap (2 yuan) and toothbrush (5 yuan). If you want to spend 100 yuan exactly, what combination of purchase can you have?

    print('\n Assignment 8')
    money = 100
    for xfs in range(7):
        for xz in range(51):
            for ys in range(21):
                if xfs * 15 + xz * 2 + ys * 5 == 100:
                    print(f'{xfs}A bottle of shampoo,{xz}A bar of soap,{ys}Take the toothbrush.')
    
  9. The thickness of a piece of paper is about 0.08mm. How many times can it be folded in half to reach the height of Mount Everest (8848.13m)?

    print('\n Assignment 9')
    paper = 0.08
    height = 8848.13 * 1000
    count = 0
    while True:
        paper *= 2
        count += 1
        if paper >= height:
            print(count)
            break
    
  10. Classical question: a pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what is the total number of rabbits every month?

    print('\n Assignment 10')
    rabbit_1 = 1
    rabbit_2 = 0
    rabbit_3 = 0
    month = 0
    while True:
        month += 1
        rabbit_2,rabbit_3,rabbit_1 = rabbit_1, rabbit_3+rabbit_2,rabbit_3
        sum = rabbit_3 + rabbit_2 + rabbit_1
        print(sum)
        if month == 12:
            break
    
  11. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

    print('\n Assignment 11')
    number = 60
    number_1 = number
    while True:
            for x in range(2,number_1 + 1):#Extract a factor, end the for loop and cycle again
                if number_1 % x == 0:
                    number_1 //= x
                    if x == number:#Judge whether number is a prime number
                        print(f'{number}Is a prime number')
                        break
                    else:#Otherwise, run again
                        print(x,end=' ')
                    if number_1 == 1:#Judge whether to enter '*' or '='.
                        print(f'= {number}')
                    else:
                        print('*',end=' ')
                    break
            else:
                break
    
  12. A company uses a public telephone to transmit data. The data is a four digit integer and is encrypted in the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits. Find the encrypted value of the input four digit integer

    print('\n Assignment 12')
    number = str(4565)
    encryption = 0
    count = 0
    for x in number:
        y = int(x)
        encryption += ((y + 5) % 10) * 10 ** count
        count += 1
    print(encryption)
    
  13. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

    print('\n Assignment 13')
    number = 60
    number_1 = number
    print(f'{number} = ',end='')
    while True:
            for x in range(2,number_1 + 1):#Extract a factor, end the for loop and cycle again
                if number_1 % x == 0:
                    number_1 //= x
                    if x == number:#Judge whether number is a prime number
                        print(f'{number}Is a prime number')
                        break
                    else:#Otherwise, run again
                        print(x,end=' ')
                    if number_1 == 1:#Judge whether to enter '*' or '='.
                        print()
                    else:
                        print('*',end=' ')
                    break
            else:
                break
    
  14. The principal of 10000 yuan is deposited in the bank with an annual interest rate of 3%. Every one year, add the principal and interest as the new principal. Calculate the principal obtained after 5 years.

    print('\n Operation 14')
    principal = 10000
    interest = 0
    year = 5
    for x in range(n):
        interest = principal * 0.003
        principal += interest
    print(principal)
    
  15. Enter an integer and calculate the sum of its digits. (Note: the input integer can be any bit)

    print('\n Assignment 15')
    number = str(123456)
    z = 0
    for x in number:
        y = int(x)
        z += y
    print(z)
    
  16. Find the maximum common divisor and minimum common multiple of two numbers. (hint: the common divisor must be less than or equal to the smaller of the two numbers, and can be divided by the two numbers at the same time; the common multiple must be greater than or equal to the larger of the two numbers, and the multiple of the larger number can be divided by the decimal of the two numbers)

    print('\n Assignment 16')
    number_1 = 12
    number_2 = 18
    if number_1 >= number_2:#Calculate the smaller and larger number of the two numbers
        number_big = number_1
        number_little = number_2
    else:
        number_big = number_2
        number_little = number_1
    for x in range(1,number_little+1):#Find the common divisor of two numbers, and the common divisor obtained later will cover the previous common divisor, so the last common divisor is the largest common divisor
        if number_1 % x == 0 and number_2 % x == 0:
            pact = x
    print(f'The greatest common divisor is{pact}')
    for x in range(number_big,number_big*number_little+1):
        if x % number_1 == 0 and x % number_2 == 0 :
            multiple = x
            break
    print(f'The least common multiple is{multiple}')
    

Topics: Python