REST API: Energy Bar
Problem statement
Select the chocolate record from the requested country that supplies the most energy under the formula below. The original task retrieves paginated API records; this offline version receives the combined records from all pages in aligned arrays.
For record i, require an exact, case-sensitive match of countries[i] and country. Let W = floor(sum(weights[i]) / weights[i].length), P = floor(sum(prices[i]) / prices[i].length), and E = floor(kcals[i] * W / 100). Maximize E, breaking ties by smaller P. If both remain equal, choose the first input record.
Return its brands[i] + ":" + types[i]. Return an empty string if no record matches. Compute sums and products with signed 64-bit arithmetic. The source formula governs selection even though its prose calls the score energy per gram.
Function
energyBar(country: String, countries: String[], brands: String[], types: String[], kcals: int[], weights: int[][], prices: int[][]) → StringExamples
Example 1
country = "Norway"countries = ["Norway"]brands = ["Tony's Chocolonely"]types = ["Truffle"]kcals = [115]weights = [[125,71,116,100,126]]prices = [[614,283,564,365,689]]return = "Tony's Chocolonely:Truffle"The shown record has floor-average weight 107, energy floor(115*107/100)=123 and floor-average price 503.
Example 2
country = "France"countries = ["France","France","Norway"]brands = ["A","B","C"]types = ["Dark","Milk","Nut"]kcals = [101,100,1000]weights = [[100],[101],[100]]prices = [[11,10],[9],[1]]return = "B:Milk"A and B each supply energy 101. B has lower floor-average price; the Norway record is excluded.
Example 3
country = "Spain"countries = ["France"]brands = ["A"]types = ["Dark"]kcals = [100]weights = [[100]]prices = [[5]]return = ""No country matches exactly.
Constraints
- All record arrays and both matrices have the same row count
0 <= n <= 500. - Each weight or price row has
1..100entries, each in1..1000000000;1 <= kcals[i] <= 1000000000. - Country, brand and type strings contain
1..80printable ASCII characters and no colon.