day11 basic operation of function

Posted by LDM2009 on Tue, 14 Dec 2021 03:15:23 +0100

  1. Write a function to realize the function of maketrans and convert two strings into a dictionary. The characters in the first string are keys and the characters in the second string are values

    First string: 'abcmn' second string: 'one, two, three, four, five'

    Results: {'a': 'one', 'b': 'two', 'c': 'three','m ':' four ',' n ':' five '}

    def make(str_key,str_valu):
        len1 = len(str_key)
        dict1={str_key[i]:str_valu[i] for i in range(0,len1)}
        print(dict1)
    str_key = 'abcmn'
    str_valu = 'one two three four five'
    make(str_key,str_valu)
    
  2. Write your own join function to connect the elements in any sequence with the specified string into a new string

    Sequence: [10, 20, 30, 'abc'] string: '+' result: '10+20+30+abc'

    Sequence: 'abc' string: '–' result: 'a – b--c'

    Note: the elements in the sequence can not be strings

    def str2(str3,a):
        str1 = str3[0]
        for i in range(1,len(str3)):
            str1 += a
            str1+=str3[i]
        print(str1)
    str3 = input('Input string:')
    a = input('Enter connection characters:')
    str2(str3,a)
    
    ###############################################
    def  new_str1(list1, str2):
        str4=str(list1[0])
        for x in range(1,len(list1)):
            a = str(list1[x])
            str4 += str2
            str4+=a
        print(str4)
    list1 = [10,20,30,'abc']
    str2 = '+'
    new_str1(list1,str2)
    
  3. Write an input own upper function to judge whether the specified string is a pure uppercase string

    'AMNDS' -> True

    'amsKS' -> False

    '123asd' -> False

    def upper1(str1):
        for i in str1:
            if not ('A' <=i <='Z'):
                print('Not pure uppercase')
                break
            else:
                print('Pure capital')
    str1 = input('Please enter a string:')
    upper1(str1)
    
  4. Write a clear function to clear the specified list.

    Note: the function is to clear the original list without generating a new list

    def clear_list(list1):
        while list1==[]:
            del list1[0]
            return list1
    list1 = [1, 2, 4, 6, 1, 3, 1, 5]
    print(clear_list(list1))
    
  5. Write a reverse function to reverse the elements in the list

    Two methods: 1 Generate a new list 2 The order of the elements in the original list is directly modified without generating a new list

    def new_list(list1):
        list2 = []
        for i in range(-1,-len(list1)-1,-1):
            list2.append(list1[i])
        print(list2)
    new_list([1,2,3,4,5,6,7,8,9])
    
    ########################################################
    def new_list(list1):
        for i in range(len(list1)):
            a=list1.pop(-1)
            list1.insert(i,a)
        print(list1)
    new_list([1,2,3,4,5,6,7,8,9])
    
  6. Write a replace function to replace the specified substring in the string with a new substring

    Original string: 'abc123abc ha ha uui123' old substring: '123' new substring: 'AB'

    Results: 'abcbabc ha ha uuiAB'

    
    
    
    
  7. Write a function that can get the ten digits of any integer

    123 -> 2

    82339 -> 3

    9 -> 0

    -234 -> 3

    def shiwei(num1):
        a = num1//10%10
        print(a)
    shiwei(6614)
    
    
    
  8. Write a function to realize the function of mathematical set operator & and find the common part of two sets:

    Set 1: {1, 2, 3} set 2: {6, 7, 3, 9, 1}

    Result: {1, 3}

    def and1(j1,j2):
        j3=j1&j2
        print(j3)
    and1({'s',3,1,'g',6,'s',1},{1,2,3,4,5,6})
    
    
    
    
  9. Write a function to implement the function of its own dictionary update method, and add all the key value pairs in one dictionary to another dictionary

    Dictionary 1: {a ': 10,' b ': 20} dictionary 2: {name': Zhang San, 'age': 18} - > the result turns Dictionary 1 into: {a ': 10,' b ': 20, name': Zhang San, 'age': 18}

    Dictionary 1: {a ': 10,' b ': 20} dictionary 2: {a': 100, 'c': 200} - > the result makes Dictionary 1: {a ': 10,' b ': 20,' c ': 200}

    def dict1(dict2:dict,dict3:dict):
        list1 = list(dict2.keys())
        list2 = list(dict2.values())
        for i in range(len(list1)):
            dict3.setdefault(list1[i],list2[i])
        print(dict3)
    dict1({'a': 10, 'b': 20},{'name': 'Zhang San', 'age': 18})
    
  10. Write a function to determine whether the specified number is a palindrome number

    12321 -> True

    2332 -> True

    9876789 -> True

    1232 -> False

    def judge(num):
        for i in range(len(num)//2):
            if num[i]!=num[-(i+1)]:
                print('Not palindromes')
                break
        else:
            print('Is the palindrome number')
    judge('1234321')
    
    
    
  11. Write a function to judge whether the specified number is complete (more difficult)

    Perfect: the sum of the true factors is equal to the number itself

    For example, 6 is a perfect number, the truth factors of 6 are 1, 2 and 3, and 1 + 2 + 3 equals 6, so 6 is a perfect number

    28 is the perfect number. The true factors of 28 are 1, 2, 4, 7 and 14. 1 + 2 + 4 + 7 + 14 equals 28, so 28 is the perfect number

    12 is not perfect: the true factor of 12: 1, 2, 3, 4, 6, 1 + 2 + 3 + 4 + 6 is not equal to 12

    
    
    

Topics: Python