diff --git a/others/arrays/set_col_rows_zero.py b/others/arrays/set_col_rows_zero.py new file mode 100644 index 0000000..5594311 --- /dev/null +++ b/others/arrays/set_col_rows_zero.py @@ -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) diff --git a/others/arrays/sum_2_int_equal_value.py b/others/arrays/sum_2_int_equal_value.py new file mode 100644 index 0000000..f088f81 --- /dev/null +++ b/others/arrays/sum_2_int_equal_value.py @@ -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))