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
244 changes: 213 additions & 31 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
"# Your code here\n",
"tup=(\"I\",)"
]
},
{
Expand All @@ -33,11 +34,23 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here"
"# Your code here\n",
"type(tup)"
]
},
{
Expand All @@ -55,13 +68,40 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-b4f64d79e570>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your code here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"k\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;31m#or, since append can only take one argument:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mnew_on_tuple\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"o\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"a\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"c\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"k\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnew_on_tuple\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"#or, since append can only take one argument:\n",
"new_on_tuple=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n",
"for item in new_on_tuple:\n",
" tup.append(item)\n",
"tup\n",
"\n",
"# Your explanation here\n",
"\n",
"# Your explanation here\n"
"#First of all, tuples are immutable, and no element can be added, changed or substracted.\n",
"#second, append is a list method\n",
"\n",
"list1=[\"I\"]\n",
"new_on_list=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n",
"for item in new_on_list:\n",
" list1.append(item)\n",
"list1\n"
]
},
{
Expand All @@ -79,13 +119,31 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I'] <class 'list'>\n",
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k') <class 'tuple'>\n"
]
}
],
"source": [
"# Your code here\n",
"\n",
"# Your explanation here\n"
"tup=(\"I\",)\n",
"list2=list(tup)\n",
"print (list2, type(list2))\n",
"new_on_list=[\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n",
"for item in new_on_list:\n",
" list2.append(item)\n",
"tup=tuple(list2)\n",
"print(tup, type(tup))\n",
"# Your explanation here\n",
"# I have created a list with the value of the tuple,\n",
"#added the new values, and turn that list into a tuple."
]
},
{
Expand All @@ -103,11 +161,26 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 45,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n') \n",
" ('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"# Your code here\n",
"tup=(\"I\", \"r\", \"o\", \"n\",\"h\", \"a\", \"c\", \"k\")\n",
"tup1=tup[0:4]\n",
"\n",
"tup2=tup[-4:]\n",
"\n",
"print(tup1,\"\\n\", tup2)"
]
},
{
Expand All @@ -121,11 +194,24 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 47,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n"
"# Your code here\n",
"tup3=tup1+tup2\n",
"tup3"
]
},
{
Expand All @@ -137,11 +223,27 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 54,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n"
"# Your code here\n",
"l_tup1=len(tup1)\n",
"l_tup2=len(tup2)\n",
"\n",
"\n",
"l_tup1+l_tup2==len(tup3)"
]
},
{
Expand All @@ -153,11 +255,21 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"h_index=tup3.index(\"h\")\n",
"print(h_index)"
]
},
{
Expand All @@ -177,11 +289,25 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 59,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a True\n",
"c True\n"
]
}
],
"source": [
"# Your code here\n"
"# Your code here\n",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"for item in letters:\n",
" if item in tup3:\n",
" print(item,True)\n",
" "
]
},
{
Expand All @@ -195,11 +321,25 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 63,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"# Your code here\n"
"# Your code here\n",
"counter=0\n",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"for item in letters:\n",
" if item in tup3:\n",
" counter+=1\n",
"print(counter)"
]
},
{
Expand Down Expand Up @@ -235,7 +375,49 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.8.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
Expand Down
Loading