Skip to content

Instantly share code, notes, and snippets.

@kylebgorman
Created October 11, 2022 14:17
Show Gist options
  • Save kylebgorman/478b0427de4a9431459583443774b25c to your computer and use it in GitHub Desktop.
Save kylebgorman/478b0427de4a9431459583443774b25c to your computer and use it in GitHub Desktop.
Methods I HW4 solution
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "41060f96",
"metadata": {},
"source": [
"# HW4 solution"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "da75aabe",
"metadata": {},
"outputs": [],
"source": [
"from typing import List"
]
},
{
"cell_type": "markdown",
"id": "42dc8d73",
"metadata": {},
"source": [
"## Part 1: vowel counting redux"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "880d134c",
"metadata": {},
"outputs": [],
"source": [
"def vowel_count(string: str) -> int:\n",
" \"\"\"Counts upper- and lowercase vowels (aeoiu) in a string.\"\"\"\n",
" vcount = 0\n",
" vowels = [\"a\", \"e\", \"i\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\"]\n",
" for char in string:\n",
" if char in vowels:\n",
" vcount += 1\n",
" return vcount"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5e742c46",
"metadata": {},
"outputs": [],
"source": [
"address = \"\"\"\n",
"Four score and seven years ago our fathers brought forth on this\n",
"continent, a new nation, conceived in liberty, and dedicated to the\n",
"proposition that all men are created equal. Now we are engaged in a\n",
"great civil war, testing whether that nation, or any nation so conceived\n",
"and so dedicated can long endure. We are met on a great battle-field of\n",
"that war. We have come to dedicate a portion of that field, as a final\n",
"resting place for those who here gave their lives that that nation might\n",
"live. It is altogether fitting and proper that we should do this. But,\n",
"in a larger sense, we can not dedicate---we can not consecrate---we can\n",
"not hallow---this ground. The brave men, living and dead, who struggled\n",
"here, have consecrated it, far above our poor power to add or detract.\n",
"The world will little note, nor long remember what we say here, but it\n",
"can never forget what they did here. It is for us the living, rather, to\n",
"be dedicated here to the unfinished work which they who fought here have\n",
"thus far so nobly advanced. It is rather for us to be here dedicated to\n",
"the great task remaining before us---that from these honored dead we\n",
"take increased devotion to that cause for which they gave the last full\n",
"measure of devotion---that we here highly resolve that these dead shall\n",
"not have died in vain---that this nation, under God, shall have a new\n",
"birth of freedom---and that government of the people, by the people, for\n",
"the people, shall not perish from the earth.\n",
"\"\"\"\n",
"assert vowel_count(address) == 449"
]
},
{
"cell_type": "markdown",
"id": "05326d52",
"metadata": {},
"source": [
"## Part 2: case and shape coding"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f6deb57b",
"metadata": {},
"outputs": [],
"source": [
"def sentence_case_shape(tokens: List[str]) -> List[str]:\n",
" \"\"\"Codes token case and shape.\n",
" \n",
" Given a list of tokens, this function codes their case (if they are\n",
" alphabetic) or shape (if they are not). Categories include:\n",
" \n",
" * upper(case alphabetic)\n",
" * title(case alphabetic)\n",
" * lower(case alphabetic)\n",
" * mixed(-case alphabetic)\n",
" * numeric (including decimals)\n",
" * other\n",
" \n",
" Args:\n",
" tokens: a list of strings.\n",
" \n",
" Returns:\n",
" a list of the case/shape codes of the same length.\n",
" \"\"\"\n",
" codes = []\n",
" for token in tokens:\n",
" if token.isupper():\n",
" codes.append(\"upper\")\n",
" elif token.istitle():\n",
" codes.append(\"title\")\n",
" elif token.islower():\n",
" codes.append(\"lower\")\n",
" elif token.isalpha():\n",
" codes.append(\"mixed\")\n",
" elif token.isnumeric() or token.isdecimal():\n",
" codes.append(\"numeric\")\n",
" else:\n",
" codes.append(\"other\")\n",
" assert len(tokens) == len(codes), \"mismatched lengths\"\n",
" return codes"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cd8a4cf4",
"metadata": {},
"outputs": [],
"source": [
"sentence = [\"Ol'\", \"McDonald\", \"had\", \"a\", \"farm\", \".\"]\n",
"codes = sentence_case_shape(sentence)\n",
"assert codes == [\"title\", \"mixed\", \"lower\", \"lower\", \"lower\", \"other\"]"
]
},
{
"cell_type": "markdown",
"id": "d347a5a4",
"metadata": {},
"source": [
"## Part 3: reflection"
]
},
{
"cell_type": "markdown",
"id": "81a7777a",
"metadata": {},
"source": [
"Your reflection here."
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment