Back-end ChallengeIn the PHP file, write a program to perform a GET request on the route https://coderbyte.com/api/challenges/json/age-counting which contains a data key and the value is a string which contains items in the format: key=STRING, age=INTEGER. Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value.Example Input{"data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"}Example Output2
Question
Back-end ChallengeIn the PHP file, write a program to perform a GET request on the route https://coderbyte.com/api/challenges/json/age-counting which contains a data key and the value is a string which contains items in the format: key=STRING, age=INTEGER. Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value.Example Input{"data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"}Example Output2
Solution
Here is a simple PHP script that performs a GET request on the specified route and counts the number of items with an age equal to or greater than 50:
<?php
// Initialize cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, "https://coderbyte.com/api/challenges/json/age-counting");
// Set the request to return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the request
$output = curl_exec($ch);
// Close cURL
curl_close($ch);
// Decode the JSON response
$response = json_decode($output, true);
// Split the data string into items
$items = explode(', ', $response['data']);
// Initialize a counter for items with age >= 50
$count = 0;
// Loop through the items
foreach ($items as $item) {
// Split the item into key and age
list($key, $age) = explode(', ', $item);
// If the age is >= 50, increment the counter
if (intval($age) >= 50) {
$count++;
}
}
// Print the final count
echo $count;
?>
This script first initializes cURL and sets the URL to the specified route. It then executes the GET request and decodes the JSON response. The data string is split into individual items, and for each item, the key and age are extracted. If the age is equal to or greater than 50, a counter is incremented. Finally, the script prints the final count.
Similar Questions
Write the output of the program below. age = 45if (age >= 35): {print ("I am Qualified")}
A list named studentAge stores age of students of a class. Write thePython command to import the required module and (using built-infunction) to display the most common age value from the given list.
Complete the main method in the AgeChecker class by Declaring and instantiating a Scanner objectPrint the "What is your age?" message to the consoleDeclaring the int age variable, assigning it the output of nextInt()If the value of age is greater than 40 then print "You are old". Otherwise, print "You are young"
7.Question 7Consider this code:12age1 = input("How old are you? ")age2 = input("How old is your best friend? ")The user enters the ages in years as whole numbers (e.g., 2). Select the code fragment(s) that print the sum of the ages. Be sure to use Python 3.1 pointprint(int(age1) + int(age2))print(int(age1) + int(age2))print(age1 + age2)print(age1 + age2)print(str(int(age1 + age2)))print(str(int(age1 + age2)))x = int(age1)x = int(age1)y = int(age2)y = int(age2)print(str(x + y))print(str(x + y))
Single File Programming QuestionProblem StatementIn a theme park, only people who meet specific criteria can experience the thrill of the roller coaster. Develop a program that takes age and height as inputs, determining eligibility using logical operators. If the age is 18 or older and the height is 150 cm or taller, output "Allowed!" Otherwise, output "Sorry, Not allowed."Input format :The first line of input consists of an integer, which represents the person's age.The second line of input consists of an integer, which represents the person's height (in cm).Output format :The output displays either "Allowed!" or "Sorry, Not allowed" based on the given conditions.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:6 ≤ age ≤ 10050 ≤ height ≤ 200Sample test cases :Input 1 :28151Output 1 :Allowed!Input 2 :17151Output 2 :Sorry, Not allowedInput 3 :6200Output 3 :Sorry, Not allowedInput 4 :18150Output 4 :Allowed!
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.