day9 - string and collection jobs

Posted by sfhc on Fri, 10 Dec 2021 02:51:39 +0100

  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)

    A = {'this','yes','one','individual','name','word'}
    B = {'really','meeting','take','name','word'}
    C = {'I','chaos','take','of','name'}
    
    1. How many students are there in total

      D = A | B | C
      count = 0
      for i in D:
          count+=1
      print(count)
      
    2. Find the number of people who only selected the first subject and their corresponding names

      a = A-(B|C)
      count1=0
      for i in a:
          count1+=1
      print(count1,a)
      
    3. Find the number of students who have chosen only one subject and their corresponding names

      a = A-(B|C)
      b = B-(A|C)
      c = C-(B|A)
      d =a|b|c
      count2 = 0
      for i in d:
          count2+=1
      print(count2,d)d =A^B^C
      count2 = 0
      for i in d:
          count2+=1
      print(count2,d)
      
    4. Find the number of students who have chosen only two subjects and their corresponding names

      E=D-d-(A&B&C)
      count3=0
      for i in E:
          count3 +=1
      print(count3,E)
      
    5. Find the number of students who have selected three courses and their corresponding names

      f = A&B&C
      count4 =0
      for i in f:
          count4 +=1
      print(count4,f)
      
  2. Enter a string and print all characters in odd bits (subscripts are characters in bits 1, 3, 5, 7...)

    str1 = '1a2b3c4d5e'
    for i in range(len(str1)):
        if i % 2==1:
            print(str1[i])
    

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

  3. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)

    name1 =input('user name:')
    if 6<= len(name1) <=10:
        print('Legal user name')
    else:
        print('Illegal user name')
    
  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

    name1 = input('user name:')
    for i in name1:
        if 48<=ord(i)<=57 or 65<=ord(i)<=90 or 97<=ord(i)<=122:
            continue
        else:
            print('User name is unqualified')
            break
    else:
        print('User name qualified')
    
  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

    name1=input('user name:')
    if 65<= ord(name1[0]) <= 90:
        for i in range(1,len(name1)):
            if  48<=ord(name1[i])<=57 or  97<=ord(name1[i])<=122:
                continue
            else:
                print('Illegal user name')
                break
        else:
            print('legitimate')
    else:
        print('Illegal user name')
    
  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 = input('Input string:')
    for i in str1:
        if 48<=ord(i)<=57:
            print(i,end='')
    
  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 = input('Input string:')
    a = str1.upper()
    print(a)
    
    ##################################################3
    str1 = input('Input string:')
    new_str= str()
    for i in str1:
        if 97<=ord(i)<=122:
            i =chr(ord(i)-32)
            new_str +=i
        else:
            new_str +=i
    print(new_str)
    
  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 '**

    num = input('Student No.:')
    if len(num)==1:
        print(f'190100{num}')
    if len(num)==2:
        print(f'19010{num}')
    if len(num) ==3:
        print(f'1901{num}')
    
  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 = input('Input string:')
    count1 = 0
    for i in str1:
        if 48 <= ord(i) <= 57:
            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 +'**

  11. Enter a string to get the middle character of the string

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

    str1 = input('Please enter a string:')
    a = len(str1)
    if a % 2:
        print(str1[(a-1)//2])
    else:
        print(str1[a//2],str1[a//2-1])
    
  12. 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

  13. Gets the common characters in two strings

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

    a = 'abc123'
    b = 'huack3'
    c = str()
    for i in a:
        if i in b:
            c +=i
    print(c)
    

Topics: Python