{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1\n", "Given an array, find out the product of its element using recursion.\n", "- Input: [1, 2, 3, 4, 5] \n", "- Output: 120" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def product(arr):\n", " \"\"\"\n", " calculates the product of all the numbers in array\n", " \"\"\"\n", " if len(arr) == 0:\n", " return 1\n", " else:\n", " return arr[0] * product(arr[1:])\n", "\n", "product([1, 2, 3, 4, 5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2\n", "Given a string, write a function `reverse` which accepts a string and returns a new string in reverse.\n", "- Input: reverse(\"Hello\")\n", "- Output: \"olleH\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'olleH'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def reverse(string):\n", " \"\"\"\n", " returns the reverse of a string\n", " \"\"\"\n", " if len(string) == 0:\n", " return \"\"\n", " return string[-1] + reverse(string[:-1])\n", "\n", "reverse(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3\n", "Given a string, write a function `reverse` to check whether the string is palindrome. \n", "Note: You must use `recursion` for this problem.\n", "- Input1: reverse(\"malayalam\") \n", "- Output1: True\n", "- Input2: reverse(\"hello\")\n", "- Output2: False" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def reverse(string):\n", " \"\"\"\n", " returns the reverse of a string\n", " \"\"\"\n", " if len(string) == 0:\n", " return \"\"\n", " return string[-1] + reverse(string[:-1])\n", "\n", "string = \"malayalam\"\n", "string == reverse(string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4\n", "Given a nested array, write a function `flatten` to return a flatlist\n", "- Input: [[2, 3, [4]]]\n", "- Output: [2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def flatten(arr):\n", " \"\"\"\n", " returns the flattened array.\n", " \"\"\"\n", " result = []\n", " for element in arr:\n", " if type(element) is list:\n", " result.extend(flatten(element))\n", " else:\n", " result.append(element)\n", " return result\n", "\n", "flatten([[2, 3, [4]]])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5\n", "Given a word list, write a function `capitalize_first` to capitalize the first letter of each string in the array. Note, you must use recursion for this problem.\n", "- Input: capitalize([\"hello\", \"world\"])\n", "- Output: [\"Hello\", \"World]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello', 'World']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def capitalize_first(arr):\n", " \"\"\"\n", " converts the first letter of all the elements into uppercase.\n", " \"\"\"\n", " result = []\n", " if len(arr) == 0:\n", " return result\n", " result.append(arr[0][0].upper() + arr[0][1:])\n", " return result + capitalize_first(arr[1:])\n", "\n", "capitalize_first([\"hello\", \"world\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 6\n", "Given a `.json` object, write a function `string_convert` which takes an object and finds all the values which are numbers and converts them into a string.\n", "- Input: {\n", " \"num\" : 1,\n", " \"data\": {\n", " \"value\" : 8,\n", " \"jam\" : {\n", " \"english\": 50\n", " }\n", " }\n", "}\n", "- Output: {\n", " \"num\" : \"1\",\n", " \"data\": {\n", " \"value\" : \"8\",\n", " \"jam\" : {\n", " \"english\": \"50\"\n", " }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'num': '1', 'data': {'value': '8', 'jam': {'english': '50'}}}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def string_convert(obj):\n", " \"\"\"\n", " converts all the integer values inside of an object into a string.\n", " \"\"\"\n", " for element in obj:\n", " if type(obj[element]) is int:\n", " obj[element] = str(obj[element])\n", " elif type(obj[element]) is dict:\n", " obj[element] = string_convert(obj[element])\n", "\n", " return obj\n", "\n", "string_convert({\n", " \"num\" : 1,\n", " \"data\": {\n", " \"value\" : 8,\n", " \"jam\" : {\n", " \"english\": 50\n", " }\n", " }\n", "})\n", "\n", "\n", " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 7\n", "Given a `.json` object, write a function `fetch_str` which takes the object and finds all the keys that have string values. Note, you must use `recursion` for this problem.\n", "- Input: {\"Hello\": \"World\",\n", " \"England\": {\n", " \"London\": \"Heathrow\",\n", " \"Oxford\":100,\n", " \"Stanford\": {\n", " \"France\": \"Paris\",\n", " \"Germany\": {\n", " \"Berlin\": \"TU Berlin\",\n", " \"Frankfurt\": 1000\n", " }\n", " }\n", " },\n", " \"India\": 1}\n", "- Output: [\"Hello\", \"London\", \"France\", \"Berlin\"] " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello', 'London', 'France', 'Berlin']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fetch_str(obj):\n", " \"\"\"\n", " Returns keys with integer values.\n", " \"\"\"\n", " result = []\n", " for element in obj:\n", " if type(obj[element]) is str:\n", " result.append(element)\n", " if type(obj[element]) is dict:\n", " result.extend(fetch_str(obj[element]))\n", " return result\n", "\n", "fetch_str({\"Hello\": \"World\",\n", " \"England\": {\n", " \"London\": \"Heathrow\",\n", " \"Oxford\":100,\n", " \"Stanford\": {\n", " \"France\": \"Paris\",\n", " \"Germany\": {\n", " \"Berlin\": \"TU Berlin\",\n", " \"Frankfurt\": 1000\n", " }\n", " }\n", " },\n", " \"India\": 1})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f" }, "kernelspec": { "display_name": "Python 3.8.3 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }