Scripting Helpers is winding down operations and is now read-only. More info→
← Blog Home

Snack Break 25: Arrays in Higher Dimensions

Hey guys! It's been a while! Sadly, I don't have the submissions for the last Snack Break, so we're starting fresh again.

Snack Breaks for here on out will be regularly updated! Be sure to check the schedule for the exact dates.


As a reminder, here are some rules and info about Snack Break:

  • Snack Breaks are short problems that can be completed in as little as 15 minutes.
    • Don't worry if you need more time. Programming is not easy!
  • Each Snack Break will last for 12 days.
    • This gives me time to actual review the submissions before posting the next Snack Break!
  • Everyone that successfully completes a Snack Break will be rewarded with the Snack Break badge.
  • Pre-existing code is allowed, as long as you wrote it.
    • Basically, no free model submissions. There are benefits to reusing code and writing it all from scratch. If you always use one method, try using the other!
  • Push yourself!
    • Try the bonuses: you might find them easier than they first appear!
  • Use the ROBLOX Lua API!
    • No challenge will forbid any ROBLOX-provided API, although some bonuses might. Use all the resources you have available!
  • Asking for help is okay.
    • We won't be revealing our answers until the next Snack Break, though!

And most importantly of all:

  • HAVE FUN
    • These challenges are meant to expose you to concepts you might not come across naturally. If you find them too hard or too much work, don't stress yourself out trying to solve them! The answers will always be released with the next Snack Break.

Without further ado, let's begin!


Snack Break 25: Arrays in Higher Dimensions

This Snack Break is brought to you by cntkillme!

Challenge

Write a function that takes a list of integers and returns a multi-dimensional Table where each dimension is the size of the corresponding integer in the list. Initialize the data in the Table to 0.

Here's a function prototype to get started:

function DimensionTable(sizes)
    local dimensions = #sizes
    local t = {}

    -- Write your code here!

    return t
end

Constraints

Where N is the number of dimensions (the size of the list) and D is the size of a dimension (the values in the list):

0 < N < inf
0 < D < inf for all of N

The size for a given dimension should be accessible by using the # operator on the relevant sub-Table.

The Table should able to be iterated over using pairs or ipairs in a generic for loop.

Example

local map = DimensionTable( {5, 1, 3, 4} )
-- Construct a 4-dimensional (4D) array of size 5x1x3x4
print( map[1][1][1][1] ) --> 0
-- Print the first value
print( map[5][1][3][4] ) --> 0
-- Print the last value
print( #map ) --> 5
-- Print size of the top dimension
print( #map[1][1][1] ) --> 4
-- Print size of the bottom dimension

Bonuses

  • Use "varargs" instead of a Table as the function parameter.
  • Specify the value to initialize the data in the Table with.
    • Do this challenge with the first one!
  • If you solved this challenge using recursion, try solving it without!
    • Conversely, if you solved this without recursion, try it with!
  • Optimize!
    • The fastest function to generate a Table with dimensions of 9, 9, 9, 9, 9, 9, 9, 9, 9 wins this one!
    • Hint: metatables might help here, if you're clever.
    • I will be testing that all Values exist, and that Values that should be nil are, but only the time it takes to create the Table will be recorded!
    • Don't be cheeky and treat that input specially by returning a pre-generated Table. Do that and you're disqualified from this bonus!

Submissions

The best way to submit your code is to PM me a link to an un-copylocked ROBLOX Place or Free Model that I can take.

You can also @adark in the Scripting Helpers Discord when I'm online!

The deadline for this Snack Break is 11:59PM UTC on Sunday, September 3rd, and the "winners" will be announced with the next Snack Break the following Friday!


About the Author

Valentine Albee - ROBLOX Developer and Professional Programmer

Feel free to shoot me a message on Discord!
Ideas and questions are always welcome. :)

Posted in Uncategorized

Commentary

Leave a Comment

CootKitty says: August 31, 2017
Sounds like fun.