From e077055be6e4100f228281d9ebb8d6380b938090 Mon Sep 17 00:00:00 2001 From: Jarrett Date: Tue, 16 Jul 2019 01:19:38 -0500 Subject: [PATCH] entry added an auto-interview answer. done in Jupyter Notebook python 3. --- entries/chapmanj/fizzbot-chapmanj.ipynb | 206 ++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 entries/chapmanj/fizzbot-chapmanj.ipynb diff --git a/entries/chapmanj/fizzbot-chapmanj.ipynb b/entries/chapmanj/fizzbot-chapmanj.ipynb new file mode 100644 index 0000000..444b5a2 --- /dev/null +++ b/entries/chapmanj/fizzbot-chapmanj.ipynb @@ -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 +}