简体   繁体   中英

List containing the given set of elements in the given order

I am having difficulties in writing code with finding elements in the list.

  • I need to write a program that prints "YES" if the list contains numbers 1, 2, 3 (all of them) in an arbitrary order and "NO" otherwise.

     [10, 2, 4, 15, 3, 6, 1] # YES [1, 17, 2, 45] # NO (because number 3 is missing) 
  • Also, I need to modify the program to prints "YES" if it contains all the numbers 1, 2, 3 occurring in the order they are listed (but not necessarily consecutively) and "NO" otherwise.

     [45, 1, 5, 2, 77, 3] # YES [45, 1, 3, 77, 2] # NO (incorrect order) 

    Clearly, the program should also print "NO" if one of 1, 2, 3 is missing. The tricky part of this task are cases of multiple occurrences of 1, 2, 3. For instance the program should print "YES" if [3, 2, 1, 2, 3] because there are occurrences of 1, 2, 3 appearing in the correct order while "NO" should be printed for [3, 3, 2, 2, 1, 2] because there are no such occurrences.

I am not a pro at programming, so I am a little bit confused, because my source code for the first task doesn't give me the right output:

n=int(input("Please enter the list length "))
A=[]

for i in range (0,n):
    print("Entering element ",i)
    CurEl=int(input("Please enter the element "))
    A.append(CurEl)
print(A)
for i in range (0,n):
    if (i==1) or (i==2)or (i==3):
        print("YES")
    else:
         print("NO")

Output:
Please enter the list length 5
('Entering element ', 0)
Please enter the element 1
('Entering element ', 1)
Please enter the element 2
('Entering element ', 2)
Please enter the element 3
('Entering element ', 3)
Please enter the element 4
('Entering element ', 4)
Please enter the element 5
[1, 2, 3, 4, 5]
NO
YES
YES
YES
NO

The first one is simple:

A = [10, 2, 4, 15, 3, 6, 1]
if (1 in A) and (2 in A) and (3 in A):
    print("YES")
else:
    print("NO")

The second is a little trickier:

def locate(list_var):
    i = 0
    for l in list_var:
        if l == 1 and i == 0:
            i = 1
        elif l == 2 and i == 1:
            i = 2
        elif l == 3 and i == 2:
            return "YES"

    return "NO"


print(locate([45, 1, 5, 2, 77, 3]))
print(locate([45, 1, 3, 77, 2]))
n=int(input("Please enter the list length "))
A=[]

for i in range (0,n):
    print("Entering element ",i)
    CurEl=int(input("Please enter the element "))
    A.append(CurEl)
print(A)

#checking if they are in it
list_to_check = [1,2,3]        #note 1
for each in A:
    if each in list_to_check:  #note 2
        list_to_check.remove(each) #note 3
    if list_to_check == []:    #note 4
        print("yes, 1 2 3 are in it")
        break                  #note 5
else:                          #note 6
    print("no, 1 2 3 are not in it")

#checking the order
list_to_check = [1,2,3]
check_slot_counter = 0         #note 7
for each in A:
    if each == list_to_check[check_slot_counter]:
        check_slot_counter += 1 #note 8
    if check_slot_counter == 3:
        print("yes, 1 2 3 are in order")
        break
else:
    print("no, 1 2 3 are not in order")

note 1: we are making a list to check if 1,2,3 against. Think of it as a note as you go buying grocery

note 2: we are using in to check if an object is 'in' a list for example here we are looping through the list we created A and can be thought as for each in A, if each is in list_to_check, then that condition pass and execute what's in the if statement

note 3: we are removing the item we found and matched from our list_to_check because we've found it, so we don't have to check again. remove() function takes an argument and it removes that item from the list you provide.

note 4: checking if our list is empty, if it is we know we have found all the items in our list meaning it passes.

note 5: break breaks out of the for loop with out needing to finish the for loop. Since we found everything we needed, we don't need to check for further matches.

note 6: for loops can have an else code block. They can be thought of as if-else blocks but in for loop, the else code block will run once after the for loop completes. Note that the else won't run if you break out of the for loop. This is a neat trick since if we have found everything need to find, we break out of the for loop, "else" we didn't find what we looked for and the for loop finished meaning we went through the list A .

note 7: a counter checking in the list so we can go in order from checking what's in the list.

note 8: variable += int is basically variable = variable + 1 a cleaner way of incrementing the counter. If our counter is 3, we went through our list, so we know it's in order. Since we are going through the A list one at a time in order, and we only increment the counter once we matches something, we know it's in order or not. It seems like you know how to access lists through index, and this is one way to do it.

I know there are a lot of other ways that are better solutions but since you said you are new, and wanting to learn, I feel this is the best way to go. You learn a few tricks and it's not overly complicated since everything is pretty logically layed out in also plain english.

As for why your code wasn't working you are not iterating (going through your list) you are only checking in range of 0 to size of list, basically, 0, 1, 2, 3, 4, 5, ..., n . To iterate through a list you have to use for temp_variable in list_name: see my example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM