First commit of partial RosettaCode contents.
Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
commit
1e05ecd7ee
781 changed files with 9080 additions and 0 deletions
9
Task/Search_a_list/Python/search_a_list-2.py
Normal file
9
Task/Search_a_list/Python/search_a_list-2.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
>>> haystack=["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
|
||||
>>> haystack.index('Bush')
|
||||
4
|
||||
>>> haystack.index('Washington')
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#95>", line 1, in <module>
|
||||
haystack.index('Washington')
|
||||
ValueError: list.index(x): x not in list
|
||||
>>>
|
||||
13
Task/Search_a_list/Python/search_a_list-3.py
Normal file
13
Task/Search_a_list/Python/search_a_list-3.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
>>> def hi_index(needle, haystack):
|
||||
return len(haystack)-1 - haystack[::-1].index(needle)
|
||||
|
||||
>>> # Lets do some checks
|
||||
>>> for n in haystack:
|
||||
hi = hi_index(n, haystack)
|
||||
assert haystack[hi] == n, "Hi index is of needle"
|
||||
assert n not in haystack[hi+1:], "No higher index exists"
|
||||
if haystack.count(n) == 1:
|
||||
assert hi == haystack.index(n), "index == hi_index if needle occurs only once"
|
||||
|
||||
|
||||
>>>
|
||||
7
Task/Search_a_list/Python/search_a_list.py
Normal file
7
Task/Search_a_list/Python/search_a_list.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
haystack=["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
|
||||
|
||||
for needle in ("Washington","Bush"):
|
||||
try:
|
||||
print haystack.index(needle), needle
|
||||
except ValueError, value_error:
|
||||
print needle,"is not in haystack"
|
||||
Loading…
Add table
Add a link
Reference in a new issue