Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 151 additions & 46 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"# We first define our iterator:\n",
"\n",
Expand All @@ -36,9 +44,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"# We continue to iterate through the iterator.\n",
"\n",
Expand All @@ -47,18 +63,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"print(next(iterator))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-91070dc82d50>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"# After we have iterated through all elements, we will get a StopIteration Error\n",
"\n",
Expand All @@ -67,9 +103,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"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",
Expand All @@ -90,24 +136,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def divisible2(iterator):\n",
" \"\"\"\n",
" This function takes an iterable and returns the first \n",
" element that is divisible by 2 and zero otherwise.\n",
" # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n",
" # Input: Iterable\n",
" # Output: Integer\n",
" \n",
" Input: Iterable\n",
" Output: Integer\n",
" \n",
" Sample Input: iter([1,2,3])\n",
" Sample Output: 2\n",
" \"\"\"\n",
" # Sample Input: iter([1,2,3])\n",
" # Sample Output: 2\n",
" \n",
" # Your code here:\n",
" "
" \n",
" for i in iterator:\n",
" if i %2 == 0:\n",
" return i\n",
" else: \n",
" print('0')\n",
" \n",
"divisible2(iter([1,2,3]))\n",
"\n",
"\n",
"\n"
]
},
{
Expand All @@ -121,15 +191,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def firstn(n):\n",
" number = 0\n",
" while number < n:\n",
" yield number\n",
" number = number + 1"
" number = 0\n",
" while number < n:\n",
" yield number\n",
" number = number + 1 # n +=1\n",
" "
]
},
{
Expand All @@ -141,13 +212,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object firstn at 0x7fc2d5ad9740>\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"iterator = firstn(5)\n",
"\n",
"for i in iterator:\n",
"print(iterator) # I print iterator to check the content \n",
"\n",
"for i in iterator: # Calling a generator function, creates an iterator \n",
" print(i)"
]
},
Expand All @@ -160,25 +246,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2\n",
"4\n"
]
}
],
"source": [
"def even_iterator(n):\n",
" \"\"\"\n",
" This function produces an iterator containing \n",
" all even numbers between 0 and n.\n",
" \n",
" Input: Integer\n",
" Output: Iterator\n",
" \n",
" Sample Input: 5\n",
" Sample Output: iter([0, 2, 4])\n",
" \"\"\"\n",
" \n",
" # This function produces an iterator containing all even numbers between 0 and n\n",
" # Input: integer\n",
" # Output: iterator\n",
" # Sample Input: 5\n",
" # Sample Output: iter([0, 2, 4])\n",
" # Your code here:\n",
" "
" number = 0 \n",
" while number < n:\n",
" if number %2== 0:\n",
" yield number\n",
" number += 1 \n",
" \n",
"result = even_iterator(5)\n",
"for i in result:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -197,9 +302,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}