Here is a program for a linear search I wrote in YASS some few months ago.
YASS
function linearSearch($item_list, $search_item) : number | boolean { $found = false $index = 0 $position = -1 while ($index < count($item_list) and $found == false){ if ($item_list[$index] == $search_item){ $found = true $position = $index } $index++ } if($found){ print("Item found at position", $position) } else{ print("Item not found") } } function main(){ $l = [3, 5, 7] linearSearch($l, 6) }
And here it is converted to Python using ZenPy:
Python
def linearSearch(item_list, search_item): found = False index = 0 position = -1 while index < len(item_list) and found == False: if item_list[index] == search_item: found = True position = index index += 1 if found: print("Item found at position", position) else: print("Item not found") def main(): l = [3, 5, 7] linearSearch(l, 5) main()
Powered by DASH 2.0