Skip to content

Instantly share code, notes, and snippets.

@maximeroussy
Created October 12, 2018 19:40
Show Gist options
  • Save maximeroussy/53c115fc9c3bf8413ccc0ddec7cc2cf9 to your computer and use it in GitHub Desktop.
Save maximeroussy/53c115fc9c3bf8413ccc0ddec7cc2cf9 to your computer and use it in GitHub Desktop.
/**
*
* This class can be used to flatten any Kotlin Array of any type of elements to a one dimensional array.
*
*/
class ArrayFlattener {
/**
*
* This recursive method can be used to flatten any Array containing nested arrays of any level.
*
* @param array The array to be flattened
* @return A one dimensional array containing all the individual elements of the provided input array and its
* nested arrays.
*/
fun flatten(array: Array<*>): Array<*> {
val result = addArrayItemToList(array, ArrayList())
return result.toTypedArray()
}
/**
*
* This recursive method is used internally to iterate over all the elements and sub elements of provided input and
* to add them to a master ArrayList.
*
* @param item The element to be added to the list (can be an item or an array itself)
* @param itemList The itemList to add elements to
* @return A list of all the elements of every array and nested array
*/
private fun addArrayItemToList(item: Any?, itemList: ArrayList<Any>): ArrayList<Any> {
if (item == null) return itemList
if (item is Array<*>) {
for (subItem in item) {
addArrayItemToList(subItem, itemList)
}
} else {
itemList.add(item)
}
return itemList
}
}
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ArrayFlattenerTest {
private lateinit var sut: ArrayFlattener
@Before
fun setup() {
sut = ArrayFlattener()
}
@Test
fun `flatten produces correct output for test case 1`() {
val inputArray = arrayOf(arrayOf(1, 2, arrayOf(3)), 4)
val expectedOutputArray = arrayOf(1, 2, 3, 4)
val result = sut.flatten(inputArray)
assertEquals(result.size, 4)
assertTrue { result.contentEquals(expectedOutputArray) }
}
@Test
fun `flatten produces correct output for test case 2`() {
val inputArray = arrayOf(arrayOf(1, 2 , arrayOf(3, 4, 5)), 6, 7)
val expectedOutputArray = arrayOf(1, 2, 3, 4, 5, 6, 7)
val result = sut.flatten(inputArray)
assertEquals(result.size, 7)
assertTrue { result.contentEquals(expectedOutputArray) }
}
@Test
fun `flatten produces correct output for test case 3`() {
val inputArray = arrayOf(arrayOf(1, arrayOf(2, 3, arrayOf(4, 5, 6), 7), 8), 9)
val expectedOutputArray = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val result = sut.flatten(inputArray)
assertEquals(result.size, 9)
assertTrue { result.contentEquals(expectedOutputArray) }
}
@Test
fun `flatten produces correct output for test case 4`() {
val inputArray = arrayOf(1, 2, 3, 4, 5)
val expectedOutputArray = arrayOf(1, 2, 3, 4, 5)
val result = sut.flatten(inputArray)
assertEquals(result.size, 5)
assertTrue { result.contentEquals(expectedOutputArray) }
}
@Test
fun `flatten produces correct output for test case 5`() {
val inputArray = arrayOf(arrayOf(null, arrayOf(2, 3, arrayOf(4, 5, 6), 7), 8), 9)
val expectedOutputArray = arrayOf(2, 3, 4, 5, 6, 7, 8, 9)
val result = sut.flatten(inputArray)
assertEquals(result.size, 8)
assertTrue { result.contentEquals(expectedOutputArray) }
}
@Test
fun `flatten produces correct output for test case 6`() {
val inputArray = arrayOf(arrayOf("1", arrayOf(2, 3, arrayOf(4, 5, 6), 7), 8), 9)
val expectedOutputArray = arrayOf("1", 2, 3, 4, 5, 6, 7, 8, 9)
val result = sut.flatten(inputArray)
assertEquals(result.size, 9)
assertTrue { result.contentEquals(expectedOutputArray) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment