Computer Science, asked by gannoju15roshni, 7 months ago

hackerrank.com/tesy enrdoatmiso/questions/liom
1. Price Check
There is a shop with old-style cash registers.
Rather than scanning items and pulling the
price from a database, the price of each
item is typed in manually. This method
sometimes leads to errors. Given a list of
items and their correct prices, compare the
prices to those entered when each item was
sold. Determine the number of errors in
selling prices.
Example
products = ['eggs', 'milk, 'cheese']
productPrices = [2.89, 3.29, 5.79]
productSold = ['eggs'
, 'eggs', 'cheese' 'milk')
soldPrice = [2.89, 2.99, 5.97, 3.29].
Price
Product
Actual
Expected
Error
eggs
2.89
2.99
2.89
eggs
cheese
5.97
5.79
milk
3.29
3.29
The second sale of eggs has a wrong price,
as does the sale of cheese. There are 2 error in pricing​

Answers

Answered by ravilaccs
0

Explanation:

There is a shop with old-style cash registers. Rather than scanning items and pulling the price from a database, the price of each item is typed in manually. This method sometimes leads to errors. Given a list of items and their correct prices, compare the prices to those entered when each item was sold. Determine the number of errors in selling prices

E.g.

products = ['eggs, 'milk, 'cheese]

productPrices = [2.89, 3.29, 5.79]

productSold = ['eggs, eggs, cheese ' milk']

soldPrice = [2.89, 2.99, 5.97, 3.29]

O/P - 1 (Return an integer where productSold price is different than actual price. Here eggs is sold twice, but in 2nd sell, price is 2.99 whereas as per products array it is 2.89)

Algo -

  • Save the products to productPrices mapping in a HashMap.
  • Iterate over the productSold list and just compare the price from soldPrice array and the map created above. If values are not equal, just increment counter and at the end return counter.
  • SampleJS code for above. Write this code but below is easy to understand and short.

function main() {

 let products = ["eggs", "milk", "cheese"];

 let productPrices = [2.89, 3.29, 5.79];

 let productSold = ["eggs", "eggs", "cheese", "milk"];

 let soldPrice = [2.89, 2.99, 5.97, 3.29];

 return productSold.filter(

   (p, i) => soldPrice[i] !== productPrices[products.indexOf(p)]

 ).length;

}

2. Find # of ways to find total sum value using range 1 to k (inclusive)

E.g.

I/P - total - 5, k-3 (i.e. 1 to 3). Find number of ways (only return count).

O/P - 5

Explanation: Different ways for above test case are highlighted below:

[1+1+1+1+1]

[1+1+1+2]

[1+2+2]

[1+1+3]

[2+3]

Similar questions