-
-
Save Gerst20051/77e24c4181cfac208161279bc0273500 to your computer and use it in GitHub Desktop.
<?php | |
// Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value. | |
$ch = curl_init('https://coderbyte.com/api/challenges/json/age-counting'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$json_data = json_decode($data, true); | |
$items = explode(', ', $json_data['data']); | |
$count = array_reduce($items, function ($count, $item) { | |
if (strpos($item, 'age=') !== false) { | |
$age = explode('=', $item)[1]; | |
if ($age >= 50) return $count + 1; | |
} | |
return $count; | |
}, 0); | |
print_r($count); // 128 | |
?> |
Hi, Guys. I think above all solutions are not correct for the test of coderbyte.com. It is no main problem that getting the counts of age. You must be focused on this text : "Once your function is working, take the final output string and intersperse it character-by-character with your CallengeToken. Your ChallengeToken: 03h2blxr49". What is the solution?
No, it's not, I literally run my code inside CoderByte and everything is oky.
/tmp/397740317/main.py:14: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
dfAge['age'] = dfAge['age'].astype(int)
5660
128
Here another example using pandas