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
123 changes: 123 additions & 0 deletions 02_activities/assignments/DSI_Python.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
" #PART 1\n",
"\n",
"\n",
"from collections import Counter\n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive=False):\n",
" \"\"\"\n",
" Checks if two words are anagrams of each other.\n",
"\n",
" Parameters:\n",
" word_a (str): The first word.\n",
" word_b (str): The second word.\n",
" is_case_sensitive (bool): If True, considers case sensitivity in anagram checking.\n",
"\n",
" Returns:\n",
" bool: True if words are anagrams, False otherwise.\n",
" \"\"\"\n",
" # Normalize words: Remove spaces and apply lowercase if case sensitivity is off\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # Use Counter to compare character occurrences\n",
" return Counter(word_a) == Counter(word_b)\n",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counter() is a very powerful tool, but is probably a overuse for the task. We can simply compare two strings without turning them into dictionaries:
word_a == word_b

"\n",
"# Test Cases\n",
"print(anagram_checker(\"Silent\", \"listen\", False)) # Expected: True\n",
"print(anagram_checker(\"Silent\", \"Listen\", True)) # Expected: False\n",
"print(anagram_checker(\"Silent\", \"Night\", False)) # Expected: False\n",
"print(anagram_checker(\"night\", \"Thing\", False)) # Expected: True\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f3oB2Z02wpwS",
"outputId": "eac66ff9-89ae-4bdb-bb6d-9344245c7049"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"True\n",
"False\n",
"False\n",
"True\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive=False):\n",
" \"\"\"\n",
" Checks if two words are anagrams of each other with an optional case-sensitive check.\n",
"\n",
" Parameters:\n",
" word_a (str): The first word.\n",
" word_b (str): The second word.\n",
" is_case_sensitive (bool): If True, considers case sensitivity in anagram checking.\n",
"\n",
" Returns:\n",
" bool: True if words are anagrams, False otherwise.\n",
" \"\"\"\n",
" # Normalize words: Remove spaces, apply lowercase if case sensitivity is off\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # Use Counter to compare character occurrences\n",
" return Counter(word_a) == Counter(word_b)\n",
"\n",
"# Test Cases for Part 2\n",
"print(anagram_checker(\"Silent\", \"listen\", False)) # Expected: True\n",
"print(anagram_checker(\"Silent\", \"Listen\", True)) # Expected: False\n",
"print(anagram_checker(\"Silent\", \"Night\", False)) # Expected: False\n",
"print(anagram_checker(\"night\", \"Thing\", False)) # Expected: True\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pJdptaCPw0nK",
"outputId": "209fdf88-4ca4-414c-fca6-cef759d55746"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"True\n",
"False\n",
"False\n",
"True\n"
]
}
]
}
]
}
135 changes: 135 additions & 0 deletions Assignment-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMWnXObtyzsz92ucZBuFrDu",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/afesobi/python/blob/Assignment-1/Assignment-1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
" #PART 1\n",
"\n",
"\n",
"from collections import Counter\n",
"\n",
"def anagram_checker(word_a, word_b, is_case_sensitive=False):\n",
" \"\"\"\n",
" Checks if two words are anagrams of each other.\n",
"\n",
" Parameters:\n",
" word_a (str): The first word.\n",
" word_b (str): The second word.\n",
" is_case_sensitive (bool): If True, considers case sensitivity in anagram checking.\n",
"\n",
" Returns:\n",
" bool: True if words are anagrams, False otherwise.\n",
" \"\"\"\n",
" # Normalize words: Remove spaces and apply lowercase if case sensitivity is off\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # Use Counter to compare character occurrences\n",
" return Counter(word_a) == Counter(word_b)\n",
"\n",
"# Test Cases\n",
"print(anagram_checker(\"Silent\", \"listen\", False)) # Expected: True\n",
"print(anagram_checker(\"Silent\", \"Listen\", True)) # Expected: False\n",
"print(anagram_checker(\"Silent\", \"Night\", False)) # Expected: False\n",
"print(anagram_checker(\"night\", \"Thing\", False)) # Expected: True\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f3oB2Z02wpwS",
"outputId": "eac66ff9-89ae-4bdb-bb6d-9344245c7049"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"True\n",
"False\n",
"False\n",
"True\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive=False):\n",
" \"\"\"\n",
" Checks if two words are anagrams of each other with an optional case-sensitive check.\n",
"\n",
" Parameters:\n",
" word_a (str): The first word.\n",
" word_b (str): The second word.\n",
" is_case_sensitive (bool): If True, considers case sensitivity in anagram checking.\n",
"\n",
" Returns:\n",
" bool: True if words are anagrams, False otherwise.\n",
" \"\"\"\n",
" # Normalize words: Remove spaces, apply lowercase if case sensitivity is off\n",
" if not is_case_sensitive:\n",
" word_a = word_a.lower()\n",
" word_b = word_b.lower()\n",
"\n",
" # Use Counter to compare character occurrences\n",
" return Counter(word_a) == Counter(word_b)\n",
"\n",
"# Test Cases for Part 2\n",
"print(anagram_checker(\"Silent\", \"listen\", False)) # Expected: True\n",
"print(anagram_checker(\"Silent\", \"Listen\", True)) # Expected: False\n",
"print(anagram_checker(\"Silent\", \"Night\", False)) # Expected: False\n",
"print(anagram_checker(\"night\", \"Thing\", False)) # Expected: True\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pJdptaCPw0nK",
"outputId": "209fdf88-4ca4-414c-fca6-cef759d55746"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"True\n",
"False\n",
"False\n",
"True\n"
]
}
]
}
]
}