day6 list job

Posted by asherinho on Tue, 22 Feb 2022 13:46:02 +0100

1. Basic questions

  1. Given a list of numbers, print all odd numbers in the list

    list1=[22,33,45,67,78]
    for x in list1:
        if x%2 !=0:
            print(x)
    
  2. Given a list of numbers, print all the numbers in the list that can be divided by 3 but cannot be divided by 2

    list2=[2,3,6,8,9,10]
    for x  in list2:
        if x%3==0 and x%2 !=0:
            print(x)
    
  3. Given a list of numbers, calculate the sum of all even numbers

    sum=0
    list3=[2,6,78,3,7]
    for x in list3:
        if x%2==0:
            sum+=x
    print(sum)
    
  4. A list of numbers is known, and the number of tens in the list is 1

    count=0
    list4=[12,112,23,45,1126]
    for x in list4:
        if x//10%10==1:
            count+=1
    print(count)
    # A list of numbers is known, and the number of ten digits in the statistical list is 0
    nums=[2,3,56,108,90,119]
    count=0
    for i in nums:
        if x>10 and x//10%10==0:
            count+=1
    print(count)
    
  5. Given a list, get all elements in the list whose subscripts are odd (subscript values starting from 0)

    For example: list1 = [10, 20, 5, 34, 90, 8]

    Result: [20, 34, 8]

    # First kind
    list1 = [10, 20, 5, 34, 90, 8]
    print(list1[1::2])
    # Second
    list1 = [10, 20, 5, 34, 90, 8]
    for x in range(len(list1)):
        if x%2 !=0:
            print(x,list1[x])
    
  6. Given a list of numbers, multiply all elements in the list by 2

    For example: num = [10, 3, 6, 12] multiplied by 2: num = [20, 6, 12, 24]

    list_1=[]
    nums = [10, 3, 6, 12]
    for x in nums:
        i=x*2
        list_1.append(i)
    print(list_1)
    
    list_1 = [10, 3, 6, 12]
    for index,item in enumerate(list_1):
        list_1[index]=item*2
    print(list_1)
    
  7. Given a list, get the central element of the list

    For example: num = [10, 2, 6, 12] - > the central elements are: 2 and 6

    Num = [10, 2, 6, 12, 10] - > the central element is: 6

    nums = [10, 2, 6,14,16]
    x=len(nums)
    if x%2==0:
        print(nums[(x//2)-1],nums[(x//2)])
    else:
        print(nums[(x//2)])
    
  8. Given a list, get all integer elements in the list

    For example: list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5]

    The result is: [10, 100, 5]

    list_1=[]
    list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5]
    for x in list1:
        if type(x)==int:
            list_1.append(x)
    print(list_1)
    

2. Advanced questions

  1. Define a list, save the scores of multiple students, and delete the values below 60 in the list

    For example: scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] after deletion: scores = [60, 89, 99, 80, 71, 66]

    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    new_scores=scores.copy()
    for x in scores:
        if x<60:
            new_scores.remove(x)
    print(new_scores)
    
  2. It is known that a list holds the names of multiple students. It is required to remove the duplicate names in the list

    For example: names = ['Xiao Ming', 'Zhang San', 'Li Si', 'Zhang San', 'Zhang San', 'Xiao Ming', 'Wang Wu', 'Wang Wu']

    After weight removal: names = ['Xiao Ming', 'Zhang San', 'Li Si', 'Wang Wu']

    list1=[]
    names = ['Xiao Ming', 'Zhang San', 'Li Si', 'Zhang San', 'Zhang San', 'Xiao Ming', 'Wang Wu', 'Wang Wu']
    for x in names:
        if x not in list1:
            list1.append(x)
    print(list1)
    
    
  3. Given a list of numbers, get the element with the largest value in the list (the max function cannot be used)

    list1=[1,4,9,3,6]
    max1=list1[0]
    for i in list1:
        if max1<i:
            max1=i
    print(max1)
    
  4. It is known that there are two sequential tables (the elements in the list have been arranged in order from small to large). It is required to merge the two lists. After merging, the elements are still sorted from small to large

    For example: list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70]

    Combined results: [9, 10, 23, 38, 39, 41, 52, 55, 55, 70, 80]

    list1 = [10, 23, 39, 41, 52, 55, 80]
    list2 = [9, 38, 55, 70]
    for x in list2:
        list1.append(x)
    print(sorted(list1))
    
  5. Given an ordered number list (from small to large), enter any number and insert the entered number into the list. It is required that the list still maintains the sorting relationship from small to large after insertion

    For example: list1 = [10, 23, 45, 67, 91] input: 50 - > LIST1 = [10, 23, 45, 50, 67, 91]

list1 = [10, 23, 45, 67, 91]
value=int(input('Please enter an arbitrary positive integer:'))
for index,item in enumerate(list1):
    if value<item:
        list1.insert(index,value)
        break
else:
    list1.append(value)
print(list1)

Topics: Algorithm leetcode