mirror of
https://github.com/ossu/computer-science.git
synced 2025-08-11 15:50:14 -04:00
search pattern in 1.1.3
This commit is contained in:
parent
575e44c837
commit
4570e40f5c
2 changed files with 18 additions and 0 deletions
Binary file not shown.
|
@ -33,4 +33,22 @@ def sumEvens(items):
|
|||
total += items[i]
|
||||
return total
|
||||
|
||||
# The search pattern
|
||||
def find(target, items): # is target in items?
|
||||
return occurrences(target, items) > 0
|
||||
|
||||
def occurrences(target, items):
|
||||
count = 0
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] == target:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
## search with short-circuit
|
||||
def find(target, items):
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] == target:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue