diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-10-08 12:01:43 +0000 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-10-08 12:01:43 +0000 |
commit | 9824cf33dd963a8ef9798c86bf24659884ff6ef7 (patch) | |
tree | 5bc8b14bd990e35ee1b39c25079e955d4e267a8b | |
parent | 59197e3cad8623a72c6fe4d806fe5413166e4794 (diff) | |
download | cs-y13-9824cf33dd963a8ef9798c86bf24659884ff6ef7.tar.gz cs-y13-9824cf33dd963a8ef9798c86bf24659884ff6ef7.tar.bz2 cs-y13-9824cf33dd963a8ef9798c86bf24659884ff6ef7.zip |
feat: add tuples
-rw-r--r-- | lesson1.py | 19 |
1 files changed, 14 insertions, 5 deletions
@@ -8,13 +8,17 @@ def linear_search_while(arr: list, item): found: bool = False index = 0 while not found and index < len(arr): - if (arr[index] == item): found = True + if (arr[index] == item): + found = True + break index += 1 - return found + return (found, index) def linear_search(arr: list, item): + index = 0 for _item in arr: if _item == item: - return True + return (True, index) + index += 1 return False def main(): print("Hello world") @@ -23,7 +27,12 @@ def main(): query = int(input("Your query for item: ")) found1 = linear_search(array, query) found2 = linear_search_while(array, query) - print("Found by for loop method") if found1 else print("Not found.") - print("Found by while loop method") if found2 else print("Not found.") + if found1[0]: + print("Found by for loop method ", found1[1]) + else: print("Not found.") + if found2[0]: + print("Found by while loop method ", found2[1]) + else: + print("Not found.") if __name__ == "__main__": main()
\ No newline at end of file |