Skip to content

Commit

Permalink
homework hack
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypernova101 committed Sep 26, 2024
1 parent a33cfb3 commit 452274d
Showing 1 changed file with 203 additions and 0 deletions.
203 changes: 203 additions & 0 deletions _notebooks/Foundation/2024-09-25-homework_hack.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
{
"cells": [
{
"cell_type": "raw",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"toc: true\n",
"layout: post\n",
"type: hacks\n",
"courses: { compsci: {week: 14} }\n",
"title: String Operations Homework Hack\n",
"description: 3.4 Team Teach Homework Hack\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sample Hack - Password Validator\n",
"\n",
"The goal of this homework hack is to create a password validator. A couple examples are given below (Simple and Advanced)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple Password Validator"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid! Here’s a fun version: HelloWorldabc\n"
]
}
],
"source": [
"def password_validator(password):\n",
" if len(password) < 8:\n",
" return \"Password too short. Must be at least 8 characters.\"\n",
"\n",
" if password == password.lower() or password == password.upper():\n",
" return \"Password must contain both uppercase and lowercase letters.\"\n",
"\n",
" if not any(char.isdigit() for char in password):\n",
" return \"Password must contain at least one number.\"\n",
"\n",
" # Optional\n",
" password = password.replace(\"123\", \"abc\")\n",
"\n",
" words = password.split(\" \")\n",
" customized_password = \"-\".join(words)\n",
"\n",
" return f\"Password is valid! Here’s a fun version: {customized_password}\"\n",
"\n",
"# Example usage\n",
"password = \"HelloWorld123\"\n",
"print(password_validator(password))\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password must contain both uppercase and lowercase letters.\n"
]
}
],
"source": [
"print(password_validator(\"HELLO123\"))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid! Here’s a fun version: Helloabc\n"
]
}
],
"source": [
"print(password_validator(\"Hello123\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced Password Validator"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid and Strong! Here’s a fun version: HiWorld13475!\n"
]
}
],
"source": [
"import re\n",
"\n",
"def password_validator(password):\n",
" if len(password) < 8:\n",
" return \"Password too short. Must be at least 8 characters.\"\n",
" \n",
" if password == password.lower() or password == password.upper():\n",
" return \"Password must contain both uppercase and lowercase letters.\"\n",
" \n",
" if not any(char.isdigit() for char in password):\n",
" return \"Password must contain at least one number.\"\n",
" \n",
" if not re.search(r\"[!@#$%^&*()_+]\", password):\n",
" return \"Password must contain at least one special character (e.g. !, @, #, etc.)\"\n",
" \n",
" common_passwords = [\"password\", \"123456\", \"letmein\", \"qwerty\"]\n",
" if password.lower() in common_passwords:\n",
" return \"Password is too common. Choose something less predictable.\"\n",
" \n",
" sequential_patterns = [\"123\", \"abc\", \"xyz\"]\n",
" for pattern in sequential_patterns:\n",
" if pattern in password.lower():\n",
" return \"Password should not contain sequential characters like '123' or 'abc'.\"\n",
" \n",
" score = 0\n",
" if len(password) >= 10:\n",
" score += 1\n",
" if re.search(r\"[A-Z]\", password) and re.search(r\"[a-z]\", password):\n",
" score += 1\n",
" if re.search(r\"\\d\", password):\n",
" score += 1\n",
" if re.search(r\"[!@#$%^&*()_+]\", password):\n",
" score += 1\n",
"\n",
" strength = \"Weak\"\n",
" if score == 2:\n",
" strength = \"Medium\"\n",
" elif score >= 3:\n",
" strength = \"Strong\"\n",
"\n",
" password = password.replace(\"Hello\", \"Hi\")\n",
" words = password.split(\" \")\n",
" customized_password = \"-\".join(words)\n",
"\n",
" return f\"Password is valid and {strength}! Here’s a fun version: {customized_password}\"\n",
"\n",
"# Example usage\n",
"password = \"HelloWorld13475!\"\n",
"print(password_validator(password))\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 452274d

Please sign in to comment.