Is there any way to find if the index is equal to the element of a vector?Best way to find if an item is in a JavaScript array?Finding the index of an item given a list containing it in PythonIs there any way to kill a Thread?How to find out if an item is present in a std::vector?How to remove an element from a list by index?How do I erase an element from std::vector<> by index?What is the easiest way to initialize a std::vector with hardcoded elements?Is there a simple way to delete a list element by value?Is there an R function for finding the index of an element in a vector?Swift for loop: for index, element in array?

Does academia have a lazy work culture?

How may I concisely assign different values to a variable, depending on another variable?

What is the difference between 1/3, 1/2, and full casters?

Explanation for a joke about a three-legged dog that walks into a bar

Why is a dedicated QA team member necessary?

How can I prevent corporations from growing their own workforce?

Trapped in an ocean Temple in Minecraft?

401(k) investment after being fired. Do I own it?

How do I stop my characters falling in love?

powerhouse of ideas

How do I address my Catering staff subordinate seen eating from a chafing dish before the customers?

What is the meaning of "you has the wind of me"?

Invert Some Switches on a Switchboard

Is my employer paying me fairly? Going from 1099 to W2

Which Roman general was killed by his own soldiers for not letting them to loot a newly conquered city?

How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?

Is it correct to translate English noun adjuncts into adjectives?

How can I make sure my players' decisions have consequences?

How do professional electronic musicians/sound engineers combat listening fatigue?

kids pooling money for Lego League and taxes

What exactly makes a General Products hull nearly indestructible?

Why did Saturn V not head straight to the moon?

What should I say when a company asks you why someone (a friend) who was fired left?

How do we explain the E major chord in this progression?



Is there any way to find if the index is equal to the element of a vector?


Best way to find if an item is in a JavaScript array?Finding the index of an item given a list containing it in PythonIs there any way to kill a Thread?How to find out if an item is present in a std::vector?How to remove an element from a list by index?How do I erase an element from std::vector<> by index?What is the easiest way to initialize a std::vector with hardcoded elements?Is there a simple way to delete a list element by value?Is there an R function for finding the index of an element in a vector?Swift for loop: for index, element in array?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Using the methodology of Divide and Conquer, I'm trying to do an algorithm that compares if the index of a vector is equal to the element in this position. I've seen that my code is inefficient in large vectors, so I've been thinking to do it by dividing the vector in two halves, but I do not know how to do it...



def indicePosicion (inicio, lista): 
if lista == []:
return -1
elif int(lista[0]) == inicio:
return inicio
else:
return indicePosicion(inicio+1, lista[1:])

numero = int(input())
lista = input().split()
print(indicePosicion(0, lista))


I introduce the number of elements in the vector:
7
Introduce the elements separate by spaces:
-3 -1 2 5 6 7 9
And the output should be
2
where the element is equal to the position










share|improve this question






























    0















    Using the methodology of Divide and Conquer, I'm trying to do an algorithm that compares if the index of a vector is equal to the element in this position. I've seen that my code is inefficient in large vectors, so I've been thinking to do it by dividing the vector in two halves, but I do not know how to do it...



    def indicePosicion (inicio, lista): 
    if lista == []:
    return -1
    elif int(lista[0]) == inicio:
    return inicio
    else:
    return indicePosicion(inicio+1, lista[1:])

    numero = int(input())
    lista = input().split()
    print(indicePosicion(0, lista))


    I introduce the number of elements in the vector:
    7
    Introduce the elements separate by spaces:
    -3 -1 2 5 6 7 9
    And the output should be
    2
    where the element is equal to the position










    share|improve this question


























      0












      0








      0








      Using the methodology of Divide and Conquer, I'm trying to do an algorithm that compares if the index of a vector is equal to the element in this position. I've seen that my code is inefficient in large vectors, so I've been thinking to do it by dividing the vector in two halves, but I do not know how to do it...



      def indicePosicion (inicio, lista): 
      if lista == []:
      return -1
      elif int(lista[0]) == inicio:
      return inicio
      else:
      return indicePosicion(inicio+1, lista[1:])

      numero = int(input())
      lista = input().split()
      print(indicePosicion(0, lista))


      I introduce the number of elements in the vector:
      7
      Introduce the elements separate by spaces:
      -3 -1 2 5 6 7 9
      And the output should be
      2
      where the element is equal to the position










      share|improve this question
















      Using the methodology of Divide and Conquer, I'm trying to do an algorithm that compares if the index of a vector is equal to the element in this position. I've seen that my code is inefficient in large vectors, so I've been thinking to do it by dividing the vector in two halves, but I do not know how to do it...



      def indicePosicion (inicio, lista): 
      if lista == []:
      return -1
      elif int(lista[0]) == inicio:
      return inicio
      else:
      return indicePosicion(inicio+1, lista[1:])

      numero = int(input())
      lista = input().split()
      print(indicePosicion(0, lista))


      I introduce the number of elements in the vector:
      7
      Introduce the elements separate by spaces:
      -3 -1 2 5 6 7 9
      And the output should be
      2
      where the element is equal to the position







      python arrays vector indexing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 18:15









      Born Tbe Wasted

      57313 bronze badges




      57313 bronze badges










      asked Mar 26 at 17:10









      Lucas VázquezLucas Vázquez

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          0














          How about just getting the list of indices where the index is equal to the element?



          a = [1, 2, 3, 3, 4, 4, 6, 6]
          [index for index, element in enumerate(a) if index == element]
          #gives you a list of indices of `a` with a value equal to the index.





          share|improve this answer






























            0















            "I've seen that my code is inefficient in large vector"




            If the input array is not sorted, then the most efficient code will have a time complexity of O(n) since we have to iterate element by element and compare the index to the value. In this case, using divide and conquer is meaningless and is only adding complexity.



            If the input array is sorted, then you can use a modified version of binary search to achieve an O(logn) time complexity.






            share|improve this answer



























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362712%2fis-there-any-way-to-find-if-the-index-is-equal-to-the-element-of-a-vector%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              How about just getting the list of indices where the index is equal to the element?



              a = [1, 2, 3, 3, 4, 4, 6, 6]
              [index for index, element in enumerate(a) if index == element]
              #gives you a list of indices of `a` with a value equal to the index.





              share|improve this answer



























                0














                How about just getting the list of indices where the index is equal to the element?



                a = [1, 2, 3, 3, 4, 4, 6, 6]
                [index for index, element in enumerate(a) if index == element]
                #gives you a list of indices of `a` with a value equal to the index.





                share|improve this answer

























                  0












                  0








                  0







                  How about just getting the list of indices where the index is equal to the element?



                  a = [1, 2, 3, 3, 4, 4, 6, 6]
                  [index for index, element in enumerate(a) if index == element]
                  #gives you a list of indices of `a` with a value equal to the index.





                  share|improve this answer













                  How about just getting the list of indices where the index is equal to the element?



                  a = [1, 2, 3, 3, 4, 4, 6, 6]
                  [index for index, element in enumerate(a) if index == element]
                  #gives you a list of indices of `a` with a value equal to the index.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 17:47









                  Samuel NdeSamuel Nde

                  9568 silver badges14 bronze badges




                  9568 silver badges14 bronze badges























                      0















                      "I've seen that my code is inefficient in large vector"




                      If the input array is not sorted, then the most efficient code will have a time complexity of O(n) since we have to iterate element by element and compare the index to the value. In this case, using divide and conquer is meaningless and is only adding complexity.



                      If the input array is sorted, then you can use a modified version of binary search to achieve an O(logn) time complexity.






                      share|improve this answer





























                        0















                        "I've seen that my code is inefficient in large vector"




                        If the input array is not sorted, then the most efficient code will have a time complexity of O(n) since we have to iterate element by element and compare the index to the value. In this case, using divide and conquer is meaningless and is only adding complexity.



                        If the input array is sorted, then you can use a modified version of binary search to achieve an O(logn) time complexity.






                        share|improve this answer



























                          0












                          0








                          0








                          "I've seen that my code is inefficient in large vector"




                          If the input array is not sorted, then the most efficient code will have a time complexity of O(n) since we have to iterate element by element and compare the index to the value. In this case, using divide and conquer is meaningless and is only adding complexity.



                          If the input array is sorted, then you can use a modified version of binary search to achieve an O(logn) time complexity.






                          share|improve this answer
















                          "I've seen that my code is inefficient in large vector"




                          If the input array is not sorted, then the most efficient code will have a time complexity of O(n) since we have to iterate element by element and compare the index to the value. In this case, using divide and conquer is meaningless and is only adding complexity.



                          If the input array is sorted, then you can use a modified version of binary search to achieve an O(logn) time complexity.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 26 at 17:49

























                          answered Mar 26 at 17:19









                          DeepSpaceDeepSpace

                          44.1k4 gold badges55 silver badges86 bronze badges




                          44.1k4 gold badges55 silver badges86 bronze badges



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362712%2fis-there-any-way-to-find-if-the-index-is-equal-to-the-element-of-a-vector%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript