You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
63 KiB
Plaintext
278 lines
63 KiB
Plaintext
3 years ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "6975250a-539e-40bd-ac35-4204ff4e3181",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# program to counter letter occurances in a string, or a word\n",
|
||
|
"# methods modified from \n",
|
||
|
"# https://www.geeksforgeeks.org/python-program-to-find-occurrence-to-each-character-in-given-string/"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"id": "acee5120-ae82-4a9f-bbfb-14148e17d4ef",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"tulip is composed of these letters and occurances :\n",
|
||
|
" {'u': 1, 't': 1, 'i': 1, 'p': 1, 'l': 1}\n",
|
||
|
"pulpit is composed of these letters and occurances :\n",
|
||
|
" {'u': 1, 't': 1, 'i': 1, 'p': 2, 'l': 1}\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 法一\n",
|
||
|
"def set_counter(inp_str):\n",
|
||
|
" # using set() + count() to get count\n",
|
||
|
" # of each element in string\n",
|
||
|
" out = {x : inp_str.count(x) for x in set(inp_str )}\n",
|
||
|
" # printing result\n",
|
||
|
" print (\"{} is composed of these letters and occurances :\\n \".format(inp_str)+ str(out))\n",
|
||
|
"\n",
|
||
|
"set_counter(\"tulip\")\n",
|
||
|
"set_counter(\"pulpit\")\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"id": "0bf4971c-7490-4dbf-910e-0502d9593a17",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"tulip composed of these letters and occurances : \n",
|
||
|
"{'t': 1, 'u': 1, 'l': 1, 'i': 1, 'p': 1}\n",
|
||
|
"pulpit composed of these letters and occurances : \n",
|
||
|
"{'p': 2, 'u': 1, 'l': 1, 'i': 1, 't': 1}\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 法二\n",
|
||
|
"# initializing string\n",
|
||
|
"def freq_dict(inp_str):\n",
|
||
|
" # frequency dictionary\n",
|
||
|
" freq = {}\n",
|
||
|
" \n",
|
||
|
" for ele in inp_str:\n",
|
||
|
" if ele in freq:\n",
|
||
|
" freq[ele] += 1\n",
|
||
|
" else:\n",
|
||
|
" freq[ele] = 1\n",
|
||
|
" # printing result\n",
|
||
|
" print (\"{} composed of these letters and occurances : \\n\".format(inp_str)+ str(freq))\n",
|
||
|
"\n",
|
||
|
"freq_dict(\"tulip\")\n",
|
||
|
"freq_dict(\"pulpit\")\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"id": "a2ce38d4-9407-4e05-a82a-e38667c18367",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"tulip is composed of these letters and occurances :\n",
|
||
|
" Counter({'t': 1, 'u': 1, 'l': 1, 'i': 1, 'p': 1})\n",
|
||
|
"pulpit is composed of these letters and occurances :\n",
|
||
|
" Counter({'p': 2, 'u': 1, 'l': 1, 'i': 1, 't': 1})\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 法三\n",
|
||
|
"from collections import Counter\n",
|
||
|
"\n",
|
||
|
"def collection_counter(inp_str):\n",
|
||
|
"\n",
|
||
|
" # using collections.Counter() to get\n",
|
||
|
" # count of each element in string\n",
|
||
|
" oup = Counter(inp_str)\n",
|
||
|
"\n",
|
||
|
" # printing result\n",
|
||
|
" print (\"{} is composed of these letters and occurances :\\n \".format(inp_str)+ str(oup))\n",
|
||
|
"\n",
|
||
|
"collection_counter(\"tulip\")\n",
|
||
|
"collection_counter(\"pulpit\")\n",
|
||
|
" \n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "9bf3deb3-467d-4056-8a1b-1df257508356",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# this program can take two input words can see if they are anagrams? \n",
|
||
|
"# isAnagram \n",
|
||
|
"# hester mofet \n",
|
||
|
"# are there anagrams in non-alphabetical word arrangements? yes, but in scalable units"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"id": "4f079374-28d7-4c44-935c-7d26fe9f4691",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# method to see if a pair of words are anagrams to each other\n",
|
||
|
"def set_counter(inp_str):\n",
|
||
|
" return {x : inp_str.count(x) for x in set(inp_str )}\n",
|
||
|
"\n",
|
||
|
"def areAnagrams(str1, str2):\n",
|
||
|
" out1 = {}\n",
|
||
|
" out2 = {}\n",
|
||
|
" #apply set counter method to each of the words and store the output \n",
|
||
|
" out1 = set_counter(str1)\n",
|
||
|
" out2 = set_counter(str2)\n",
|
||
|
" # compare output \n",
|
||
|
" if out1 == out2:\n",
|
||
|
" return True\n",
|
||
|
" else: \n",
|
||
|
" return False "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 36,
|
||
|
"id": "1f4fab84-aeec-4b6f-af67-a76d2af5e20c",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"~~~~~~~~~~~~~~~~~~~~movie still from The Silence of Lamb to for a pair of anagrams ~~~~~~~~~~~~\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<matplotlib.image.AxesImage at 0x7f14448dbd00>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 36,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAADXCAYAAADhqxGkAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAACo40lEQVR4nOz9eYxl2XbeB/7W3vucc+cbc+RcWcOrevVmko+T+DhoJFui+UTBGthuSd1WewBaMBpoGJKFBtqAYEDotuUWYLQadFstdbclWpJlaxYpUaZM0pwe+Qa+qebMyjEiMuY7nXP23qv/2CciI2t6WVmZWZlZ9ytkRcQdz7333O/ss9a3vk9UlTnmmGOOOZ4smA97A+aYY4455rj/mJP7HHPMMccTiDm5zzHHHHM8gZiT+xxzzDHHE4g5uc8xxxxzPIGYk/scc8wxxxOIB0buIvITIvKSiLwqIn/xQT3PHHPMMcccb4c8CJ27iFjgZeAPAleB3wJ+RlW/ed+fbI455phjjrfhQa3cvw94VVVfV9UK+Dngiw/oueaYY4455ngL3AN63LPAlRN/XwW+/91uLCLzMdk55phjjvePW6q6+k5XPChyl3e47A4CF5F/H/j3H9DzP+IQ3vktut9Q3vK2P4J4WO/Fe+G93qd3277v9N4+Cq/rcca9vu8fNh72fhEvv9s1D4rcrwLnT/x9Drh+8gaq+rPAz8J85T7HHHPMcb/xoGruvwV8TESeFpEc+FPAP3pAzzXHHHPMMcdb8EBW7qrqReTPAz8PWOBvqOo3HsRzzTHHHPcGkVQemDvDPpl4IFLI970RH7myzLzmfhuPQm36o1lzf7TJfV5zvzvE31bVz7/TNQ+q5j7HP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figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Python program to read\n",
|
||
|
"# image using matplotlib\n",
|
||
|
"\n",
|
||
|
"# importing matplotlib modules\n",
|
||
|
"import matplotlib.image as mpimg\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"\n",
|
||
|
"# Read Images\n",
|
||
|
"img = mpimg.imread('therestofme.jpg')\n",
|
||
|
"\n",
|
||
|
"# Output Images\n",
|
||
|
"print(\"~~~~~~~~~~~~~~~~~~~~movie still from The Silence of Lamb to for a pair of anagrams ~~~~~~~~~~~~\")\n",
|
||
|
"plt.imshow(img)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 27,
|
||
|
"id": "3df449e7-93d0-499b-8ea5-84bdba8cd165",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"True"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 27,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"areAnagrams(\"hestermofet\",\"therestofme\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 28,
|
||
|
"id": "b517982e-ef53-4424-8a2c-5baf2cdccb1d",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"False"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 28,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"areAnagrams(\"hester mofet\",\"the rest of me\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "7af1e993-ca08-41eb-924e-d66b44a3480d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"in this case the empty strings are also counted. "
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.9.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|