├── .gitignore ├── LICENSE.txt ├── README.md ├── hackerrank ├── HackerRankAPI.py ├── __init__.py ├── settings.py └── testing.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # PyInstaller 26 | # Usually these files are written by a python script from a template 27 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 28 | *.manifest 29 | *.spec 30 | 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | htmlcov/ 37 | .tox/ 38 | .coverage 39 | .cache 40 | nosetests.xml 41 | coverage.xml 42 | 43 | # Translations 44 | *.mo 45 | *.pot 46 | 47 | # Django stuff: 48 | *.log 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Nikhil Kumar Singh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![PyPI](https://img.shields.io/badge/PyPi-v1.2.4-f39f37.svg)](https://pypi.python.org/pypi/hackerrank-sdk) 2 | [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/nikhilkumarsingh/hackerrank-sdk/blob/master/LICENSE.txt) 3 | 4 | 5 | hackerrank-sdk 6 | ============== 7 | An Unofficial Python Client that supports interaction with [HackerRank API](https://www.hackerrank.com/api/docs). 8 | 9 | This SDK provides integration with HackerRank API for compiling and running code in several languages. It can be accessed via a simple API key based authorization process.[Get your api-key from here](https://www.hackerrank.com/api/docs). 10 | 11 | The code is Python 2, but Python 3 compatible. 12 | 13 | 14 | ## Installation 15 | 16 | Fast install: 17 | 18 | pip install hackerrank-sdk 19 | 20 | For a manual install get this package: 21 | 22 | wget https://github.com/nikhilkumarsingh/hackerrank-sdk/archive/master.zip 23 | unzip master.zip 24 | rm master.zip 25 | cd hackerrank-sdk-master 26 | 27 | Install the package: 28 | 29 | python setup.py install 30 | 31 | ## Examples 32 | ```python 33 | from hackerrank.HackerRankAPI import HackerRankAPI 34 | 35 | API_KEY = '' #your API-KEY here 36 | 37 | compiler = HackerRankAPI(api_key = API_KEY) 38 | 39 | print compiler.supportedlanguages() #prints a list of supported languages 40 | 41 | 42 | source = '''print "hello"''' #give your source code here 43 | 44 | ''' 45 | Alternatively,you can copy existing files to source this way: 46 | with open(file_name,'r') as f: 47 | source = f.read() 48 | ''' 49 | 50 | result = compiler.run({'source': source, 51 | 'lang':'python' 52 | }) 53 | 54 | 55 | print(result.output,result.time,result.memory,result.message) #get different variables associated with the result 56 | ``` 57 | Testcases are passed as a list of strings. 58 | 59 | Here is another example which shows how to give testcases to the compiler: 60 | ```python 61 | from hackerrank.HackerRankAPI import HackerRankAPI 62 | 63 | API_KEY = '' # Your API-KEY here 64 | 65 | compiler = HackerRankAPI(api_key = API_KEY) 66 | 67 | source =''' 68 | N, M = map(int,raw_input().split()) 69 | for i in xrange(1,N,2): 70 | print (".|."*i).center(M,'-') 71 | 72 | print "WELCOME".center(M,'-') 73 | for i in xrange(N-2,-1,-2): 74 | print (".|."*i).center(M,'-') 75 | ''' 76 | 77 | result = compiler.run({'source': source, 78 | 'lang':'python', 79 | 'testcases':["9 27"] 80 | }) 81 | 82 | print(result.output[0]) 83 | ``` 84 | Here is the output: 85 | 86 | ![output](http://i.imgur.com/D9vbr1Z.png) 87 | -------------------------------------------------------------------------------- /hackerrank/HackerRankAPI.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import requests 3 | import json 4 | from hackerrank.settings import RUN_API_ENDPOINT 5 | from hackerrank.settings import LANG_CODE 6 | 7 | 8 | class HackerRankAPI(): 9 | # initialize the API object 10 | def __init__(self, api_key): 11 | self.params_dict = {} 12 | self.params_dict['api_key'] = api_key 13 | self.params_dict['format'] = 'json' 14 | 15 | # run given piece of code 16 | def run(self, code): 17 | self.manage_params(code) 18 | response = self.__request(RUN_API_ENDPOINT, self.params_dict) 19 | result = Result(response.json()['result']) # create a result object of Result class 20 | return result 21 | 22 | # update params_dict with code data 23 | def manage_params(self, code): 24 | self.params_dict['source'] = code['source'] 25 | self.params_dict['lang'] = self.getLangCode(code['lang']) 26 | if 'testcases' in code: 27 | self.params_dict['testcases'] = json.dumps(code['testcases']) 28 | else: 29 | self.params_dict['testcases'] = json.dumps([""]) # empty testcase 30 | 31 | # send API request 32 | def __request(self,url,params): 33 | try: 34 | response = requests.post(url, data=params) 35 | return response 36 | except Exception as e: 37 | print(e) 38 | 39 | # utility function to get language code to be passed as parameter to API 40 | def getLangCode(self, lang): 41 | try: 42 | return LANG_CODE[lang] 43 | except KeyError: 44 | print("%s language not recognized.Use function supportedlanguages() to see the list of proper names of allowed languages."%lang) 45 | return -1 46 | 47 | # get list of all supported languages 48 | def supportedlanguages(self): 49 | return LANG_CODE.keys() 50 | 51 | 52 | 53 | # to convert json to a class object of Result 54 | class Result(): 55 | def __init__(self,result): 56 | self.error = result['stderr'] 57 | self.output = result['stdout'] 58 | self.memory = result['memory'] 59 | self.time = result['time'] 60 | self.message = result['compilemessage'] 61 | -------------------------------------------------------------------------------- /hackerrank/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /hackerrank/settings.py: -------------------------------------------------------------------------------- 1 | # hackerrank API endpoint 2 | RUN_API_ENDPOINT = 'http://api.hackerrank.com/checker/submission.json' 3 | 4 | 5 | # supported languages and their codes passed to API 6 | LANG_CODE = {'fsharp': 33, 'javascript': 20, 'whitespace': 41, 'python': 5, 'lolcode': 38, 'mysql': 10, 'fortran': 54, 'tcl': 40, 'oracle': 11, 'pascal': 25, 'haskell': 12, 'cobol': 36, 'octave': 46, 'csharp': 9, 'go': 21, 'php': 7, 'ruby': 8, 'java8': 43, 'bash': 14, 'visualbasic': 37, 'groovy': 31, 'c': 1, 'erlang': 16, 'java': 3, 'd': 22, 'scala': 15, 'tsql': 42, 'ocaml': 23, 'perl': 6, 'lua': 18, 'xquery': 48, 'r': 24, 'swift': 51, 'sbcl': 26, 'smalltalk': 39, 'racket': 49, 'cpp': 2, 'db2': 44, 'objectivec': 32, 'clojure': 13, 'python3': 30, 'rust': 50} 7 | -------------------------------------------------------------------------------- /hackerrank/testing.py: -------------------------------------------------------------------------------- 1 | from hackerrank.HackerRankAPI import HackerRankAPI 2 | 3 | API_KEY = 'hackerrank|751319-994|d057e21968795c38201ca37d376201eff936f29b' 4 | 5 | compiler = HackerRankAPI(api_key = API_KEY) 6 | 7 | source =''' 8 | N, M = map(int,raw_input().split()) 9 | for i in xrange(1,N,2): 10 | print (".|."*i).center(M,'-') 11 | 12 | print "WELCOME".center(M,'-') 13 | 14 | for i in xrange(N-2,-1,-2): 15 | print (".|."*i).center(M,'-') 16 | ''' 17 | 18 | result = compiler.run({'source': source, 19 | 'lang':'python', 20 | 'testcases':["9 27"] 21 | }) 22 | print(result.output[0]) 23 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | 4 | REQUIREMENTS = [ 5 | 'requests' 6 | ] 7 | 8 | 9 | CLASSIFIERS = [ 10 | 'Development Status :: 4 - Beta', 11 | 'Intended Audience :: Developers', 12 | 'Topic :: Internet', 13 | 'License :: OSI Approved :: MIT License', 14 | 'Programming Language :: Python', 15 | 'Programming Language :: Python :: 2', 16 | 'Programming Language :: Python :: 2.6', 17 | 'Programming Language :: Python :: 2.7', 18 | 'Programming Language :: Python :: 3', 19 | 'Programming Language :: Python :: 3.3', 20 | 'Programming Language :: Python :: 3.4', 21 | 'Programming Language :: Python :: 3.5', 22 | ] 23 | 24 | setup(name='hackerrank-sdk', 25 | version='1.2.4', 26 | description='Python client for HackerRank API', 27 | url='https://github.com/nikhilkumarsingh/hackerrank-sdk', 28 | author='Nikhil Kumar Singh', 29 | author_email='nikhilksingh97@gmail.com', 30 | license='MIT', 31 | packages=['hackerrank'], 32 | classifiers=CLASSIFIERS, 33 | keywords='hackerrank code compiler online api python client' 34 | ) 35 | --------------------------------------------------------------------------------