Skip to content
Open
Show file tree
Hide file tree
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: 162 additions & 35 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
"tup = 'I',"
]
},
{
Expand All @@ -33,11 +33,19 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"# Your code here"
"print(type(tup))"
]
},
{
Expand All @@ -55,13 +63,25 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r')\n"
]
}
],
"source": [
"# Your code here\n",
"#tup.append('r')\n",
" # It's not possible to append the elements because the 'tuple' object has no attribute 'append'. \n",
" # This means this Class has not a built-in function for self.append() command. It is logical as tuples are wanted to be inmutable.\n",
"\n",
"# Your explanation here\n"
"# The correct process to add the elements to 'tup' is to concatenate it with a tuple of the element.\n",
"tup = tup + tuple('r')\n",
"print(tup)"
]
},
{
Expand All @@ -79,13 +99,26 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('o', 'r')\n"
]
}
],
"source": [
"# Your code here\n",
"#tup[0] = 'o',\n",
" # As tuples are intended to be inmutable data structures, 'tuples' object does not support item assignment.\n",
"\n",
"# Your explanation here\n"
"# Alternative code (but very different from item assignment) could be:\n",
"l_tup = list(tup)\n",
"l_tup[0] = 'o'\n",
"tup = tuple(l_tup)\n",
"print(tup)"
]
},
{
Expand All @@ -103,11 +136,24 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n')\n",
"('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"tup1 = tup[:4]\n",
"tup2 = tup[-4:]\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
Expand All @@ -121,11 +167,22 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k') ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"tup3 = tup1 + tup2\n",
"print(tup3 == tup)\n",
"print(tup3,tup)"
]
},
{
Expand All @@ -137,11 +194,20 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# Your code here\n"
"sum_tups = len(tup1) + len(tup2)\n",
"print(len(tup3) == sum_tups)"
]
},
{
Expand All @@ -153,11 +219,22 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here"
"tup3.index('h')"
]
},
{
Expand All @@ -177,11 +254,26 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"# Your code here\n"
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"for let in letters:\n",
" print(let in tup3,)"
]
},
{
Expand All @@ -195,11 +287,28 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"0\n",
"1\n",
"0\n",
"0\n"
]
}
],
"source": [
"# Your code here\n"
"for let in letters:\n",
" counter = 0\n",
" for i in range(len(tup3)):\n",
" if let == tup3[i]:\n",
" counter = counter+1\n",
" print(counter)"
]
},
{
Expand All @@ -211,11 +320,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "NameError",
"evalue": "name 'p_tup1' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-11-087260f8dd70>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mpacked_l\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mpacked_tup\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtuple\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpacked_l\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;33m(\u001b[0m\u001b[0mp_tup1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mp_tup2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mp_tup3\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m21\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpacked_tup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mp_tup2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'p_tup1' is not defined"
]
}
],
"source": [
"# Your code here\n"
"packed_l = []\n",
"for i in range(100):\n",
" packed_l.append(i)\n",
"packed_tup = tuple(packed_l)\n",
"(p_tup1[:10], *p_tup2, p_tup3[21:]) = packed_tup\n",
"for t in p_tup2:\n",
" print(t)"
]
}
],
Expand All @@ -235,7 +362,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.8.3"
}
},
"nbformat": 4,
Expand Down
Loading