Created
October 13, 2020 20:33
-
-
Save gkspranger/e852f512823ecb442428f58443208bf9 to your computer and use it in GitHub Desktop.
crystal attempt at multidimensional array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# start nested array if i==100 | |
# end nested array if i==200 | |
flat_tokens = [1, 2, 100, 3, 4, 200, 5, 100, 6, 7, 200, 8] | |
alias MYARR = Array(Array(Array(Int32) | Int32) | Array(Int32) | Int32) | |
def parse(tokens, list : MYARR) | |
return {tokens, list} if tokens.empty? | |
token = tokens.shift | |
if token == 100 | |
puts "START nested" | |
remaining_tokens, nested_list = parse(tokens, MYARR.new) | |
puts "current list: #{list} #{list.class}" | |
puts "nested list: #{nested_list} : #{nested_list.class}" | |
puts "remaining tokens={#{remaining_tokens} : #{remaining_tokens.class}}" | |
# list.push nested_list | |
parse remaining_tokens, list | |
elsif token == 200 | |
puts "END nested" | |
return {tokens, list} | |
else | |
puts "token={#{token} : #{token.class}}" | |
list.push token | |
end | |
parse tokens, list | |
end | |
puts "flat tokens={#{flat_tokens} : #{flat_tokens.class}}" | |
mytree = parse(flat_tokens, MYARR.new) | |
puts mytree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment