Skip to main content

Flattening An Array

How do you flatten an array?  That is, how do you take something like this:

[1, [2, [3, [4]]]]

and change it into something like this?

[1, 2, 3, 4]

The first thing that occurs to me is that you would go through each element of the given array and check to see whether it is an array.  So we go through:

1 (NO)
[2, [3, [4]]] (YES)

We're done with the non-arrays -- we can keep them somewhere (another array) but the arrays require further processing.  We process them in exactly the way we processed the first array.  The trick is figuring out how to do that organically: we need the computer to keep processing elements of the array and members of those members recursively (!) on down to the elements.

What I think you would do is create a general function that processes an array into elements, then call that function not only on the array itself, but on the members of that array.  How would that look?

Well, if we have an element, we don't call it.  But if we have an array, we do call it.  It gives us back a result that we can parse through, saving the good stuff and continuing to process the bad stuff.  So for that second element processing would look like this:

[2, [3, [4]]] -- Call =>
2, [3, [4]] -- Keep 2 =>
[3, [4]] -- Call =>
3, [4] -- Keep 3 =>
[4] -- Call
4 -- Keep 4
DONE!

So in short, the function returns an array, which we loop through, keeping all the items that are non-arrays and processing the rest.  How does it return that array?  It just loops through the items and stores them, I guess -- nothing fancy.

I'll update as I think more about this -- I think this is on the right track, but I'm not yet convinced it would work.  Maybe there's some way to do it with just loops so that you don't have to bother with recursive functions at all...

Comments

Popular posts from this blog

Getting Geodata From Google's API

The apps I'm going to be analyzing are part of Dr. Charles Severance's MOOC on Python and Databases and work together according to the following structure (which applies both in this specific case and more generally to any application that creates and interprets a database using online data). The data source, in this case, is Google's Google Maps Geocoding API.  The "package" has two components: geoload.py  and geodump.py .  geoload.py  reads a list of locations from a file -- addresses for which we would like geographical information -- requests information about them from Google, and stores the information on a database ( geodata.db ).  geodump.py  reads and parses data from the database in JSON, then loads that into a javascript file.  The javascript is then used to create a web page on which the data is visualized as a series of points on the world-map.  Dr. Severance's course focuses on Python, so I'm only going to work my way through ...

Shell Sort

Today I spent a little bit of time researching the "Shell" sort.  I wanted to post a few notes about the Princeton Algorithms Course's implementation to help me solidify my understanding. First, a little tidbit.  When I first heard about this algorithm, I thought it had something to do with shell games.  Turns out a man named Donald Shell discovered this method of sorting, whence the name. The Algorithms  book gives the following explanation (Sedgewick and Wayne,  Algorithms, 4th ed., p. 258): The idea is to rearrange the array to give it the property that taking every hth entry (starting anywhere) yields a sorted subsequence. Such an array is said to be h-sorted. Put another way, an h-sorted array is h independent sorted subsequences, interleaved together. By h-sorting for some large values of h, we can move items in the array long distances and thus make it easier to h-sort for smaller values of h. Using such a procedure for any sequence of values o...

It's a Date

I guess I should really be putting these things up in GitHub.  The way I see it, the coding journal is just a place to share the code I write or study along with any notes I have about it.  It's sort of a documentation LiveJournal, if you will. Anyway, this is a "study" for my project idea: create an app that will prompt the user for two dates, then calculate the difference between them. The burden of this study is twofold: (1) convert dates in standard American form (e.g. December 15, 1993) into dates in standard American numeric form (e.g. 12/15/1993); (2) create a numerical representation of the date. To process the date, I started with a list of the months.  I then used a loop to create a dictionary that would attach a value to each month. Next I had to parse the user entry (I haven't added any debugging for incorrect entries yet). I did so by splitting the entry into "raw" data.  I used my dictionary to process the month name into a number, str...