HackerRank Day 11: 2D Arrays | Python

preview_player
Показать описание
In this series, I will walk you through Hacker Rank’s 30 days of code challenge day by day.

In Day 11, we will get more familiar with Arrays by working with 2D arrays.

Try solving it yourself!

View my solution for Day 11 at

Join our LinkedIn Group to ask questions and learn from others.

Support me on Patreon!

#OTSC #HackerRank #Python
Рекомендации по теме
Комментарии
Автор

I wonder how many people quit programming when they realized this problem was labeled "EASY."

genjioto
Автор

Outstanding!! I learn here to validate constraint, before i'm doing in different way.

SMARTPALASH
Автор

no way this problem is easy. If someone is new to python they definitely cant solve this problem without learning about 2d arrays.

helloworld
Автор

after 1 hour figuring out what it means i have fully understood it thanks for the tutorial !

nguyenlongvu
Автор

I want to be think like you and more than you. I am completely beginner to python can't even understand what you are writing .... But i will learn . may it take more days months. But i am willing to learn. And i want to say to you... "THANKS FOR TEACHING"

ultraeye
Автор

superb!! approach...
did a little bit of code rearrangement

import math
import os
import random
import re
import sys

# the hourglassSum function .
def hourglassSum(arr):
# subset hourgalss sum function
def get_hourgalss_sum(matrix, row, col):
summ = 0
summ = summ + matrix[row-1][col-1] #current sum + top left element
summ = summ + matrix[row-1][col] #current sum + element above the arr[i, j]
summ = summ + matrix[row-1][col+1] #current sum + top right element
summ = summ + matrix[row][col] #current sum + middle element of hg/arr[i, j]
summ = summ + matrix[row+1][col-1] #current sum + bottom left element of hg
summ = summ + matrix[row+1][col] #current sum + element below the arr[i, j]
summ = summ + matrix[row+1][col+1] #current sum + bottom right element
return summ

max_hourglass_sum = -63 #minimum value of an element a
#looping through the 2d array
for i in range(1, len(arr)-1):
for j in range(1, len(arr)-1):
current_hourglass_sum = get_hourgalss_sum(arr, i, j)
if current_hourglass_sum > max_hourglass_sum:
max_hourglass_sum = current_hourglass_sum
return max_hourglass_sum



if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

arr = []

for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))

result = hourglassSum(arr)

fptr.write(str(result) + '\n')

fptr.close()

pavanpatro
Автор

Solved my dilemma...thanks man! Great work and explanation was way better..

ekanshmishra
Автор

Just for the record this solution is very specific to the constraints outlined in the problem. Maybe I make my life harder but when I solve these problems I do not hard-code anything into the solution so when I solved this I could handle the matrix of any size be it 6x6 or 20x20 and I really don't care what integer is in each value of the matrix. Maybe that's why the problem is classified as easy while it takes me a while to solve it.

CosmoZooo
Автор

Thank you for a great video. For the max_hourglass_sum variable, could you explain how you get it in another way. I see in the matrix in the example that there are 7 negative nines, which makes 63 but I am getting how you get your answer. Please help.

lonwabozaula
Автор

Same logic but updated one line ( without adding each element in the sum )

max_sum = -63

for i in range(1, 5):
for j in range(1, 5):
current_max = arr[i][j] + sum(arr [ i - 1][ j - 1: j + 2]) + sum(arr[ i + 1][ j - 1 : j + 2])
if current_max > max_sum:
max_sum = current_max

print(max_sum)

hritikdj
Автор

my crude solution (it works even if it looks ugly af)

def hourglassSum(arr):
xx=[]
for i in range(0, 4):
for j in range(0, 4):
x = + arr[i+1][j+1] +
xx.append(x)

return max(xx)

spearchew
Автор

Can you explain me the get_hourglass_sum method I don't understand the row and column that you are using like [row-1][col-1]. If you use only row-1 and col-1 what about other rows and columns where the hourglass pattern gets involved. Sorry if this is a silly question.

immanuelsamuelgwu
Автор

My idea without restrictions


A=arr
x, y=len(A), len(A[0])

for j in range (y-2):
for i in range(x-2):
section=[A[z+j][i:i+3] for z in range(3)]
section[1][0], section[1][2]=0, 0
for i in range(3)])

if i==0 and j==0:
suma=sum_section
if sum_section>suma:
suma=sum_section


print(suma)

horace
Автор

Hello! Will you continue posting these videos?

elenakorshunova
Автор

sir i did this without the function, i wrote the sum logic in the nested for block itself but it's not showing the sum of all the hourglasses, please help!

tiksharajput
Автор

have you used kadane's algorithm here?

ryanmanchikanti
Автор

Hi thank you for the tutorial.. It was nice and informative..

abishekkirubanand
Автор

i came up with something pretty similar to this but i feel like there has to be a cleaner way to do the addition of the hour glass

jacob.peters
Автор

thank you for posting this. late to the party, but why did you start your function with _? is it supposed to indicate something?

irynayavorska
Автор

I don't get how you added them. Does your row col start in the middle of the hour glass? If so, how? Can someone also explain range (1, 5)? Shouldn't that be range6?

dorianholmes
welcome to shbcf.ru