Skip to content
Merged
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
209 changes: 195 additions & 14 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,171 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# For testing purposes, we will write our code in the function\n",
"def anagram_checker(word_a, word_b):\n",
" # Your code here\n",
" # transforming everything into lowercase as we assume it's not sensetive \n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # moving everything but letters as we need to check if the words are anagram for each other\n",
" word_a_clean = ''.join(char for char in word_a if char.isalpha())\n",
" word_b_clean = ''.join(char for char in word_b if char.isalpha())\n",
" # checking if the words have the same length, if not - return False\n",
" if len(word_a_clean) == len(word_b_clean):\n",
" # comparing 2 sorted strings and returning True if they are equal, that means the words are anagrams\n",
" if sorted(word_a_clean) == sorted(word_b_clean):\n",
" print ('True')\n",
" else:\n",
" print ('False')\n",
" else:\n",
" print ('False')\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# I also tried to resolve it using loops (sth I remember from coding courses from university ), wanted to understand how it gonna work\n",
"def anagram_checker1 (word_a, word_b):\n",
" # transforming everything into lowercase as we assume it's not sensetive \n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # moving everything but letters as we need to check if the words are anagram for each other\n",
" word_a_clean = ''.join(char for char in word_a if char.isalpha())\n",
" word_b_clean = ''.join(char for char in word_b if char.isalpha())\n",
" # checking if the words have the same length, if not - return False\n",
" if len(word_a_clean) == len(word_b_clean):\n",
" #counting frequency for each string\n",
" frequency_word_a = {}\n",
" frequency_word_b = {}\n",
" for n in word_a_clean:\n",
" if n in frequency_word_a:\n",
" frequency_word_a[n] += 1\n",
" else:\n",
" frequency_word_a[n] = 1\n",
" # printing frequency_word_a values for visibility \n",
" print (frequency_word_a)\n",
" for n in word_b_clean:\n",
" if n in frequency_word_b:\n",
" frequency_word_b[n] += 1\n",
" else:\n",
" frequency_word_b[n] = 1\n",
" # printing frequency_word_b values for visibility\n",
" print(frequency_word_b)\n",
" # comparing frequency between the strings and return True if it's anagram\n",
" if frequency_word_a == frequency_word_b:\n",
" print ('True')\n",
" else:\n",
" print ('False')\n",
" else:\n",
" print ('False')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'s': 1, 'i': 1, 'l': 1, 'e': 1, 'n': 1, 't': 1}\n",
"{'l': 1, 'i': 1, 's': 1, 't': 1, 'e': 1, 'n': 1}\n",
"True\n"
]
}
],
"source": [
"anagram_checker1(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"anagram_checker1(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n': 1, 'i': 1, 'g': 1, 'h': 1, 't': 1}\n",
"{'t': 1, 'h': 1, 'i': 1, 'n': 1, 'g': 1}\n",
"True\n"
]
}
],
"source": [
"anagram_checker1(\"night\", \"Thing\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -97,22 +232,68 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" # if our check is case sensetive, we skip transforming everything into lowercaseand compare the strings as they are\n",
" if is_case_sensitive == True:\n",
" # moving everything but letters as we need to check if the words are anagram for each other\n",
" word_a_clean = ''.join(char for char in word_a if char.isalpha())\n",
" word_b_clean = ''.join(char for char in word_b if char.isalpha())\n",
" # checking if the words have the same length, if not - return False\n",
" if len(word_a_clean) == len(word_b_clean):\n",
" # comparing 2 sorted strings and returning True if they are equal, that means the words are anagrams\n",
" if sorted(word_a_clean) == sorted(word_b_clean):\n",
" print ('True')\n",
" else:\n",
" print ('False')\n",
" else:\n",
" print ('False')\n",
" # if our check is not case sensetive - we do next:\n",
" else:\n",
" # transforming everything into lowercase as we assume it's not sensetive \n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
" # moving everything but letters as we need to check if the words are anagram for each other\n",
" word_a_clean = ''.join(char for char in word_a if char.isalpha())\n",
" word_b_clean = ''.join(char for char in word_b if char.isalpha())\n",
" # checking if the words have the same length, if not - return False\n",
" if len(word_a_clean) == len(word_b_clean):\n",
" # comparing 2 sorted strings and returning True if they are equal, that means the words are anagrams\n",
" if sorted(word_a_clean) == sorted(word_b_clean):\n",
" print ('True')\n",
" else:\n",
" print ('False')\n",
" else:\n",
" print ('False')\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -130,7 +311,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +325,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.21"
}
},
"nbformat": 4,
Expand Down
Loading