Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fizzbot-chapmanj entry #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions entries/chapmanj/fizzbot-chapmanj.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import urllib, json, urllib.request"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"baseurl = \"https://api.noopschallenge.com\"\n",
"question = json\n",
"question_url = ''"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def get_question(qurl):\n",
" with urllib.request.urlopen(qurl) as url:\n",
" question = json.loads(url.read().decode())\n",
" return question"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def codify(rules, numbers) :\n",
" \n",
" response = '{ \"answer\": \"'\n",
" \n",
" for n in numbers :\n",
" \n",
" use_number = True\n",
" \n",
" #test \n",
" for r in rules: \n",
" if n % r['number'] == 0 :\n",
" response += r['response']\n",
" use_number = False\n",
" \n",
" if use_number :\n",
" response += str(n)\n",
" \n",
" response += ' '\n",
" \n",
" response += '\"}'\n",
" \n",
" response = response.replace(' \"}', '\"}')\n",
" \n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def postit(qurl, qanswer) :\n",
"\n",
" head={'Content-Type': 'application/json'}\n",
" \n",
" answer = qanswer.encode('utf8')\n",
" \n",
" try :\n",
" req = urllib.request.Request(qurl, data=answer, headers=head)\n",
" res = urllib.request.urlopen(req)\n",
" response = json.load(res)\n",
"\n",
" return response\n",
" \n",
" except urllib.error.HTTPError as e:\n",
" response = json.load(e)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 Thank you for your application to Noops Inc.\n",
"\n",
"Our automated fizzbot interview process will help us determine if you have what it takes to become a Noop.\n",
"\n",
"For each question, you will GET the question and then give us the answer back to the same URL.\n",
"You will also find the URL for the next question in the nextQuestion parameter for each response.\n",
"\n",
"The first question is at https://api.noopschallenge.com/fizzbot/questions/1.\n",
"\n",
"Good Luck\n",
"\n",
"1 https://api.noopschallenge.com/fizzbot/questions/1\n",
"{'message': 'What is your favorite programming language?\\nMine is COBOL, of course.\\nPOST your answer back to this URL in JSON format. If you are having difficulties, see the exampleResponse provided.', 'exampleResponse': {'answer': 'COBOL'}}\n",
"{'result': 'correct', 'message': 'Of course. How interesting. Are you ready for your first REAL question?', 'nextQuestion': '/fizzbot/questions/DvfDePgar3d9XYxH95KkWVEJeeGOJLS8vRH7SHVrvxQ'}\n",
"1 correct\n",
"2 correct\n",
"3 correct\n",
"4 correct\n",
"5 correct\n",
"6 correct\n",
"7 interview complete\n"
]
}
],
"source": [
"#answer the first few questions\n",
"question = get_question(baseurl + \"/fizzbot\")\n",
"print(0, question['message'])\n",
"\n",
"question_url = baseurl + question['nextQuestion']\n",
"print(1, question_url)\n",
"\n",
"question = get_question(question_url)\n",
"print(question)\n",
"\n",
"ans='{\"answer\" : \"C#\"}'\n",
"response = postit(question_url, ans)\n",
"print(response)\n",
"\n",
"#answer pattern questions\n",
"i = 1\n",
"while response.get('result') != 'interview complete':\n",
" \n",
" #get and set question\n",
" question = get_question(baseurl + response['nextQuestion'])\n",
" #verify\n",
" #print(i, question)\n",
" \n",
" #additional feedback\n",
"# #print message\n",
"# print(i, question['message'])\n",
"# #display rules\n",
"# print(i, question['rules'])\n",
"# #display numbers\n",
"# print(i, question['numbers'])\n",
" \n",
" #make ans\n",
" ans = codify(question['rules'], question['numbers'])\n",
"# print(i, ans)\n",
" \n",
" #post\n",
" response = postit(question_url, ans)\n",
"# print (i, response)\n",
" \n",
" #feedback\n",
" print(i, response['result'])\n",
" \n",
" #stopping condition\n",
" if response.get('result') == 'interview complete' :\n",
" break\n",
" \n",
" #set next \n",
" question_url = baseurl + response['nextQuestion']\n",
" \n",
" i = i + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}