Skip to content

Instantly share code, notes, and snippets.

@kylebgorman
Created September 23, 2022 00:08
Show Gist options
  • Save kylebgorman/24dce65ad6e147def692852b281e5fa9 to your computer and use it in GitHub Desktop.
Save kylebgorman/24dce65ad6e147def692852b281e5fa9 to your computer and use it in GitHub Desktop.
Methods I HW3 solution
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "cf084082",
"metadata": {},
"source": [
"# HW3 solution"
]
},
{
"cell_type": "markdown",
"id": "284b0bcc",
"metadata": {},
"source": [
"## Part 1: vowel counting"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "abe460c5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"449\n"
]
}
],
"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",
"# Alternatively, we could lowercase the address itself.\n",
"vowels = [\"a\", \"e\", \"i\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\"]\n",
"vcount = 0\n",
"for character in address:\n",
" if character in vowels:\n",
" vcount += 1\n",
"print(vcount)"
]
},
{
"cell_type": "markdown",
"id": "a709c4e4",
"metadata": {},
"source": [
"## Stretch goal: case folding"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "db40026b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"449\n"
]
}
],
"source": [
"address_folded = address.casefold() # This is new.\n",
"vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"] # Redefining this.\n",
"vcount = 0\n",
"for character in address_folded: # Iterating over the new variable.\n",
" if character in vowels:\n",
" vcount += 1\n",
"print(vcount)"
]
},
{
"cell_type": "markdown",
"id": "3b6d379f",
"metadata": {},
"source": [
"## Part 2: simplifying code"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db5c1b6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"9\n",
"15\n",
"21\n",
"27\n",
"33\n",
"39\n",
"45\n",
"51\n",
"57\n",
"63\n",
"69\n",
"75\n",
"81\n",
"87\n",
"93\n",
"99\n"
]
}
],
"source": [
"# Before:\n",
"for i in range(100):\n",
" if (3 * i) % 2 == 1:\n",
" if (2 * i) % 3 == 0:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0f81c6ca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"9\n",
"15\n",
"21\n",
"27\n",
"33\n",
"39\n",
"45\n",
"51\n",
"57\n",
"63\n",
"69\n",
"75\n",
"81\n",
"87\n",
"93\n",
"99\n"
]
}
],
"source": [
"# After:\n",
"for i in range(100):\n",
" if (3 * i) % 2 == 1 and not (2 * i) % 3:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"id": "d29bfcbf",
"metadata": {},
"source": [
"## Part 3: initials"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f980e187",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"KBG\n"
]
}
],
"source": [
"first_name = \"Kyle\"\n",
"middle_name = \"Benjamin\"\n",
"last_name = \"Gorman\"\n",
"print(first_name[0] + middle_name[0] + last_name[0])"
]
},
{
"cell_type": "markdown",
"id": "3e350657",
"metadata": {},
"source": [
"## Part 4: reflection"
]
},
{
"cell_type": "markdown",
"id": "24b6dfd1",
"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