Want to get involved? We're always looking for ideas and content for Weekly Challenges.
SUBMIT YOUR IDEASo I believe that the NBD IDs are no longer in use or they're new values so I used the community to instead use my URL plus given API Key and add the candy names to end of my url for the search. I cleaned and parsed out only the "energy" and KCAL values of each candy. However, there were many findings and KCAL values per candy. I summarized to see what was the most frequent and recurring calorie amount per candy. If you apply those most often calorie findings to the qty of each candy in their bag I found a total of 34,750 calories in their bag, which is 45 calories shy of solution.
I had one hell of a time with this challenge. The IDs are no longer valid; the API seems to return data only some of the time. I tried finding all of the relevant IDs. I found 4 of the 5. I then switched to Python since I was getting too frustrated.
Here's my solution in Python and my best result from the API.
import requests
import json
import re
import time
from usda import API_KEY
"""
DATA from Alteryx:
Candy Name Count
Sour Patch Kids 3
Snickers 15
Atomic Fireballs 15
Candy Corn 30
Sweetarts 25
"""
def usda(fdc_list: list, candy_dict: dict, regEx: str) -> None:
for id in fdc_list:
url = f'https://api.nal.usda.gov/fdc/v1/foods/?fdcIds={id}&api_key={API_KEY}'
calories = None
time.sleep(0.8)
r = requests.get(url=url)
my_json = json.loads(s=r.text)
try:
description = my_json[0]['description']
except KeyError as e:
print(f'KeyError for {e}')
if re.search(pattern=regEx, string=r.text):
calories = re.search(pattern=regEx, string=r.text).group(1)
if calories:
print(f'We collected {round(candy_dict[description] * float(calories), 2)} calories for {description}.')
# Call the function
if __name__ == '__main__':
usda(
fdc_list=[579277, 789264, 476145, 425594, 570272],
candy_dict={
'SOUR PATCH KIDS': 3,
'SNICKERS': 15,
'ATOMIC FIREBALLS': 15,
'CANDY CORN': 30,
'SWEETARTS GUMMIES': 25,
},
regEx=r'\"calories\"\:\{\"value\"\:(\d+\.?\d+)'
)