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
148 changes: 100 additions & 48 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,19 @@
"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-3-79d5a2bcc9c1>\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\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;31m# Your explanation here\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[0;31m# tuple is immutable\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'"
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_18356/3078821583.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Your code here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;31m# Your explanation here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m#tuples are imutablme hence we cannot append the data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
"# Your explanation here\n",
"# tuple is immutable"
"\n",
"#tuples are imutablme hence we cannot append the data"
]
},
{
Expand All @@ -105,14 +106,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup[0] = \"r\"\n",
"tup1 = (\"I\", \"r\", \"o\", \"n\")\n",
"tup2 = (\"h\", \"a\", \"c\", \"k\")\n",
"\n",
"tup = tup1 + tup2\n",
"print(tup)\n",
"# Your explanation here\n",
"# tuple is immutable"
"#since tuples are immutable I combined twotuples into one using addition "
]
},
{
Expand All @@ -130,13 +143,24 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n')\n",
"('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup1 = (\"I\", \"r\", \"o\", \"n\")\n",
"tup2 = (\"h\", \"a\", \"c\", \"k\")"
"tup1 = tup[0:4]\n",
"tup2 = tup[4:9]\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
Expand All @@ -150,26 +174,23 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"True\n"
]
}
],
"source": [
"# Your code here\n",
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"tup3 = tup1 + tup2\n",
"tup3 = tup1 +tup2\n",
"print(tup3)\n",
"print(tup)\n",
"print(tup == tup3)"
"print(tup3 == tup)"
]
},
{
Expand All @@ -181,26 +202,29 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"8\n",
"8\n",
"8\n"
]
}
],
"source": [
"# Your code here\n",
"print(len(tup1))\n",
"print(len(tup3))\n",
"print(len(tup1) + len(tup2))\n",
"print(len(tup3))"
"count1 = len(tup1)\n",
"count2 = len(tup2)\n",
"\n",
"count_sum =count1 +count2\n",
"\n",
"count3 = len(tup3)\n",
"\n",
"print(count_sum)\n",
"print(count3)"
]
},
{
Expand All @@ -212,7 +236,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -221,15 +245,14 @@
"4"
]
},
"execution_count": 32,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup3.index(\"h\")\n",
"##It's number 4"
"tup3.index(\"h\")\n"
]
},
{
Expand All @@ -249,33 +272,27 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[True, False, True, False, False]\n"
"True\n"
]
}
],
"source": [
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"# Your code here\n",
"# with a for loop\n",
"#for element in letters:\n",
" # if element in tup3:\n",
" # print(True)\n",
" # else:\n",
" # print(False)\n",
"#using a list comprehension\n",
"letter = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"existing_letters = [True if (element in tup3) else False for element in letters ]\n",
"res = any(ele in tup3 for ele in letter)\n",
"\n",
"#if element in tup3 print(True) else print(False) \n",
"print(existing_letters)\n"
"if res == True:\n",
" print(res)\n",
"else:\n",
" print(\"False\")"
]
},
{
Expand All @@ -289,18 +306,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n",
"\n"
"\n",
"\n",
"def count_occurence (tup, list):\n",
" '''define 1st count as 0'''\n",
" count = 0\n",
" for element in tup:\n",
" if element in tup:\n",
" count +=1\n",
" return count"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_occurence(tup3, letter)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -314,7 +366,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
Loading