Skip to content

Q80 Multi Dim Solution #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

Hemanth21k
Copy link
Contributor

@Hemanth21k Hemanth21k commented Jul 7, 2025

Following upon previous conversation, this PR follows on resolve #215 and an added solution for creating a centered subpart.

Question 80: Consider an arbitrary array, write a function that extracts a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary)

Previous Conversation for Context:

Me:

      "
      I have to say I do not quite understand the question nor the solution.
      The existing solution is also not centered.
      So, I am adding my solution according to my understanding of the question.
      step 1: extract a subpart of given shape from an array.
      step 2: pad the subpart so that it can be centered across all existing dimensions.
      Please let me know if this is the correct solution or the correct understanding of the question.
      Thank you. 
      "

Current solution output by author:

Arbitrary array Z: 
      [  [5 7 7 1 6 6 2 6 3 7]
         [3 7 0 1 0 8 5 0 5 7]
         [8 8 8 9 6 8 6 5 8 4]
         [3 7 1 9 2 6 5 9 0 8]
         [4 8 3 6 5 3 8 9 5 1]
         [9 3 6 2 7 7 2 5 1 7]
         [3 2 6 5 3 1 9 7 2 6]
         [5 1 1 3 5 3 8 7 6 2]
         [9 9 7 7 9 8 8 3 0 2]
         [1 9 4 8 2 7 6 7 3 1]  ]

Output centered subpart of the array, R:

    [  [0 0 0 0 0]
       [0 5 7 7 1]
       [0 3 7 0 1]
       [0 8 8 8 9]
       [0 3 7 1 9]  ]    (padded with fill value 0)

As you can see above the output sub array array is clearly not centered and is also fixed, meaning no matter how many times you run you always take the same subpart of the array (indices wise, of course Z is randomly generated every time).

Updated solution (showing multi dimensional array):

Random array of shape (5,5,5):
[[[0.41318587, 0.02686425, 0.27634727, 0.34828896, 0.53552608],
[0.54426485, 0.40573159, 0.32785901, 0.95110226, 0.74314067],
[0.20954932, 0.28623217, 0.70648967, 0.8790684 , 0.58088525],
[0.93627437, 0.9338704 , 0.26930749, 0.28473462, 0.57721401],
[0.26036955, 0.2339317 , 0.41383287, 0.32453434, 0.5999981 ]],

[[0.17264282, 0.09969286, 0.54365084, 0.24587597, 0.63170746],
[0.73230861, 0.61695342, 0.33547145, 0.32364859, 0.30090559],
[0.82022138, 0.25265008, 0.20509734, 0.79525084, 0.69119536],
[0.37488242, 0.70970922, 0.49893688, 0.76578014, 0.82251079],
[0.02927624, 0.5578041 , 0.72246968, 0.23032316, 0.82340544]],

[[0.52939221, 0.35862534, 0.68652225, 0.17703581, 0.79556611],
[0.628605 , 0.49367779, 0.92209193, 0.30016312, 0.64213181],
[0.04117666, 0.2536342 , 0.70921765, 0.00874844, 0.50106527],
[0.47607017, 0.61145768, 0.85605809, 0.3551498 , 0.15807602],
[0.08091961, 0.02181234, 0.35937453, 0.2406722 , 0.05558978]],

[[0.62017971, 0.64246925, 0.28136912, 0.30680361, 0.80700583],
[0.01607795, 0.30556255, 0.84313362, 0.21627144, 0.81764976],
[0.12892675, 0.14246595, 0.59990127, 0.88475525, 0.4042635 ],
[0.07722488, 0.07539892, 0.65651728, 0.19927838, 0.73940527],
[0.89474121, 0.24049369, 0.97836756, 0.21814119, 0.09843175]],

[[0.31122096, 0.82433984, 0.3487815 , 0.34754057, 0.60063218],
[0.99627488, 0.21458165, 0.10521583, 0.31445038, 0.94451234],
[0.95148131, 0.60257491, 0.18610522, 0.87703271, 0.62835079],
[0.35963395, 0.69448514, 0.47953283, 0.64439803, 0.5633606 ],
[0.17343141, 0.69441931, 0.02642798, 0.92214243, 0.65579668]]]

Output - a random centered subpart of shape (2,3,2):
[[[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]

[[0. 0. 0. 0. ]
[0. 0.17264282 0.09969286 0. ]
[0. 0.73230861 0.61695342 0. ]
[0. 0.82022138 0.25265008 0. ]
[0. 0. 0. 0. ]]

[[0. 0. 0. 0. ]
[0. 0.52939221 0.35862534 0. ]
[0. 0.628605 0.49367779 0. ]
[0. 0.04117666 0.2536342 0. ]
[0. 0. 0. 0. ]]

[[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]] (padded with fill value 0)


The updated solution not only takes a random subpart of a given shape, but also centers it evenly across multiple dimensions.

Please correct me if I am wrong but this was my understanding of the given question.

Thank you.

@rougier
Copy link
Owner

rougier commented Jul 10, 2025

I've edited your comment to ease reading of outputs. Can you also update the second part using only integers because it make things easier to read and debug.

For the first example you give, can you five the full script in another comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exercise N62 - There is an unnecessary code, Exercise N80 - IndexError
2 participants