mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
63
ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
63
ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def intersection_two_arrays_sets(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_sets([1,2,3,5,7,8], [3,5,6])
|
||||
set([3, 5])
|
||||
>>> intersection_two_arrays_sets([1,2,7,8], [3,5,6])
|
||||
set([])
|
||||
'''
|
||||
# O(n)
|
||||
set1 = set(seq1)
|
||||
set2 = set(seq2)
|
||||
|
||||
return set1.intersection(set2) #same as list(set1 & set2)
|
||||
|
||||
|
||||
def intersection_two_arrays_On2(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_On2([1,2,3,5,7,8], [3,5,6])
|
||||
[3, 5]
|
||||
>>> intersection_two_arrays_On2([1,2,7,8], [3,5,6])
|
||||
[]
|
||||
'''
|
||||
|
||||
final = []
|
||||
|
||||
for i in seq1:
|
||||
for j in seq2:
|
||||
if i == j:
|
||||
final.append(i)
|
||||
|
||||
return final
|
||||
|
||||
|
||||
def intersection_two_arrays_On(seq1, seq2):
|
||||
'''
|
||||
>>> intersection_two_arrays_On([1,2,3,5,7,8], [3,5,6])
|
||||
[5, 3]
|
||||
>>> intersection_two_arrays_On([1,2,7,8], [3,5,6])
|
||||
[]
|
||||
'''
|
||||
|
||||
final = []
|
||||
|
||||
while seq1 and seq2:
|
||||
|
||||
if seq1[-1] == seq2[-1]:
|
||||
final.append(seq1.pop())
|
||||
seq2.pop()
|
||||
elif seq1[-1] > seq2[-1]:
|
||||
seq1.pop()
|
||||
else:
|
||||
seq2.pop()
|
||||
|
||||
return final
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
Loading…
Add table
Add a link
Reference in a new issue