Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Created June 15, 2023 15:59
Show Gist options
  • Save santiagobasulto/428dfbbd17d9a985af1dbc135be38935 to your computer and use it in GitHub Desktop.
Save santiagobasulto/428dfbbd17d9a985af1dbc135be38935 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "a440591f-eed5-4e69-a9d5-c2a645539d51",
"metadata": {},
"source": [
"### 1. The n00b"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "0e3d9612-8234-4644-a21c-fda0996da6ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is even\n",
"3 is odd\n",
"4 is even\n",
"5 is odd\n",
"6 is even\n",
"7 is odd\n",
"8 is even\n",
"9 is odd\n",
"10 is even\n",
"11 is odd\n"
]
}
],
"source": [
"# Checking for 2\n",
"if 2 % 2 == 0:\n",
" print('2 is even')\n",
"else:\n",
" print('2 is odd')\n",
"# Checking for 3\n",
"if 3 % 2 == 0:\n",
" print('3 is even')\n",
"else:\n",
" print('3 is odd')\n",
"# Checking for 4\n",
"if 4 % 2 == 0:\n",
" print('4 is even')\n",
"else:\n",
" print('4 is odd')\n",
"# Checking for 5\n",
"if 5 % 2 == 0:\n",
" print('5 is even')\n",
"else:\n",
" print('5 is odd')\n",
"# Checking for 6\n",
"if 6 % 2 == 0:\n",
" print('6 is even')\n",
"else:\n",
" print('6 is odd')\n",
"# Checking for 7\n",
"if 7 % 2 == 0:\n",
" print('7 is even')\n",
"else:\n",
" print('7 is odd')\n",
"# Checking for 8\n",
"if 8 % 2 == 0:\n",
" print('8 is even')\n",
"else:\n",
" print('8 is odd')\n",
"# Checking for 9\n",
"if 9 % 2 == 0:\n",
" print('9 is even')\n",
"else:\n",
" print('9 is odd')\n",
"# Checking for 10\n",
"if 10 % 2 == 0:\n",
" print('10 is even')\n",
"else:\n",
" print('10 is odd')\n",
"# Checking for 11\n",
"if 11 % 2 == 0:\n",
" print('11 is even')\n",
"else:\n",
" print('11 is odd')"
]
},
{
"cell_type": "markdown",
"id": "47fc1320-f3b8-4674-b866-7ce7fbbd0d80",
"metadata": {},
"source": [
"### 2. The purist"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "74ad0775-70aa-41a0-b706-68facf9c0b3a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is even\n",
"3 is odd\n",
"4 is even\n",
"5 is odd\n",
"6 is even\n",
"7 is odd\n",
"8 is even\n",
"9 is odd\n",
"10 is even\n",
"11 is odd\n"
]
}
],
"source": [
"for i in range(2, 12):\n",
" if i % 2 == 0:\n",
" print(f'{i} is even')\n",
" else:\n",
" print(f'{i} is odd')"
]
},
{
"cell_type": "markdown",
"id": "71d64d47-451d-478b-847d-ebcb78c773ee",
"metadata": {},
"source": [
"### 3. The metaprogrammer"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "3c4debbc-57a5-42e8-b49e-268fecb01fb4",
"metadata": {},
"outputs": [],
"source": [
"s = \"\"\n",
"for i in range(2, 12):\n",
" s += f\"# Checking for {i}\\n\"\n",
" s += f\"if {i} % 2 == 0:\\n\"\n",
" s += f\"{' '*4}print('{i} is even')\\n\"\n",
" s += f\"else:\\n\"\n",
" s += f\"{' '*4}print('{i} is odd')\\n\""
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "885342b0-92bc-4341-b551-395337ab6026",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is even\n",
"3 is odd\n",
"4 is even\n",
"5 is odd\n",
"6 is even\n",
"7 is odd\n",
"8 is even\n",
"9 is odd\n",
"10 is even\n",
"11 is odd\n"
]
}
],
"source": [
"# who said eval is bad 💅\n",
"exec(s)"
]
},
{
"cell_type": "markdown",
"id": "0ebaf898-3ada-46cc-9f3d-fa89314e584d",
"metadata": {},
"source": [
"### 4. The grey beard that just doesn't give a fuck anymore"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d6b723f8-0330-4005-8005-2447b8fe9ed9",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c244c43e-2218-43e7-92bc-79271df92ed7",
"metadata": {},
"outputs": [],
"source": [
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ef84fded-6e5a-41fb-81a6-06c96d837e6c",
"metadata": {},
"outputs": [],
"source": [
"MSG = \"\"\"\n",
"Write a Python program that prints the first ten numbers,\n",
"starting from 2, and if the number is even or odd.\n",
"The output of the program should be:\n",
"\n",
"2 is even\n",
"3 is odd\n",
"4 is even\n",
"5 is odd\n",
"\n",
"Output ONLY the python program, nothing else.\n",
"\"\"\"\n",
"\n",
"body = {\n",
" \"model\": \"gpt-3.5-turbo\",\n",
" \"messages\": [{\"role\": \"user\", \"content\": MSG},]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "29a09168-a06f-4b68-bf68-7bf10052c9db",
"metadata": {},
"outputs": [],
"source": [
"resp = requests.post(\n",
" \"https://api.openai.com/v1/chat/completions\",\n",
" headers={\"Authorization\": f\"Bearer {OPENAI_API_KEY}\"},\n",
" json=body\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1996cc54-381d-41ce-abec-bf84eaa10f57",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is even\n",
"3 is odd\n",
"4 is even\n",
"5 is odd\n",
"6 is even\n",
"7 is odd\n",
"8 is even\n",
"9 is odd\n",
"10 is even\n",
"11 is odd\n"
]
}
],
"source": [
"exec(resp.json()['choices'][0]['message']['content'])"
]
},
{
"cell_type": "markdown",
"id": "bd57421a-c7ea-478e-bfaf-0d3a6a364199",
"metadata": {},
"source": [
"### 4.5 The academic\n",
"\n",
"*Sometimes it works 🤷‍♂️*"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "a03b0de4-1e95-470a-a565-ee9798dbfb98",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "fe35ec03-7669-440b-9f3e-a845427279db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is even\n",
"3 is even\n",
"4 is odd\n",
"5 is even\n",
"6 is even\n",
"7 is odd\n",
"8 is even\n",
"9 is odd\n",
"10 is even\n",
"11 is odd\n"
]
}
],
"source": [
"for i in range(2, 12):\n",
" print(f\"{i} is {random.choice(['even', 'odd'])}\")"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment