{"id":391,"date":"2021-03-16T13:55:32","date_gmt":"2021-03-16T13:55:32","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=391"},"modified":"2021-06-24T09:25:38","modified_gmt":"2021-06-24T08:25:38","slug":"linear-search","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/linear-search\/","title":{"rendered":"Linear Search"},"content":{"rendered":"<p>Search through a list of values, looking for either:<\/p>\n<ul>\n<li>if a target value is in the list<\/li>\n<li>the location (index) of a target value in the list<\/li>\n<\/ul>\n<h1>General Algorithm<\/h1>\n<p>A result of -1 is used to indicate &#8220;not found&#8221;.<\/p>\n<pre>found = -1\nfor each item in list\n    if item = target value then\n        found = index value\n    end if\nnext item<\/pre>\n<h1>Find if a value is in a list<\/h1>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\ntarget = &quot;fred&quot;\r\n \r\nfound = False\r\nfor name in names: \r\n    if name == target: \r\n        found = True\r\n        break # can stop searching\r\n \r\nif found: \r\n    print(f&quot;{target} was found&quot;}\r\nelse: \r\n    print(f&quot;{target} was not found&quot;}\r\n# <\/pre>\n<h1>Find location of a value in a list<\/h1>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\ntarget = &quot;fred&quot;\r\n \r\nfound = -1\r\nfor name in range(20):\r\n    if names&#x5B;name] == target:\r\n        found = name\r\n        break # can stop searching\r\n \r\nif found == -1:\r\n    print(f&quot;{target} was not in list&quot;}\r\nelse:\r\n    print(f&quot;{names&#x5B;found]} had a mark of {mark&#x5B;found]}&quot;)\r\n#\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Search through a list of values, looking for either: if a target value is in the list the location (index) of a target value in the list General Algorithm A result of -1 is used to indicate &#8220;not found&#8221;. found = -1 for each item in list if item = target value then found = index value end if next item Find if a value is in a list #<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/linear-search\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":330,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-391","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/391\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/391\/revisions\/667"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/330"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}