Skip to main content
Question Protected by Jamal
Tweeted twitter.com/StackCodeReview/status/1015926840748314630
added 5 characters in body
Source Link
Daniel
  • 4.5k
  • 2
  • 17
  • 40

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.: Hackerrank 2d array problem

Sample input is like this;this:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this; 19this:

19

My actual code input from the submission website is as follows;follows:

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different stack overflowStackOverflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un"un-pythonic"Pythonic" and am sure not at all timetimes optimized.How How can iI rewrite this code to be more "Pythonic"? I understand list-comprehensions comprehensions, but how can itthey be applied to Multimulti-dimensional arrays like this case? Also, would re-writingrewriting the code to be pythonicPythonic actually optimize the solution?

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.Hackerrank 2d array problem

Sample input is like this;

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this; 19

My actual code input from the submission website is as follows;

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different stack overflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un-pythonic" and am sure not at all time optimized.How can i rewrite this code to be more "Pythonic"? I understand list-comprehensions, but how can it be applied to Multi-dimensional arrays like this case? Also, would re-writing the code to be pythonic actually optimize the solution?

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake: Hackerrank 2d array problem

Sample input is like this:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this:

19

My actual code input from the submission website is as follows:

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different StackOverflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "un-Pythonic" and am sure not at all times optimized. How can I rewrite this code to be more "Pythonic"? I understand list comprehensions, but how can they be applied to multi-dimensional arrays like this case? Also, would rewriting the code to be Pythonic actually optimize the solution?

edited tags
Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92
deleted 5 characters in body
Source Link

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.Hackerrank 2d array problem

Sample input is like this;

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this; 19

My actual code input from the submission website is as follows;

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2]     [j+1]+arr[i+2][j+2]arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different stack overflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un-pythonic" and am sure not at all time optimized.How can i rewrite this code to be more "Pythonic"? I understand list-comprehensions, but how can it be applied to Multi-dimensional arrays like this case? Also, would re-writing the code to be pythonic actually optimize the solution?

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.Hackerrank 2d array problem

Sample input is like this;

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this; 19

My actual code input from the submission website is as follows;

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2]     [j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different stack overflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un-pythonic" and am sure not at all time optimized.How can i rewrite this code to be more "Pythonic"? I understand list-comprehensions, but how can it be applied to Multi-dimensional arrays like this case? Also, would re-writing the code to be pythonic actually optimize the solution?

I came up with this code as a solution to a contest question. The original question can be viewed here for attribution sake.Hackerrank 2d array problem

Sample input is like this;

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample output is like this; 19

My actual code input from the submission website is as follows;

import math
import os
import random
import re
import sys



if __name__ == "__main__":
    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
        

        
total = 0
max_total = -1073741824


for i in range(len(arr)):
    for j in range(len(arr[i])):
        if (j+2 < 6) and (i+2 < 6):
            total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            if max_total < total:
                
                max_total = total
print(max_total)

There were several code listings on a different stack overflow posting C++/Ruby solutions to 2d array problem. Being an absolute beginner in Python, I am still coming up to speed with advanced concepts in Python so my solution presented here is "Un-pythonic" and am sure not at all time optimized.How can i rewrite this code to be more "Pythonic"? I understand list-comprehensions, but how can it be applied to Multi-dimensional arrays like this case? Also, would re-writing the code to be pythonic actually optimize the solution?

added 106 characters in body
Source Link
Loading
added 16 characters in body
Source Link
Loading
Corrected the heading to comply with Codereview (https://codereview.stackexchange.com/help/how-to-ask). Also formatted the code by pasting in the code that was accepted by the autograder
Source Link
Loading
Source Link
Loading