This is the 17th day of my participation in Gwen Challenge

Instances of 26

The title

Use the recursive method to find 5 factorial. .

Analysis of the

Recursive formula: f (n) = f (n – 1) ∗ nf (n) = f (n – 1) * nf (n) = f (n – 1) ∗ n;

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:07
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 26.py
# @Software: PyCharm
# @desc: Exercise Example 26

def fact(num) :
    if num == 0:
        return 1
    else:
        return fact(num - 1) * num


if __name__ == '__main__':
    print(fact(5))

Copy the code

The results of

Examples of 27

The title

Using recursive function calls, the input 5 characters are printed out in reverse order.

Analysis of the

Note the boundary condition (that is, if the string length is 0);

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:09
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 27.py
# @Software: PyCharm
# @desc: Exercise example 27

def reverseString(str, length) :
    if (length == 0) :return
    print(str[length - 1], end=' ')
    reverseString(str, length - 1)


if __name__ == '__main__':
    str = input("Input string \n")
    reverseString(str.len(str))
Copy the code

The results of

Examples of 28

The title

There are five people sitting together and they ask the fifth person how old is he? He said he was two years older than the fourth. Asked how old the fourth man was, he said he was two years older than the third. He asked the third man and said he was two years older than the second. Ask the second person and say he is two years older than the first person. Finally asked the first man, he said 10. How old is the fifth, please?

Analysis of the

Using the method of recursion, recursion is divided into two stages: back and recursion. If you want to know the age of the fifth person, you need to know the age of the fourth person, and so on up to the first person (10 years old) and back again.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:16
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 28.py
# @Software: PyCharm
# @desc: Exercise example 28

def age(num) :
    if num == 1:
        return 10
    else:
        return 2 + age(num - 1)


if __name__ == '__main__':
    print("Age of the fifth man:" + str(age(5)))
Copy the code

The results of

Examples of 29

The title

Given a positive integer with no more than 5 digits, the following requirements: 1. Find the number of digits, 2.

Analysis of the

The key is how to decompose the positive integer and print it in reverse order;

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:19
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 29.py
# @Software: PyCharm
# @desc: Exercise example 29

if __name__ == '__main__':
    num = int(input("Enter a positive integer with more than 5 digits \n"))
    one = num % 10
    ten = num % 100 // 10
    hundred = num % 1000 // 100
    thousand = num % 10000 // 1000
    million = num // 10000

    ifmillion ! =0:
        print("Five digits:", one, ten, hundred, thousand, million)
    elifthousand ! =0:
        print("Four digits:", one, ten, hundred, thousand)
    elifhundred ! =0:
        print("Three digits:", one, ten, hundred)
    eliften ! =0:
        print("Two digits:", one, ten)
    elifone ! =0:
        print("1 digit:", one)

Copy the code

The results of

Examples of 30

The title

A 5 digit number, determine whether it is a palindrome number. That is, 12321 is a palindrome number, the ones place is the same as the thousands place, and the tens place is the same as the thousands place.

Analysis of the

As in the previous example, the point is to factor the integer, and then determine whether the ones place is the same as the thousands place, and the tens place is the same as the thousands place;

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:30
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 30.py
# @Software: PyCharm
# @desc: Exercise example 30

if __name__ == '__main__':
    num = int(input("Enter a 5-digit positive integer \n"))
    one = num % 10
    ten = num % 100 // 10
    hundred = num % 1000 // 100
    thousand = num % 10000 // 1000
    million = num // 10000

    if one == million and ten == thousand:
        print("%d is a palindrome" % num)
    else:
        print("%d is not a palindrome" % num)
Copy the code

The results of