Weekly Challenges

Solve the challenge, share your solution and summit the ranks of our Community!

Also available in | Français | Português | Español | 日本語
IDEAS WANTED

Want to get involved? We're always looking for ideas and content for Weekly Challenges.

SUBMIT YOUR IDEA

Challenge #139: Spooky API Connection

tammybrown_tds
8 - Asteroid
Spoiler
tammybrown_tds_0-1644620958813.png

 

TimothyH
Alteryx
Alteryx
Spoiler
TimothyH_0-1648751232224.png

 

scoles0617
8 - Asteroid

My solution:

SylwiaDragon
8 - Asteroid

That's as far as I could get - it looks like the codes don't exist anymore.

I tried typing it manually to FoodData page, and no results were found.

TonyAndriani
9 - Comet

Reposting under my new user ID.

binuacs
20 - Arcturus

solution attached

mmontgomery
11 - Bolide

Challenge #139

ARussell34
8 - Asteroid

So 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.

Luke_C
17 - Castor
Spoiler
Luke_C_0-1657804422307.png

 

acarter881
12 - Quasar

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.

Spoiler
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+)'
        )

acarter881_0-1661301845847.png