List Unflattening

List unflattening refers to converting a flat linear list into a nested list structure with sublists. This is the inverse of list flattening.

For example, [1,2,3,4,5,6] can be unflattened to [[1,2],[3,4],[5,6]]. The flat list is broken down into sublists of a particular size.

Unflattening helps organize data that is easier to process in hierarchical or multidimensional forms rather than one single list.

Solution

Here is how list unflattening can be implemented:

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import java.util.*;

class Main {
  public static List<List<Integer>> unflatten(List<Integer> flatList, int size) {
    List<List<Integer>> nestedList = new ArrayList<>();

    for(int i=0; i<flatList.size(); i+=size) {
      nestedList.add(flatList.subList(i, Math.min(i+size, flatList.size())));
    }
    return nestedList;  
  }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <vector>
using namespace std;

vector<vector<int>> unflatten(vector<int>& flatVec, int size) {
  vector<vector<int>> nestedVec;
  
  for (int i=0; i<flatVec.size(); i+=size) {
    nestedVec.push_back(vector<int>(flatVec.begin()+i, flatVec.begin()+min(i+size, flatVec.size())));
  }

  return nestedVec;
}

Python

1
2
3
4
5
6
7
def unflatten(flat_list, size):
  
  nested_list = []
  for i in range(0, len(flat_list), size):
    nested_list.append(flat_list[i:i+size])

  return nested_list 

The key steps are to iterate through flat list in chunks of given size and append each chunk as sublists to build the nested structure.

Unflattening provides an easy way to organize flat data into hierarchical forms.

Description: List Unflattening

List unflattening is the opposite of list flattening. In this process, a flat list is transformed into a nested, multi-level list based on certain conditions or structures. The main use-case for this is when you have flattened data that you want to reorganize into its original hierarchical structure.

Solution:

Here are implementations to demonstrate list unflattening in Java, C++, and Python.

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListUnflattening {
    public static List<Object> unflatten(List<Integer> flatList, int[] levels) {
        List<Object> nestedList = new ArrayList<>();
        int i = 0;
        for (int level : levels) {
            List<Object> currentList = new ArrayList<>();
            while (level-- > 0) {
                currentList.add(flatList.get(i++));
            }
            nestedList.add(currentList);
        }
        return nestedList;
    }

    public static void main(String[] args) {
        List<Integer> flatList = Arrays.asList(1, 2, 3, 4, 5, 6);
        int[] levels = {2, 1, 3};
        
        List<Object> nestedList = unflatten(flatList, levels);
        System.out.println(nestedList);
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>

std::vector<std::vector<int>> unflatten(std::vector<int> flatList, std::vector<int> levels) {
    std::vector<std::vector<int>> nestedList;
    int i = 0;
    for (int level : levels) {
        std::vector<int> currentList;
        while (level-- > 0) {
            currentList.push_back(flatList[i++]);
        }
        nestedList.push_back(currentList);
    }
    return nestedList;
}

int main() {
    std::vector<int> flatList = {1, 2, 3, 4, 5, 6};
    std::vector<int> levels = {2, 1, 3};
    
    auto nestedList = unflatten(flatList, levels);
    for (auto& sublist : nestedList) {
        for (int num : sublist) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def unflatten(flat_list, levels):
    nested_list = []
    i = 0
    for level in levels:
        current_list = []
        while level > 0:
            current_list.append(flat_list[i])
            i += 1
            level -= 1
        nested_list.append(current_list)
    return nested_list

if __name__ == "__main__":
    flat_list = [1, 2, 3, 4, 5, 6]
    levels = [2, 1, 3]
    
    nested_list = unflatten(flat_list, levels)
    print(nested_list)

Key Takeaways

  • Unflattening is used to revert a flattened list to a nested structure based on specific rules or indices.
  • This is often implemented with the use of loop constructs and conditionals to generate the nested structure.
  • The implementations in Java, C++, and Python show how to unflatten a list based on the number of elements that should be in each nested sublist.