Largest product out of 4 in a grid - python
I'm doing some project euler problems to practice more. I am at problem 11 and I keep getting the wrong answer.
Here is the list of lists:
problem_11 = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
]
Here is the code:
def largest_product_grid(grid):
greatest = 0
patterns = [
[(1, 0), (2, 0), (3, 0)],
[(1, -1), (2, -2), (3, -3)],
[(0, -1), (0, -2), (0, -3)],
[(0, 1), (0, 2), (0, 3)],
[(-1, -1), (-2, -2), (-3, -3)]]
for y in range(0, 19):
for x in range(0, 19):
for p in patterns:
product = grid[y][x] * grid[p[0][1]][p[0][0]] * grid[p[1][1]][p[1][0]] * grid[p[2][1]][p[2][0]]
if greatest < product:
greatest = product
return greatest
I'm pretty sure it's doing more calculations than it needs to, doing the same math several times and going further on both x and y than I probably need to go. Yet I still keep getting the wrong answer. The correct should be: 70600674.
Edit: Question:
"What is the greatest product of four adjacent numbers in the same
direction (up, down, left, right, or diagonally) in the 20×20 grid?"
I was expecting index out of range errors, yet I oversaw a stupid error.
For starters, you want your search pattern to be a function of x and y.
product = grid[y][x] * grid[p[0][1]][p[0][0]] * grid[p[1][1]][p[1][0]] * grid[p[2][1]][p[2][0]]
Right now, p[0][1] will always return the same value no matter what x and y are. You should replace the above line with something like:
product = grid[y][x] * grid[y + p[0][1]][x + p[0][0]] * grid[y + p[1][1]][x + p[1][0]] * grid[y + p[2][1]][x + p[2][0]]
Related
Add list as list element Python weird behaviour
I am trying to store the file indexes from a directory in a list variable using Python. This list will be used for a recursive function that will change the content of files, but the list is storing incorrect values. My directory has more than 5k files with the format: ccc-ppp.htm where ccc is the chapter number (from 0 to 24) and ppp is page number, that varies from chapter to chapter. For example: 0-1.htm 0-2.htm 0-3.htm 1-1.htm 1-2.htm ... 1-10.htm 2-1.htm ... 2.158.htm ... 24-100.htm Some chapters have more than 500 pages. My intention is to store in a list of lists: Dir = [[1, 2, 3], [1, 2, 3, ..., 10], [1, 2, ..., 158], ..., [0, 1, 2, ..., 100]] (Note that some chapters start with ...-0.htm My code: import os.path Folder = "C:\MyFoler\\" Ch = 0 Pg = 0 Chapter = [] Dir = [] while Ch <= 24: File = Folder + str(Ch) + "-" + str(Pg) + ".htm" if os.path.exists(File): Chapter.append(Pg) else: print(f"\nChapter {Ch}: {Chapter}") # OBS(1) Dir.append(Chapter) print(f"\nDir: {Dir}") # OBS(2) Chapter.clear() Ch += 1 Pg = -1 print(f"\nDir: {Dir}") # OBS(3) Pg += 1 OBS(1): It prints the list of pages in a chapter (working OK). For example to Chapter 24: Chapter 24: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] OBS(2): It prints a list, with Ch elements, and each element is the Chapter, but it repeats the chapter in all list elements. For example to Ch = 24: Dir: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]] The problem is that it is storing Chapter in all Dir elements, which is unwanted. The wanted result is: Dir[0] = [1, 2, 3] Dir[1] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ... Dir[24] =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] OBS(3): I don't know why the result of this line is: Dir: [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []] It seems that when I clear the Chapter with Chapter.clear(), it clear the contents of Dir. Why? Then, can someone help me to fix the code, please? Thanks in advance for your attention.
You are not properly appending the chapters to the list Dir. I would recommend to do this, first create a dictionary where the number of the chapter is the key and then set he value of each key to be and empty list. Then use a loop to parse the dictionary and append the the page correctly to the corresponding list. dir = {} for chapter in range(0,25): dir[chapter]=[]
You are continuing to append the same list (Chapter) to Dir each time through. Consider the below very simple example, and what happens when we go from using the same list, vs. creating a new one on each loop iteration. >>> a = [] >>> b = [] >>> for i in range(3): ... for j in range(i): ... a.append(j) ... b.append(a) ... >>> b [[0, 0, 1], [0, 0, 1], [0, 0, 1]] >>> >>> b = [] >>> for i in range(3): ... a = [] ... for j in range(i): ... a.append(j) ... b.append(a) ... >>> b [[], [0], [0, 1]] >>>
Sliding window on a 2D NumPy array with axis=0
I have a np.array a such that a.shape = (10, 5) and I want to use a sliding window of size 3 so that the output array b.shape = (8, 15). For example i, j = np.ogrid[:10, :5] a = 10*i + j print(a) array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44], [50, 51, 52, 53, 54], [60, 61, 62, 63, 64], [70, 71, 72, 73, 74], [80, 81, 82, 83, 84], [90, 91, 92, 93, 94]]) Then b should be array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24], [10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34], [20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44], [30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 50, 51, 52, 53, 54], [40, 41, 42, 43, 44, 50, 51, 52, 53, 54, 60, 61, 62, 63, 64], [50, 51, 52, 53, 54, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74], [60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 80, 81, 82, 83, 84], [70, 71, 72, 73, 74, 80, 81, 82, 83, 84, 90, 91, 92, 93, 94]]) I tried numpy.lib.stride_tricks.sliding_window_view but it seems not working for this scenario.
Here you can use one concatenate: i, j = np.ogrid[:9, :5] a = 10*i + j #generate your array b = np.concatenate( [a[:-2,:],a[1:-1,:],a[2:,:]],axis=1) print(b) #returns array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24], [10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34], [20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44], [30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 50, 51, 52, 53, 54], [40, 41, 42, 43, 44, 50, 51, 52, 53, 54, 60, 61, 62, 63, 64], [50, 51, 52, 53, 54, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74], [60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 80, 81, 82, 83, 84]]) Also you can do it for any windows size as windowSize = 3 nRows,nCols = a.shape b =np.concatenate([a[c:nRows-(windowSize-c-1),:] for c in range(windowSize)],axis=1)
Using numpy.lib.stride_tricks.sliding_window_view, simply slide on the flattened array and slice the elements you want: import numpy as np np.lib.stride_tricks.sliding_window_view(a.ravel(), 15)[::a.shape[1]] output: array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24], [10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34], [20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44], [30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 50, 51, 52, 53, 54], [40, 41, 42, 43, 44, 50, 51, 52, 53, 54, 60, 61, 62, 63, 64], [50, 51, 52, 53, 54, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74], [60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 80, 81, 82, 83, 84], [70, 71, 72, 73, 74, 80, 81, 82, 83, 84, 90, 91, 92, 93, 94]])
Converting between ranges of numbers?
I'm using the following method to convert from one Range to another Range of numbers def pos(self, value) : return int( math.floor(self.nitems * ((value - self.vmin)/float(self.vrange)) ) ) the problem is that it is not doing it consistently i.e. for a min-max/range : 10-100/90 nitems : 100 i.e : 10-100 => 0-100 i get gaps : missing : -1,19,29,39,49,....,99,100 In [66]: np.array([ne.pos(x) for x in range(100)]) Out[66]: array([-12, -10, -9, -8, -7, -6, -5, -4, -3, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 97, 98]) Where is my error in the formula ? if there is consistent way to do it with any range ? Is it because of the roundings ? It has to "land" on every value in my target-range! and it has to be sequential. hmm... now that I think about it may be it is not possible when the input range is smaller than the output range, unless I allow for real-numbers ? it seems so, when i try with float no gaps : In [82]: np.array([ne.pos(x) for x in np.linspace(0,101,150)]) Out[82]: array([-12, -11, -10, -9, -9, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 58, 58, 59, 60, 61, 61, 62, 63, 64, 64, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 73, 73, 74, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 83, 83, 84, 85, 86, 86, 87, 88, 89, 89, 90, 91, 92, 92, 93, 94, 95, 95, 96, 97, 98, 98, 99, 100, 101])
It seems as though you are over-complicating this, if all you're trying to do is create a second range of integers that is the same length but different starting point as the first range. a = range(10, 100) len(a) == 90 list(a) == [10, 11, 12, ..., 98, 99] b = range(0, len(a)) len(b) == 90 list(b) == [0, 1, 2, ..., 88, 89] c = range(20, 20 + len(a)) len(c) == 90 list(c) == [20, 21, 22, ..., 108, 109]
Calculate three vectors Pearson Correlation
I want to calculate the Pearson Correlation of three vectors. Like: np.corrcoef(X,Y,Z) Anyone knows how to do that in Python? Any library available? Big thanks!
Create a 2-D array: import numpy as np X = np.random.randint(0,100,100) Y = np.random.randint(0,100,100) Z = np.random.randint(0,100,100) spam = np.array([X,Y,Z]) Then run np.corrcoeff on that: eggs = np.corrcoef(spam) Which gives spam array([[12, 1, 23, 95, 58, 37, 38, 76, 99, 24, 50, 47, 29, 67, 38, 62, 28, 39, 26, 32, 55, 11, 67, 71, 71, 50, 9, 54, 59, 22, 70, 63, 11, 43, 30, 54, 14, 11, 89, 68, 98, 27, 18, 66, 14, 23, 83, 81, 24, 90, 56, 40, 3, 94, 86, 54, 66, 68, 96, 74, 46, 19, 58, 74, 7, 73, 93, 91, 10, 75, 32, 91, 45, 57, 81, 96, 20, 25, 18, 86, 59, 58, 94, 97, 10, 11, 6, 30, 28, 76, 56, 86, 64, 38, 26, 25, 26, 77, 76, 0], [76, 54, 66, 16, 97, 22, 66, 59, 90, 67, 85, 58, 49, 13, 36, 44, 91, 20, 63, 2, 1, 51, 66, 12, 32, 10, 48, 88, 6, 96, 68, 60, 22, 24, 51, 2, 94, 95, 98, 79, 24, 22, 94, 69, 36, 37, 50, 94, 8, 2, 15, 80, 99, 77, 15, 95, 76, 25, 0, 69, 46, 21, 47, 87, 88, 6, 34, 63, 46, 12, 62, 4, 36, 89, 21, 44, 96, 22, 11, 14, 63, 55, 70, 58, 33, 93, 88, 39, 0, 18, 9, 92, 37, 13, 12, 67, 98, 34, 9, 6], [56, 21, 61, 55, 82, 60, 13, 36, 17, 52, 93, 88, 65, 29, 67, 81, 65, 59, 33, 89, 58, 99, 78, 1, 75, 39, 10, 75, 94, 57, 42, 32, 19, 7, 77, 82, 96, 61, 94, 50, 61, 13, 21, 51, 70, 14, 60, 79, 32, 29, 22, 93, 31, 19, 74, 37, 85, 74, 50, 50, 37, 5, 33, 39, 75, 32, 43, 10, 74, 39, 74, 26, 57, 38, 79, 90, 63, 13, 53, 57, 44, 4, 13, 39, 14, 97, 21, 81, 2, 49, 72, 5, 72, 32, 63, 37, 53, 31, 8, 12]]) eggs array([[ 1. , -0.14396307, -0.01107133], [-0.14396307, 1. , 0.17098877], [-0.01107133, 0.17098877, 1. ]])
numpy.correlate vs numpy documentation - is there a contradiction here ? Why is the resulting list reversed ?
I get the following result using numpy's correlate function: In [153]: np.correlate([1],np.arange(100)) Out[153]: array([99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) In [154]: This result seems to be in contradiction with the 90. page of the numpy book : Based on the formula above I would have expected an increasing array 0..99, however, the result is a decreasing array 99..0. Can someone explain what is going on here ? Why does the implementation contradicts the specification ? Why does it make sense to reverse the list ?
Looks like you are expecting the old_behaviour of numpy.correlate. The book you link to is very old (2006), so it looks like numpy.correlate has changed since it was written (it actually changed in numpy v1.4). From the docs for numpy v1.9: old_behavior : bool If True, uses the old behavior from Numeric, (correlate(a,v) == correlate(v,a), and the conjugate is not taken for complex arrays). If False, uses the conventional signal processing definition. In [2]: np.correlate([1],np.arange(100)) Out[2]: array([99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) In [3]: np.correlate([1],np.arange(100),old_behavior=True) Out[3]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]) In [4]: np.correlate(np.arange(100),[1]) Out[4]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]) EDIT On further inspection, I think the difference is due to this line in the old definition: K=len(x)-1 and M=len(y)-1, and we assume K ≥ M (without loss of generality because we can interchange the roles of x and y without effect). So, I believe for your case, in the old definition, it is making y=[1] and x=np.arange(100), because len(x) must be greater than len(y). The new definition does not do that, instead "input arrays are never swapped", so x=[1] and y=np.arange(100). Thus, the differences.