├── .gitattributes ├── .gitignore ├── README.md ├── Sol1(Detailed).py └── Sol1(Quick).py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 105 | __pypackages__/ 106 | 107 | # Celery stuff 108 | celerybeat-schedule 109 | celerybeat.pid 110 | 111 | # SageMath parsed files 112 | *.sage.py 113 | 114 | # Environments 115 | .env 116 | .venv 117 | env/ 118 | venv/ 119 | ENV/ 120 | env.bak/ 121 | venv.bak/ 122 | 123 | # Spyder project settings 124 | .spyderproject 125 | .spyproject 126 | 127 | # Rope project settings 128 | .ropeproject 129 | 130 | # mkdocs documentation 131 | /site 132 | 133 | # mypy 134 | .mypy_cache/ 135 | .dmypy.json 136 | dmypy.json 137 | 138 | # Pyre type checker 139 | .pyre/ 140 | 141 | # pytype static type analyzer 142 | .pytype/ 143 | 144 | # Cython debug symbols 145 | cython_debug/ 146 | 147 | # PyCharm 148 | # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can 149 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 150 | # and can be added to the global gitignore or merged into this file. For a more nuclear 151 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 152 | #.idea/ 153 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # REST API Hackerank 2 | My Solution the Hacker Rest API Intermediate Problem 3 | -------------------------------------------------------------------------------- /Sol1(Detailed).py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'getTotalGoals' function below. 13 | # 14 | # The function is expected to return an INTEGER. 15 | # The function accepts following parameters: 16 | # 1. STRING team 17 | # 2. INTEGER year 18 | # 19 | # Author: Teddy Oweh 20 | # Email: teddy@teddyoweh.net 21 | import requests 22 | def doac(who,data): 23 | box =[] 24 | for k in data['data']: 25 | num = int(k['team{}goals'.format(who)]) 26 | box.append(num) 27 | ans = sum(box) # Adding al the numbers in the array 28 | return ans 29 | 30 | def getTotalGoals(team, year): 31 | url1 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team1={}'.format(year,team) # First URL for when the team parsed is team1 32 | url2 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team2={}'.format(year,team) # First URL for when the team parsed is team2 33 | 34 | response1 = requests.get(url1) # API Call to the API Endpoint for the First URL 35 | response2 = requests.get(url2) # API Call to the API Endpoint for the Second URL 36 | 37 | # Decoding the API Response to JSON Format to be parsed as a dictionary then to allow easy Mutation. 38 | data1=response1.json() 39 | data2=response2.json() 40 | 41 | # Initial values for the sum of for goals on each page for the 2 URLS 42 | ans1= 0 43 | ans2= 0 44 | 45 | for i in range(data1['total_pages']): # Making a loop to repeat based of the number of pages. 46 | n_url1 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team1={}&page={}'.format(year,team,i+1) 47 | n_response1 = requests.get(n_url1) 48 | n_data1 = n_response1.json() 49 | n_1 = 1 # Indicating that this is for URL 1 50 | 51 | 52 | ans1+= doac(n_1,n_data1) # Adding the sum of goals on each page // Function (doac()) Calculates the sum of goals on each page. 53 | 54 | for k in range(data2['total_pages']): # Making a loop to repeat based of the number of pages. 55 | n_url2 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team2={}&page={}'.format(year,team,k+1) 56 | n_response2 = requests.get(n_url2) 57 | n_data2 = n_response2.json() 58 | n_2 = 2 # Indicating that this is for URL 2 59 | 60 | 61 | ans2+= doac(n_2,n_data2) # Adding the sum of goals on each page // Function (doac()) Calculates the sum of goals on each page. 62 | 63 | 64 | 65 | 66 | return ans1+ans2 # Returning the sum of goals on each page for both URLS 67 | 68 | -------------------------------------------------------------------------------- /Sol1(Quick).py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'getTotalGoals' function below. 12 | # 13 | # The function is expected to return an INTEGER. 14 | # The function accepts following parameters: 15 | # 1. STRING team 16 | # 2. INTEGER year 17 | 18 | # Author: Teddy Oweh 19 | # Email: teddy@teddyoweh.net 20 | import requests 21 | def doac(who,data1): 22 | return sum([int(k['team{}goals'.format(who)]) for k in data1['data']]) 23 | 24 | def getTotalGoals(team, year): 25 | url1 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team1={}'.format(year,team) 26 | url2 = 'https://jsonmock.hackerrank.com/api/football_matches?year={}&team2={}'.format(year,team) 27 | data1 = requests.get(url1).json() 28 | data2 = requests.get(url2).json() 29 | 30 | ans1= 0 31 | ans2= 0 32 | 33 | for i in range(data1['total_pages']): 34 | 35 | ans1+=doac(1,requests.get('https://jsonmock.hackerrank.com/api/football_matches?year={}&team1={}&page={}'.format(year,team,i+1)).json()) 36 | for k in range(data2['total_pages']): 37 | 38 | ans2+=doac(2,requests.get('https://jsonmock.hackerrank.com/api/football_matches?year={}&team2={}&page={}'.format(year,team,k+1)).json()) 39 | 40 | 41 | 42 | return ans1+ans2 43 | 44 | --------------------------------------------------------------------------------