python - List containing the given set of elements in the given order -


i having difficulties in writing code finding elements in list.

  • i need write program prints "yes" if list contains numbers 1, 2, 3 (all of them) in arbitrary order , "no" otherwise.

    [10, 2, 4, 15, 3, 6, 1]   # yes [1, 17, 2, 45]            # no (because number 3 missing) 
  • also, need modify program prints "yes" if contains numbers 1, 2, 3 occurring in order listed (but not consecutively) , "no" otherwise.

    [45, 1, 5, 2, 77, 3]      # yes [45, 1, 3, 77, 2]         # no (incorrect order) 

    clearly, program should print "no" if 1 of 1, 2, 3 missing. tricky part of task cases of multiple occurrences of 1, 2, 3. instance program should print "yes" if [3, 2, 1, 2, 3] because there occurrences of 1, 2, 3 appearing in correct order while "no" should printed [3, 3, 2, 2, 1, 2] because there no such occurrences.

i not pro @ programming, little bit confused, because source code first task doesn't give me right output:

n=int(input("please enter list length ")) a=[]  in range (0,n):     print("entering element ",i)     curel=int(input("please enter element "))     a.append(curel) print(a) in range (0,n):     if (i==1) or (i==2)or (i==3):         print("yes")     else:          print("no")  output: please enter list length 5 ('entering element ', 0) please enter element 1 ('entering element ', 1) please enter element 2 ('entering element ', 2) please enter element 3 ('entering element ', 3) please enter element 4 ('entering element ', 4) please enter element 5 [1, 2, 3, 4, 5] no yes yes yes no 

n=int(input("please enter list length ")) a=[]  in range (0,n):     print("entering element ",i)     curel=int(input("please enter element "))     a.append(curel) print(a)  #checking if in list_to_check = [1,2,3]        #note 1 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 in it")         break                  #note 5 else:                          #note 6     print("no, 1 2 3 not in it")  #checking order list_to_check = [1,2,3] check_slot_counter = 0         #note 7 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 in order")         break else:     print("no, 1 2 3 not in order") 

note 1: making list check if 1,2,3 against. think of note go buying grocery

note 2: using in check if object 'in' list example here looping through list created a , can thought each in a, if each in list_to_check, condition pass , execute what's in if statement

note 3: removing item found , matched our list_to_check because we've found it, don't have check again. remove() function takes argument , removes item list provide.

note 4: checking if our list empty, if know have found items in our list meaning passes.

note 5: break breaks out of loop out needing finish loop. since found needed, don't need check further matches.

note 6: loops can have else code block. can thought of if-else blocks in loop, else code block run once after loop completes. note else won't run if break out of loop. neat trick since if have found need find, break out of loop, "else" didn't find looked , loop finished meaning went through list a.

note 7: counter checking in list can go in order checking what's in list.

note 8: variable += int variable = variable + 1 cleaner way of incrementing counter. if our counter 3, went through our list, know it's in order. since going through a list 1 @ time in order, , increment counter once matches something, know it's in order or not. seems know how access lists through index, , 1 way it.

i know there lot of other ways better solutions since said new, , wanting learn, feel best way go. learn few tricks , it's not overly complicated since pretty logically layed out in plain english.

as why code wasn't working not iterating (going through list) checking in range of 0 size of list, basically, 0, 1, 2, 3, 4, 5, ..., n. iterate through list have use for temp_variable in list_name: see example.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -