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
217 changes: 185 additions & 32 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
"tup = (\"I\",) "
]
},
{
Expand All @@ -32,11 +32,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n"
"type(tup)"
]
},
{
Expand All @@ -54,13 +65,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup_origin = (\"I\",) \n",
"\n",
"tup_adding = (\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\",) #we create a new tup for adding it later by +\n",
"\n",
"tup_append = tup_origin + tup_adding\n",
"\n",
"# Your explanation here\n"
"print(tup_append)\n",
" \n",
"# You can't add elements to a tuple because of their immutable property. There's no append() or extend() method for tuples so we add another one tuple and sum them together"
]
},
{
Expand All @@ -80,13 +105,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup = list(tup_origin)\n",
"\n",
"tup_new_values = \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"\n",
"\n",
"tup.extend(tup_new_values)\n",
"\n",
"print(tuple(tup))\n",
"\n",
"# Your explanation here\n"
"\n",
"# We cannot add elements to a tuple so we convert it into a list, add elements and convert again in tuple\n"
]
},
{
Expand All @@ -104,11 +144,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"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"
"tup1 = tup[:4]\n",
"\n",
"tup2 = tup[4:]\n",
"\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
Expand All @@ -122,11 +176,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I', 'r', 'o', 'n', 'h', 'a', 'c', 'k']\n",
"Result is: False\n"
]
}
],
"source": [
"# Your code here\n"
"tup3 = tup1 + tup2\n",
"print(tup3)\n",
"\n",
"# initialize list of tuples \n",
"tup1 = ['I', 'r', 'o', 'n']\n",
"tup2 = ['h', 'a', 'c', 'k']\n",
" \n",
"# convert lists to sets\n",
"set1 = set(tup1)\n",
"set2 = set(tup2)\n",
" \n",
"# check if sets are identical\n",
"res = set1 == set2\n",
"print(\"Result is: \", (res))"
]
},
{
Expand All @@ -138,11 +214,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of elements in tup1 and tup2 is: 8\n",
"Sets are both: 8\n"
]
}
],
"source": [
"# Your code here\n"
"#count the number of elements in tup1 and tup2\n",
"\n",
"tup1= ['I', 'r', 'o', 'n']\n",
"tup2= ['h', 'a', 'c', 'k']\n",
"tup3 = ['I', 'r', 'o', 'n', 'h', 'a', 'c', 'k']\n",
"\n",
"sum1 = len(tup1)\n",
"sum2 = len(tup2)\n",
"sum12 = sum1 + sum2\n",
"print(\"The number of elements in tup1 and tup2 is:\", sum12)\n",
"\n",
"# check if sets are identical\n",
"sum3 = len(tup3)\n",
"print(\"Sets are both:\", sum3)"
]
},
{
Expand All @@ -154,11 +252,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 42,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The index number of h is: 4\n"
]
}
],
"source": [
"# Your code here\n"
"# tuple containing vowels\n",
"tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k',)\n",
"\n",
"# index of 'e' in vowels\n",
"index = tup3.index('h')\n",
"\n",
"print(\"The index number of h is: \", index)"
]
},
{
Expand All @@ -178,11 +290,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"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 element in letters:\n",
" if element in tup3:\n",
" print(\"True\")\n",
" else: \n",
" print(\"False\")"
]
},
{
Expand All @@ -203,11 +333,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 55,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a: 1\n",
"b: 0\n",
"c: 1\n",
"d: 0\n",
"e: 0\n"
]
}
],
"source": [
"# Your code here\n"
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"\n",
"# Create a dictionary\n",
"letter_counts = {}\n",
"\n",
"for letter in letters:\n",
" count = tup3.count(letter)\n",
" letter_counts[letter] = count\n",
"\n",
"for letter, count in letter_counts.items():\n",
" print(f\"{letter}: {count}\")\n"
]
}
],
Expand All @@ -227,7 +380,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.10.9"
},
"toc": {
"base_numbering": 1,
Expand Down
Loading