mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
simple examples
This commit is contained in:
parent
52fdba36de
commit
35e2780fea
2 changed files with 93 additions and 0 deletions
58
src/extra_interview_problems/math_arrays_and_strings/find_largest_sum.py
Executable file
58
src/extra_interview_problems/math_arrays_and_strings/find_largest_sum.py
Executable file
|
@ -0,0 +1,58 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
'''You are given an array of integers (both positive and negative). Find the contiguous
|
||||||
|
sequence with the largest sum. Return the sum.'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_largest_sum(array):
|
||||||
|
'''
|
||||||
|
>>> find_largest_sum([-1, 2, -3, 5, 3, 1, -16, 7, 1, -13, 1])
|
||||||
|
9
|
||||||
|
>>> find_largest_sum([])
|
||||||
|
0
|
||||||
|
>>> find_largest_sum([1])
|
||||||
|
1
|
||||||
|
'''
|
||||||
|
if not array: return 0
|
||||||
|
p1, p2 = 0, 1
|
||||||
|
all_sums = set()
|
||||||
|
sum_ = array[p1]
|
||||||
|
|
||||||
|
while p1 < p2 and p2 < len(array):
|
||||||
|
if sum_ + array[p2] < 0:
|
||||||
|
all_sums.add(sum_)
|
||||||
|
p2 += 1
|
||||||
|
p1 = p2
|
||||||
|
sum_ = 0
|
||||||
|
sum_ += array[p2]
|
||||||
|
p2 += 1
|
||||||
|
all_sums.add(sum_)
|
||||||
|
return max(all_sums)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def find_largest_sum_simple(array):
|
||||||
|
'''
|
||||||
|
>>> find_largest_sum_simple([-1, 2, -3, 5, 3, 1, -16, 7, 1, -13, 1])
|
||||||
|
9
|
||||||
|
>>> find_largest_sum_simple([])
|
||||||
|
0
|
||||||
|
>>> find_largest_sum_simple([1])
|
||||||
|
1
|
||||||
|
'''
|
||||||
|
max_sum, sum_ = 0, 0
|
||||||
|
|
||||||
|
for item in array:
|
||||||
|
sum_ += item
|
||||||
|
if max_sum < sum_:
|
||||||
|
max_sum = sum_
|
||||||
|
elif sum_ < 0:
|
||||||
|
sum_ = 0
|
||||||
|
return max_sum
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
35
src/extra_interview_problems/math_arrays_and_strings/find_sum_ints.py
Executable file
35
src/extra_interview_problems/math_arrays_and_strings/find_sum_ints.py
Executable file
|
@ -0,0 +1,35 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
'''Design an algorithm to find all pairs of integers within an array
|
||||||
|
which sum to a specified value.'''
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
def find_ints_sum(array, sum_):
|
||||||
|
'''
|
||||||
|
>>> find_ints_sum([4, 5, 3, 5, 2, 8, 1], 9)
|
||||||
|
set([(5, 4), (1, 8)])
|
||||||
|
>>> find_ints_sum([1, 2], 5)
|
||||||
|
set([])
|
||||||
|
>>> find_ints_sum([1], 1)
|
||||||
|
'Not enough values'
|
||||||
|
'''
|
||||||
|
if len(array)< 2:
|
||||||
|
return "Not enough values"
|
||||||
|
|
||||||
|
dict = defaultdict()
|
||||||
|
pairs = set()
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
if (sum_ - i) in dict:
|
||||||
|
pairs.add((i, sum_ -i))
|
||||||
|
else:
|
||||||
|
dict[i] = True
|
||||||
|
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
# if the array was sorted, we could do this with two pointers
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
Loading…
Add table
Add a link
Reference in a new issue