- Published on
La Sonda Silenciosa - Interview challenge solution
- Authors
- Name
- Christian Guevara
- @cgTheDev
TLDR: Script
This challenge belongs to a series of programming challenges published by AltScore. You can see the complete list here. This is the first challenge.
Problem statement
¡La Sonda Silenciosa! 🛰️
Misión: Eres un intrépido explorador estelar en una misión crucial para mapear un sistema solar recién descubierto. Tu objetivo es determinar la velocidad orbital instantánea de un planeta potencialmente habitable para evaluar su idoneidad para la vida.
Desafío: La extraña interferencia cósmica en esta región del espacio dificulta la obtención de lecturas exitosas de tu escáner de largo alcance.
Datos Clave: Cuando el escáner funciona, te proporciona:
distance
: La distancia recorrida por el planeta en su órbita durante el período de observación (en unidades astronómicas).time
: El tiempo transcurrido durante la observación (en horas).
Objetivo: Calcular la velocidad orbital instantánea del planeta hasta el número entero más cercano.
Recursos:
API para obtener una lectura del escáner: [GET] /v1/s1/e1/resources/measurement (Siempre recibirás un código de estado HTTP 200, incluso si el escaneo no es exitoso).
Envía tu respuesta aquí: [POST] /v1/s1/e1/solution
Consulta la Documentación para más detalles.
¡Prepárate para desafiar la interferencia cósmica y desentrañar los secretos de este nuevo mundo! 🚀
Solution
Check the endpoint
When calling the endpoint using cURL we will always get a 200 OK even when the response is an error.
cURL call:
curl -X 'GET' \
'https://makers-challenge.altscore.ai/v1/s1/e1/resources/measurement' \
-H 'accept: application/json' \
-H 'API-KEY: SOME-API-KEY'
Error response example:
{
"distance": "failed to measure, try again",
"time": "failed to measure, try again"
}
I tried like 10 times manually and the response was the same, so it was time to automate it.
The good old bash.
Automate the endpoint call
while true; do
response=$(curl -s -X 'GET' \
'https://makers-challenge.altscore.ai/v1/s1/e1/resources/measurement' \
-H 'accept: application/json' \
-H 'API-KEY: SOME-API-KEY')
echo "Response: $response"
if [[ "$response" != '{"distance":"failed to measure, try again","time":"failed to measure, try again"}' ]]; then
echo "Output changed!"
echo "$response"
break
fi
sleep 0.3
done
After some tries I get the following answer:
...
...
...
Response: {"distance":"failed to measure, try again","time":"failed to measure, try again"}
Response: {"distance":"failed to measure, try again","time":"failed to measure, try again"}
Response: {"distance":"898 AU","time":"2.217283950617284 hours"}
Output changed!
{"distance":"898 AU","time":"2.217283950617284 hours"}
Calculate the speed
Now I need to calculate the speed which is the following formula:
Send the response
Send the response to the API endpoint for the solution:
curl -X 'POST' \
'https://makers-challenge.altscore.ai/v1/s1/e1/solution' \
-H 'accept: application/json' \
-H 'API-KEY: SOME-API-KEY' \
-H 'Content-Type: application/json' \
-d '{
"speed": 405
}'
And the response is:
{
"result": "correct"
}
TADA! 🎉