diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index d68aeb6..e361aa9 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " + " \"\"\"\n", + " This function returns the summation of every number from 1 to num\n", + " \"\"\"\n", + " # Your code here: \n", + " result = 0\n", + " for k in range(1,num):\n", + " result += k\n", + " return result" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2850" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation" + "# print summation\n", + "summation(76)" ] }, { @@ -60,7 +78,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +87,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", + " \"\"\"\n", + " This function returns the sum or *args\n", + " \"\"\"\n", + " # Your code here:\n", + " result = 0\n", " \n", - " # Your code here:" + " for i in args:\n", + " result += i\n", + " return result\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "summation2(*array)" ] }, { @@ -97,23 +145,50 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", " \n", - " # Your code here:" + " # Your code here:\n", + " result = a + b + c\n", + " return result\n", + "\n", + "summation3(2,3,4)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "summation3(3,6,9)" ] }, { @@ -125,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +209,79 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], + "source": [ + "result = 0\n", + "for key,value in dictionary.items():\n", + " if key != \"red\":\n", + " result += value\n", + " else:\n", + " pass\n", + "\n", + "print (result)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", " \n", - " # Your code here:" + " # Your code here:\n", + " result = 0\n", + " for key,value in kwargs.items():\n", + " if key != \"red\":\n", + " result += value\n", + " else:\n", + " pass\n", + " return result\n", + "\n", + "summation4(**dictionary)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "summation4(**dictionary)" ] }, { @@ -162,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +306,81 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation5('define the function input'):\n", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def summation5(a:int, b:int, c:int, *args, **kwargs):\n", + " return (a + b + c + sum(list(args)) + sum(list(kwargs.values())))\n", + "\n", + "summation5(a,b,c,*d,**e)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def summation5(a:int, b:int, c:int, *args, **kwargs):\n", " # This function returns the sum of a,b,c,d and e\n", " # You need to define the function input\n", - " # Your code here:" + " # Your code here:\n", + " \n", + " x = 0\n", + " for i in args:\n", + " x += i\n", + " \n", + " y = 0\n", + " for key,value in kwargs.items():\n", + " y += value\n", + " result = a + b + c + x + y\n", + " return result\n", + "\n", + "summation5(4,6,15,*[48,465,23,96],**{'A':16,'B':32,'C':64})" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "summation5(a,b,c,*d,**e)" ] }, { @@ -205,9 +394,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +417,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +436,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# After we have iterated through all elements, we will get a StopIteration Error\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,9 +476,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], "source": [ "# We can also iterate through an iterator using a for loop like this:\n", "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", @@ -274,12 +509,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "iterator=iter([1,2,3])\n", "\n", + "iterator2=iter([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])\n", + "\n", "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", " # Input: Iterable\n", @@ -288,16 +525,33 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for i in iterator:\n", + " if i % 2 == 0:\n", + " print (i)\n", + " else:\n", + " print (0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "0\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "divisible2(iterator)\n", + "divisible2(iterator2)" ] }, { @@ -311,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -331,14 +585,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + }, + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "iterator = firstn(5)\n", "\n", "for i in iterator:\n", - " print(i)" + " print(i)\n", + " \n", + "type(firstn)" ] }, { @@ -350,9 +628,45 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,\n", + " 2,\n", + " 4,\n", + " 6,\n", + " 8,\n", + " 10,\n", + " 12,\n", + " 14,\n", + " 16,\n", + " 18,\n", + " 20,\n", + " 22,\n", + " 24,\n", + " 26,\n", + " 28,\n", + " 30,\n", + " 32,\n", + " 34,\n", + " 36,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 44,\n", + " 46,\n", + " 48,\n", + " 50]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -360,7 +674,11 @@ " # Output: iterator\n", " \n", " # Sample Input: 5\n", - " # Sample Output: iter([0, 2, 4])" + " # Sample Output: iter([0, 2, 4])\n", + " eg = [i for i in range(0,n,2)]\n", + " return eg\n", + " \n", + "even_iterator(51)" ] }, { @@ -374,15 +692,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6]\n" + ] + } + ], + "source": [ + "print(even_iterator(8))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +724,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.2" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index d68aeb6..e361aa9 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " + " \"\"\"\n", + " This function returns the summation of every number from 1 to num\n", + " \"\"\"\n", + " # Your code here: \n", + " result = 0\n", + " for k in range(1,num):\n", + " result += k\n", + " return result" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2850" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation" + "# print summation\n", + "summation(76)" ] }, { @@ -60,7 +78,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +87,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", + " \"\"\"\n", + " This function returns the sum or *args\n", + " \"\"\"\n", + " # Your code here:\n", + " result = 0\n", " \n", - " # Your code here:" + " for i in args:\n", + " result += i\n", + " return result\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "summation2(*array)" ] }, { @@ -97,23 +145,50 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", " \n", - " # Your code here:" + " # Your code here:\n", + " result = a + b + c\n", + " return result\n", + "\n", + "summation3(2,3,4)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "summation3(3,6,9)" ] }, { @@ -125,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +209,79 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], + "source": [ + "result = 0\n", + "for key,value in dictionary.items():\n", + " if key != \"red\":\n", + " result += value\n", + " else:\n", + " pass\n", + "\n", + "print (result)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", " \n", - " # Your code here:" + " # Your code here:\n", + " result = 0\n", + " for key,value in kwargs.items():\n", + " if key != \"red\":\n", + " result += value\n", + " else:\n", + " pass\n", + " return result\n", + "\n", + "summation4(**dictionary)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "summation4(**dictionary)" ] }, { @@ -162,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +306,81 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation5('define the function input'):\n", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def summation5(a:int, b:int, c:int, *args, **kwargs):\n", + " return (a + b + c + sum(list(args)) + sum(list(kwargs.values())))\n", + "\n", + "summation5(a,b,c,*d,**e)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def summation5(a:int, b:int, c:int, *args, **kwargs):\n", " # This function returns the sum of a,b,c,d and e\n", " # You need to define the function input\n", - " # Your code here:" + " # Your code here:\n", + " \n", + " x = 0\n", + " for i in args:\n", + " x += i\n", + " \n", + " y = 0\n", + " for key,value in kwargs.items():\n", + " y += value\n", + " result = a + b + c + x + y\n", + " return result\n", + "\n", + "summation5(4,6,15,*[48,465,23,96],**{'A':16,'B':32,'C':64})" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "summation5(a,b,c,*d,**e)" ] }, { @@ -205,9 +394,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +417,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +436,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# After we have iterated through all elements, we will get a StopIteration Error\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,9 +476,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], "source": [ "# We can also iterate through an iterator using a for loop like this:\n", "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", @@ -274,12 +509,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "iterator=iter([1,2,3])\n", "\n", + "iterator2=iter([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])\n", + "\n", "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", " # Input: Iterable\n", @@ -288,16 +525,33 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for i in iterator:\n", + " if i % 2 == 0:\n", + " print (i)\n", + " else:\n", + " print (0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "0\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "divisible2(iterator)\n", + "divisible2(iterator2)" ] }, { @@ -311,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -331,14 +585,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + }, + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "iterator = firstn(5)\n", "\n", "for i in iterator:\n", - " print(i)" + " print(i)\n", + " \n", + "type(firstn)" ] }, { @@ -350,9 +628,45 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,\n", + " 2,\n", + " 4,\n", + " 6,\n", + " 8,\n", + " 10,\n", + " 12,\n", + " 14,\n", + " 16,\n", + " 18,\n", + " 20,\n", + " 22,\n", + " 24,\n", + " 26,\n", + " 28,\n", + " 30,\n", + " 32,\n", + " 34,\n", + " 36,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 44,\n", + " 46,\n", + " 48,\n", + " 50]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -360,7 +674,11 @@ " # Output: iterator\n", " \n", " # Sample Input: 5\n", - " # Sample Output: iter([0, 2, 4])" + " # Sample Output: iter([0, 2, 4])\n", + " eg = [i for i in range(0,n,2)]\n", + " return eg\n", + " \n", + "even_iterator(51)" ] }, { @@ -374,15 +692,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6]\n" + ] + } + ], + "source": [ + "print(even_iterator(8))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +724,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.2" } }, "nbformat": 4,