Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find1(arr, tar):
- nrow = len(arr)
- ncol = len(arr[0])
- r, c = nrow - 1, 0
- while (r >= 0 and c < ncol):
- if tar == arr[r][c]:
- return (r + 1, c + 1)
- elif tar > arr[r][c]:
- c += 1
- else:
- r -= 1
- return None
- def find2(arr, tar):
- nrow = len(arr)
- ncol = len(arr[0])
- r, c = 0, ncol - 1
- while (r < nrow and c >= 0):
- if tar == arr[r][c]:
- return (r + 1, c + 1)
- elif tar > arr[r][c]:
- r += 1
- else:
- c -= 1
- return None
- int_str = """
- 1 4 7 11 15
- 2 5 8 12 19
- 3 6 9 16 22
- 10 13 14 17 24
- 18 21 23 26 30
- """
- int_arr = [list(map(int, line.split())) for line in int_str.strip().split('\n')]
- print(find1(int_arr, 1), find2(int_arr, 1))
- print(find1(int_arr, 12), find2(int_arr, 12))
- print(find1(int_arr, 9), find2(int_arr, 9))
- print(find1(int_arr, 100), find2(int_arr, 100))
Add Comment
Please, Sign In to add comment