Skip to content

find if value is sum of two ints in a array #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions others/arrays/set_col_rows_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def set_cols_rows_to_zero(m):
len_rows = len(m)
len_cols = len(m[0])
list_pos = []

# finding zeros in the matrix
for i in range(len_rows):
for j in range(len_cols):
if m[i][j] == 0:
list_pos.append([i,j])

# replacing rows and cols by zeros
for pos in list_pos:
for j in range(len_cols): # add zeros on row
m[pos[0]][j] = 0
for i in range(len_rows):
m[i][pos[1]] = 0
return m

test = [[1,2,3], [4,0,6], [1,3,8]]
test2 =[
[1, 5, 45, 0, 81],
[6, 7, 2, 82, 8],
[20, 22, 49, 5, 5],
[0, 23, 50, 4, 92],
]
r = set_cols_rows_to_zero(test2)
print(r)
22 changes: 22 additions & 0 deletions others/arrays/sum_2_int_equal_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Given an array of integers and a value, determine if there are any two integers in the array whose sum is equal to the given value.
Return true if the sum exists and return false if it does not.
"""

def sum_of_two_exist(a, value):
existing_numbers = set()
for n in a:
if (value - n) in existing_numbers:
return True
existing_numbers.add(n)
return False

a = [5,7,1,2,8,4,3]
test = [10,3,20,1,2,7]

for v in test:
equal = sum_of_two_exist(a, v)
if equal:
print("Value {} exist on sum of two ints in array {}".format(v, a))
else:
print("Value {} DOES NOT exist on sum of two ints in array {}".format(v, a))