Skip to content

Instantly share code, notes, and snippets.

@vinamelody
Created June 17, 2016 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinamelody/daa7a32f426a40f9a2b44706594c1f59 to your computer and use it in GitHub Desktop.
Save vinamelody/daa7a32f426a40f9a2b44706594c1f59 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Write some code in either Ruby or JavaScript that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]. Please don't use any built-in flatten functions in either language.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, I would like to understand the example array given.\n",
"\n",
"Array `[[1,2,[3]],4]` consist of:\n",
"\n",
"```\n",
"array = [subarray1,4]\n",
"subarray1 = [1,2,subarray2]\n",
"subarrary2 = [3]\n",
"```\n",
"\n",
"So, I am thinking if there's a way to loop `array` items and if current item is an array, call the same function (like a recursive \n",
"function)."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, [3]], 4]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array = [[1,2,[3]],4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initially I would like to know if this function `is_a?` works"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"found array: [1, 2, [3]]\n"
]
},
{
"data": {
"text/plain": [
"[[1, 2, [3]], 4]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array.each do |item|\n",
" if item.is_a? Array\n",
" puts(\"found array: #{item}\")\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So perhaps I could make a function that if item is an array, call itself. And I will need to store the result in new array."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
":make_array"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"def make_array(result=[], array)\n",
" @result = result\n",
" puts(\"array is #{array}\")\n",
" array.each do |item|\n",
" if item.is_a? Array\n",
" puts(\"found array: #{item}\")\n",
" make_array(result, item)\n",
" else\n",
" result.push(item)\n",
" end\n",
" end\n",
" return result\n",
"end\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array is [[1, 2, [3]], 4]\n",
"found array: [1, 2, [3]]\n",
"array is [1, 2, [3]]\n",
"found array: [3]\n",
"array is [3]\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"make_array array\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cleaned code becomes"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
":make_array"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def make_array(result=[], array)\n",
" @result = result\n",
" puts(\"array is #{array}\")\n",
" array.each do |item|\n",
" if item.is_a? Array\n",
" puts(\"found array: #{item}\")\n",
" make_array(result, item)\n",
" else\n",
" result.push(item)\n",
" end\n",
" end\n",
" return result\n",
"end\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.5",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment