You work in the best consumer electronics company around. Your boss asked you to find out which products generate the most revenue.
You'll be given lists of products
, amounts
, and prices
. Given these three lists of same length, return the product name(s) with the highest revenue (amount * price
). If multiple products have the same revenue, order them according to their original positions.
Example
products: ["Computer", "Cell Phones", "Vacuum Cleaner"]
amounts: [3, 24, 8]
prices: [199, 299, 399]
return: Cell Phones
Test
products: ["Cell Phones", "Vacuum Cleaner", "Computer", "Autos", "Gold", "Fishing Rods", "Lego", " Speakers"]
amounts: [0, 12, 24, 17, 19, 23, 120, 8]
prices: [9, 24, 29, 31, 51, 8, 120, 14]
This challenge comes from JakobPri on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion (10)
Scala
And some tests
Elixir
Haskell
Javascript - Reduce method
OCaml, with Core as stdlib:
This will return
None
in case all inputs lists are empty, but raise an exception in case they aren't of equal length. That's easy to fix though, as Core defines an appropriate result type for pattern matching against:python
Here is C++ solution
Python
Haskell:
include
int main()
{
int i , a , j , revenue;
char A[15][30];
int B[30];
int C[30];
printf("enter the number of inputs you want to enter :");
scanf("%d" , &a);
printf("enter the name of the items :-\n");
for(i=-1;i<a;i++)
{
{
if(i>0)
{
if(C[i]*B[i]>C[i-1]*B[i-1])
{
revenue=C[i]*B[i];
j=i;
}
}
}
}