From 719121ffa5e7bd533ef1fe4d8c25379d86ddd26b Mon Sep 17 00:00:00 2001 From: Puneet Saini Date: Mon, 24 Jun 2019 01:41:37 +0530 Subject: [PATCH 1/2] Got B --- fizz3.py | 59 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/fizz3.py b/fizz3.py index cd42359..2fcb4e5 100644 --- a/fizz3.py +++ b/fizz3.py @@ -22,10 +22,11 @@ def print_response(dict): # try an answer and see what fizzbot thinks of it def try_answer(question_url, answer): print_sep() - body = json.dumps({ 'answer': answer }) + body = json.dumps({'answer': answer}) print('*** POST %s %s' % (question_url, body)) try: - req = urllib.request.Request(domain + question_url, data=body.encode('utf8'), headers={'Content-Type': 'application/json'}) + req = urllib.request.Request( + domain + question_url, data=body.encode('utf8'), headers={'Content-Type': 'application/json'}) res = urllib.request.urlopen(req) response = json.load(res) print_response(response) @@ -37,10 +38,41 @@ def try_answer(question_url, answer): print_response(response) return response +def getAnswer(question_data): + rules = question_data['rules'] + numbers = question_data['numbers'] + number = [] + response = [] + result = [0] * len(numbers) + + for rule in rules: + number.append(rule['number']) + response.append(rule['response']) + + for i in range(len(numbers)): + divBy = [] + for j in range(len(number)): + if(not numbers[i] % number[j]): + divBy.append(j) + if(divBy != []): + result[i] = '' + for j in divBy: + result[i] += response[j] + else: + result[i] = numbers[i] + + return(" ".join([str(x) for x in result])) + + # keep trying answers until a correct one is given -def get_correct_answer(question_url): +def get_correct_answer(question_url, question_data, first): while True: - answer = input('Enter your answer:\n') + if(first): + answer = 'python' + else: + answer = getAnswer(question_data) + # answer = input('Enter your answer:\n') + print(answer) response = try_answer(question_url, answer) @@ -49,28 +81,33 @@ def get_correct_answer(question_url): exit() if (response.get('result') == 'correct'): - input('press enter to continue') + # input('press enter to continue') return response.get('nextQuestion') # do the next question -def do_question(domain, question_url): +def do_question(domain, question_url, first): print_sep() print('*** GET %s' % question_url) - request = urllib.request.urlopen( ('%s%s' % (domain, question_url)) ) + request = urllib.request.urlopen(('%s%s' % (domain, question_url))) question_data = json.load(request) print_response(question_data) print_sep() next_question = question_data.get('nextQuestion') - if next_question: return next_question - return get_correct_answer(question_url) + if next_question: + return next_question + return get_correct_answer(question_url, question_data, first) def main(): question_url = '/fizzbot' + question_url = do_question(domain, question_url, True) + question_url = do_question(domain, question_url, True) while question_url: - question_url = do_question(domain, question_url) + question_url = do_question(domain, question_url, False) + -if __name__ == '__main__': main() +if __name__ == '__main__': + main() From c83c93f46dbe9eca12d4b8960bab6c28d5a4d3e9 Mon Sep 17 00:00:00 2001 From: Puneet Saini Date: Mon, 24 Jun 2019 02:23:56 +0530 Subject: [PATCH 2/2] Reduced code size --- fizz3.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/fizz3.py b/fizz3.py index 2fcb4e5..bfe7be3 100644 --- a/fizz3.py +++ b/fizz3.py @@ -43,31 +43,26 @@ def getAnswer(question_data): numbers = question_data['numbers'] number = [] response = [] - result = [0] * len(numbers) for rule in rules: number.append(rule['number']) response.append(rule['response']) for i in range(len(numbers)): - divBy = [] + res = '' for j in range(len(number)): if(not numbers[i] % number[j]): - divBy.append(j) - if(divBy != []): - result[i] = '' - for j in divBy: - result[i] += response[j] - else: - result[i] = numbers[i] + res += response[j] + if(res != ''): + numbers[i] = res - return(" ".join([str(x) for x in result])) + return(" ".join([str(x) for x in numbers])) # keep trying answers until a correct one is given -def get_correct_answer(question_url, question_data, first): +def get_correct_answer(question_url, question_data, firstTwo): while True: - if(first): + if(firstTwo): answer = 'python' else: answer = getAnswer(question_data) @@ -85,7 +80,7 @@ def get_correct_answer(question_url, question_data, first): return response.get('nextQuestion') # do the next question -def do_question(domain, question_url, first): +def do_question(domain, question_url, firstTwo): print_sep() print('*** GET %s' % question_url) @@ -98,7 +93,7 @@ def do_question(domain, question_url, first): if next_question: return next_question - return get_correct_answer(question_url, question_data, first) + return get_correct_answer(question_url, question_data, firstTwo) def main():