day8 - string and collection jobs

  1. Use three sets to represent the names of students who choose courses in three disciplines (a student can choose multiple courses at the same time)

    subject1 = {'Xiao Song', 'Xiao Liu', 'Xiao Zhang', 'pony', 'Xiao He'}
    subject2 = {'Xiao Yan', 'Xiao Liu', 'Xiao Zhang', 'pony'}
    subject3 = {'Xiao Yan', 'petty thief', 'Xiao Zhang', 'Xiaotian', 'Small hall', 'Xiao Hong'}
    
    1. How many students are there in total

      print(len(subject1 | subject2 | subject3))
      
    2. Find the number of people who only selected the first subject and their corresponding names

      new_subject = (subject1 - subject2) & (subject1 - subject3)
      print(len(new_subject), new_subject)
      
    3. Find the number of students who have chosen only one subject and their corresponding names

      new_subject = (subject1 | subject2 | subject3) - (subject1 & subject2) - (subject1 & subject3) - (subject2 & subject3)
      print(len(new_subject), new_subject)
      
    4. Find the number of students who have chosen only two subjects and their corresponding names

      new_subject = ((subject1 & subject2) | (subject1 & subject3) | (subject2 & subject3)) - ((subject1 & subject3) & subject2)
      print(len(new_subject), new_subject)
      
    5. Find the number of students in three subjects and their corresponding names

      new_subject = (subject1 & subject2 & subject3)
      print(len(new_subject), new_subject)
      
  2. Enter a string and print all characters in odd bits (subscripts are characters in bits 1, 3, 5, 7...)

    For example, input 'abcd1234' and output 'bd24'

    str1 = 'abcd1234 '
    print(str1[1::2])
    
  3. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)

    str1 = str(input("Please enter user name:"))
    Method 1:
    if 5 < len(str1) < 11:
        print('The user name is legal')
    else:
        print('Illegal user name')
        
    Method 2:
    str2='legitimate' if 5 < len(str1) < 11 else 'wrongful'
    print(str2)
    
  4. Enter the user name to judge whether the user name is legal (the user name can only be composed of numbers and letters)

    For example, 'ABC' - Legal '123' - Legal 'abc123a' - Legal

    str1 = str(input("Please enter user name:"))
    for i in str1:
        if not('0' < i < '9' or 'a' < i < 'z' or 'A' < i < 'Z'):
            print('Illegal user')
            break
    else:
        print('User legal')
    
  5. Enter the user name and judge whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be capital letters)

    For example, 'ABC' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - Legal

    str1 = str(input("Please enter user name:"))
    
    if 'A' <= str1[0] <= 'Z':
        count1 = 0
        for i in str1[1::]:
            if not ('0' < i < '9' or 'a' < i < 'z' or 'A' < i < 'Z'):
                print('Illegal user name')
                break
            elif '0' <= i <= '9':
                count1 += 1  # Number of occurrences of Statistics
        else:
            if count1 != 0:
                print('The user name is legal')
            else:
                print('Illegal user name')
    else:
        print('Illegal user')
    
  6. Enter a string and take out all the numeric characters in the string to produce a new string

    For example: input * * 'abc1shj23kls99+2kkk' * * output: '123992'

    str1 = str(input('Please enter a string:'))
    str2=str()
    for i in str1:
        if '0'<=i<='9':
            str2+=i
    print(str2)
    
    # Exercise: insert ABC after all Chinese
    str1 = 'jsjs92 name sssj=-92021 October 21:25:58'
    # 'jsjs92 abcssj = - 9, ABC10, ABC21, 2001 ABC10:25:58'
    str2 = str()
    for i in str1:
        str2 += i
        if '\u4e00' <= i <= '\u9fa5':
            str2 += 'ABC'
    print(str2)
    
  7. Input a string and change all lowercase letters in the string into corresponding uppercase letters for output (implemented by upper method and self writing algorithm)

    For example: input * * 'A2H2KLM12 +' * * output 'A2H2KLM12 +'

    str1 = str(input("Please enter user name:"))
    Method 1:
    '''
    ASCII Coding difference:
    a=97  A=65
    b=98  B=66
     Difference 32
     Lowercase coded value-32 ==Uppercase coded value
    '''
    str2 = str()
    for i in str1:
        if 'a' <= i <= 'z':
            x = chr(ord(i) - 32)
            str2 += x
        else:
            str2 += i
    print(str2)
    Method 2:
    dict1 = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K',
             'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V',
             'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z'}
    str2 = str()
    for i in str1:
        if 'a' <= i <= 'z':
            i == dict1[i]
            str2 += dict1[i]
        else:
            str2 += i
    print(str2)
    
  8. Enter a number less than 1000 to generate the corresponding student number

    For example: input * *'23 ', output' py1901023 '* * input * *'9', output 'py1901009' * * input * *'123 ', output' py1901123 '**

    str1 = str(input('Please enter a number less than 1000:'))
    str2 = 'py1901'
    str3 = str()
    Method 1:
    str3=str2+'0'*(3-len(str1))+str1
    print(str3)
    
    Method 2:
    if len(str1) == 1:
        str3 = str2 + '00' + str1
        print(str3)
    elif len(str1) == 2:
        str3 = str2 + '0' + str1
        print(str3)
    else:
        str3 = str2 + str1
        print(str3)
    
  9. Enter a string to count the number of non alphanumeric characters in the string

    For example: input * * 'anc2+93-sj nonsense' * * output: 4 input * * '= =' * * output: 3

    str1 = str(input('Please enter a string:'))
    count1 = 0
    for i in str1:
        if '0' <= i <= '9' or 'a' <= i <= 'z' or 'A' <= i <= 'Z':
            continue
        else:
            count1 += 1
    print(count1)
    
  10. Enter a string, change the beginning and end of the string to '+' to produce a new string

For example: input string * * 'abc123', output '+ bc12 +'**

str1 = str(input('Please enter a string:'))
str2 = str()
if str1[0] != '+' and str1[-1] != '+':
    str2 = '+' + str1[1:(len(str1) - 1)] + '+'
print(str2)
  1. Enter a string to get the middle character of the string

    For example: input * * 'abc1234' * * output: '1' input * * 'abc123' * * output * * 'c1'**

    str1 = str(input('Please enter a string:'))
    if len(str1) % 2 == 0:
        print(str1[len(str1) // 2 - 1], str1[len(str1) // 2])
    else:
        print(str1[len(str1) // 2])
    
  2. The writer implements the function of the string function find/index (get the position of the first occurrence of string 2 in string 1)

    For example, string 1 is: how are you? Im fine, Thank you! , String 2 is: you, print 8

    str1 = 'how are you? Im fine, Thank you!'
    str2 = 'you'
    l = len(str2)
    for index in range(len(str1) - l+1):
        if str1[index:index + l] == str2:
            print(index)
            break
    else:
        print('The same character does not exist')
    
  3. Gets the common characters in two strings

    For example, string 1 is: abc123, string 2 is: huak3, print: common characters are: a3

str1 = 'abc123'
str2 = 'huak3'
for i in str2:
    for j in str1:
        if i == j:
            print(i, end=' ')
        else:
            continue

Tags: Python Back-end

Posted on Mon, 25 Oct 2021 06:57:15 -0400 by nuying117