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.

335 lines
8.0 KiB
Plaintext

2 years ago
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# balanced emoticons: a playful ~~critical~~ code study\n",
"\n",
"this is written for the doggies and kitties in the anthropo-sphere <br>\n",
"methods implemented were referred with caution to the following:<br>\n",
"https://www.geeksforgeeks.org/python-program-check-string-palindrome-not/\n",
"more cat emoticons: https://www.copyandpastesymbols.net/emoticon/cat-emoticons.html"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0_0 is a balanced emoticon!\n"
]
}
],
"source": [
"# palindrome method \n",
"# slice the string backwards \n",
"\n",
"def isBalanced(s):\n",
" if s == s[::-1]:\n",
" print(\"{} is a balanced emoticon!\".format(s))\n",
" else:\n",
" print(\"{} is not a balanced emoticon, try another one?\".format(s))\n",
"\n",
"# try a simple emoticon\n",
"suprised_f = \"0_0\"\n",
"isBalanced(suprised_f)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(*♥^•ﻌ•^♥*) is not a balanced emoticon, try another one?\n"
]
}
],
"source": [
"cat_hearty = \"(*♥^•ﻌ•^♥*)\"\n",
"isBalanced(cat_hearty)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*♥^•ﻌ•^♥* is a balanced emoticon!\n"
]
}
],
"source": [
"#strip the opening brackets and closing brackets \n",
"s_cat_hearty = cat_hearty.strip(\"()\")\n",
"isBalanced(s_cat_hearty)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#update the isBalanced method \n",
"def isBalanced(s):\n",
" if s == s[::-1]:\n",
" print(\"{} is a balanced emoticon!\".format(s))\n",
" else:\n",
" s_strip = s.strip(\"()\")\n",
" if s_strip == s_strip[::-1]:\n",
" print(\"{} is a balanced emoticon!\".format(s))\n",
" else:\n",
" print(\"{} is not a balanced emoticon, try another one?\".format(s))\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(*♥^•ﻌ•^♥*) is a balanced emoticon!\n"
]
}
],
"source": [
"#try with the updated method \n",
"isBalanced(cat_hearty)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ლ(●ↀωↀ●)ლ is not a balanced emoticon, try another one?\n",
"\n",
"(゚∀゚) is a balanced emoticon!\n",
"\n",
"(=^・ω・^=) is a balanced emoticon!\n",
"\n",
"(^・x・^) is a balanced emoticon!\n",
"\n"
]
}
],
"source": [
"# try a list of kitty emoji strings\n",
"c_strings_l = [\"ლ(●ↀωↀ●)ლ\",\"(゚∀゚)\",\"(=^・ω・^=)\",\"(^・x・^)\"]\n",
"for string in c_strings_l:\n",
" isBalanced(string)\n",
" print()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"▼・ᴥ・▼ is a balanced emoticon!\n",
"\n",
"U・ﻌ・U is a balanced emoticon!\n",
"\n",
" υ´• ﻌ •`υ is not a balanced emoticon, try another one?\n",
"\n",
"ᐡ ・ ﻌ ・ ᐡ is a balanced emoticon!\n",
"\n"
]
}
],
"source": [
"d_strings_l = [\"▼・ᴥ・▼\",\"U・ﻌ・U\",\" υ´• ﻌ •`υ\",\"ᐡ ・ ﻌ ・ ᐡ\"]\n",
"for string in d_strings_l:\n",
" isBalanced(string)\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ლ)●ↀωↀ●(ლ\n"
]
}
],
"source": [
"#why would it not be? \n",
"wide_eyes = \"ლ(●ↀωↀ●)ლ\"\n",
"r_wide_eyes = wide_eyes[::-1]\n",
"s_r_wide_eyes = r_wide_eyes.strip(\"()\")\n",
"print(s_r_wide_eyes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"in the kitty emoji, the strip method won't work now since it only applies to stripping away leading or trailing chars. therefore the method is buggy, as the pair of brackets are not matching. similarily, in the doggie emoji, the pair of ticks that make up the eyebrows are not assigned as matching. "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Half loop comparison says failing test\n",
"it's going to print half a doggie tree watch out\n",
"υ\n",
"´υ\n",
"•´υ\n",
" •´υ\n",
"ﻌ •´υ\n",
" ﻌ •´υ\n",
"• ﻌ •´υ\n",
"`• ﻌ •´υ\n",
"υ`• ﻌ •´υ\n"
]
}
],
"source": [
"# bonus program\n",
"# printing emoticon trees\n",
"# the program will fail \n",
"\n",
"# *******************************\n",
"# * run a half loop comparison * \n",
"# *******************************\n",
"\n",
"def isPalindrome(str):\n",
" \n",
" # Run loop from 0 to len/2\n",
" for i in range(0, int(len(str)/2)):\n",
" if str[i] != str[len(str)-i-1]:\n",
" return False\n",
" return True\n",
" \n",
"# main function\n",
"s = \" υ´• ﻌ •`υ\"\n",
"ans = isPalindrome(s)\n",
" \n",
"if (ans):\n",
" print(\"Half loop comparison says passing test\")\n",
"else:\n",
" print(\"Half loop comparison says failing test\")\n",
" \n",
"# *******************************\n",
"# * move emoticon to a new var * \n",
"# *******************************\n",
"\n",
"# Python program to check\n",
"# if a string is palindrome\n",
"# or not\n",
"\n",
"x = \"υ´• ﻌ •`υ\"\n",
"\n",
"w = \"\"\n",
"z = \"\"\n",
"y = \"\"\n",
"\n",
"print(\"it's going to print half a doggie tree watch out\")\n",
"for i in x:\n",
" w = i + w\n",
" print(w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"now the eyebrows are flipped and doggie's mood changed, the ticks didn't match the original and it failed the test. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" υ\n",
" `υ\n",
" •`υ\n",
" •`υ\n",
" ﻌ •`υ\n",
" ﻌ •`υ\n",
" • ﻌ •`υ\n",
" ´• ﻌ •`υ\n",
" υ´• ﻌ •`υ\n"
]
}
],
"source": [
"#add indent to make the tree's other half \n",
"indent = \" \"\n",
"counter = len(w)\n",
"\n",
"for i in w:\n",
" z = i + z\n",
" indent = indent * counter \n",
" print(indent + z)\n",
" counter = counter - 1\n",
" indent = \" \""
]
}
],
"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": 4
}