Skip to content

Instantly share code, notes, and snippets.

@AdamStelmaszczyk
Created May 12, 2020 18:38
Show Gist options
  • Save AdamStelmaszczyk/8a35ad8a727b7ef6f70c813b997f8e95 to your computer and use it in GitHub Desktop.
Save AdamStelmaszczyk/8a35ad8a727b7ef6f70c813b997f8e95 to your computer and use it in GitHub Desktop.
Python Speed Coding Challenge source code
# The MIT License
#
# Copyright (c) 2020 Adam Stelmaszczyk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import json
import requests
import re
from functools import reduce
from collections import defaultdict
# lines 7 to 249 had 33 task solutions, left only 1 for example
def number_to_string(a):
x = a[0]
return str(x)
# (...)
# end of task solutions
headers = {
'cookie': 'PHPSESSID=56da4f8ebccb51b621da22876106e13e',
}
params = (
('ch', '15'),
('acc', '40'),
)
data = {
'challengeSlug': 'python-scc',
'email': '',
'leaderboardName': 'Adam Stelmaszczyk',
'isConfirmedToBeContacted': '0'
}
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
s = requests.Session()
s.verify = False
response = s.post('https://speedcoding.toptal.com/webappApi/entry', headers=headers, params=params, data=data)
data = response.json()['data']
try:
attempt_id = data['attemptId']
entry_id = data['entry']['id']
entry_key = data['entry']['entry_key']
except:
print(response.text)
exit(1)
data = {
'attempt_id': attempt_id,
'entry_key': entry_key,
'tests_json': '{"test1":"1","test2":"3.141","test3":"-1.121211223","test4":"-23","test5":"9823498723"}',
'code': 'd'
}
url = 'https://speedcoding.toptal.com/webappApi/entry/{}/attemptTask'.format(entry_id)
response = requests.post(url, data=data)
# print(response.json())
def solve(response):
if 'nextTask' in response.json()['data']:
slug = response.json()['data']['nextTask']['slug'].replace('-', '_')
# print(slug)
else:
exit(0)
tests = response.json()['data']['nextTask']['tests_json']
for k, v in tests.items():
result = globals()[slug](v['args'])
tests[k] = result
data['attempt_id'] = response.json()['data']['attemptId']
data['tests_json'] = json.dumps(tests)
data['code'] = 'd'
response = s.post(url, headers=headers, data=data)
# print(response.json())
return response
while True:
# print(response.json())
response = solve(response)
# Installation commands to copy-paste in the new instance:
# curl -sL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -o conda.sh
# bash conda.sh -bfp .
# bin/conda create -n toptal -y python=3 requests
# source bin/activate toptal
# curl -sL https://gist.githubusercontent.com/AdamStelmaszczyk/8a35ad8a727b7ef6f70c813b997f8e95/raw/94abd2505caa5416a80e6d1f69a27eed08010f8c/run.py > run.py
# echo "while true; do date; time python3 run.py; sleep 7; done" > loop.sh
# chmod +x loop.sh
# nohup ./loop.sh &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment