[Programming Fundamentals] Lambda and Linq - Exercise - 06. CottageScraper
Здравейте колеги,
Имам проблем с 6-та задача от Lambda and LINQ - Exercise. Задачата ми дава 70 от 100 точки. Мисля, че проблема може да е в закръглянето. Имате ли идея как да реша задачата? Благодаря предварително.
-Решението ми - https://pastebin.com/XjR68XWL
6.CottageScraper *
You’re a carpenter at the local woodworking shop. Your client wants you to build a wooden skyscraper/cottage. A CottageScraper, he calls it. But in order to accomplish this task, he needs the trees to be taller than a certain height. The problem is that he doesn’t know what type of tree he’ll use yet, and doesn’t know how tall he wants the CottageScraper to be. You’ve obviously got nothing better to do, so you go to work chopping down logs until he calls you with the details.
Write a program which receives tree types and their lengths in the format “{type} -> {height}”. When you receive the command “chop chop”, it’s time to get paid.
On the next line, you will receive the type of tree that needs to be used to build the CottageScraper. On the final input line, you will receive the minimum length per tree, needed to accomplish the task. Filter the trees based on type and minimum length, making sure that you’ll only use the trees of the specified type and minimum length.
After which, calculate the total price of the CottageScraper, which includes the price of all the trees you collected up to that point. The price is calculated as being the average meters of all logs you collected, per meter of log, rounded to the second decimal place.
You’re going to charge the client 100% of the price per log for logs you’ll use in the skyscraper, and 25% of the price per log for logs you won’t use for the CottageScraper. Both fees are rounded to the second decimal place.
After you make the calculations, print them on the console in the following format:
- On the first line of the console, print “Price per meter: ${pricePerMeter:F2}”.
- On the second, print “Used logs price: ${usedLogsPrice:F2}”.
- On the third line, print “Unused logs price: ${unusedLogsPrice:F2}”.
- On the final line of the output, print “CottageScraper subtotal: ${subTotal:F2}”.
Examples
Input |
Output |
Comments |
Maple -> 20 Oak -> 12 Poplar -> 25 Maple -> 33 Poplar -> 11 Poplar -> 30 chop chop Poplar 12 |
Price per meter: $21.83 Used logs price: $1200.65 Unused logs price: $414.77 CottageScraper subtotal: $1615.42 |
Needed type: Poplar Needed height: at least 12m
Price per meter == (sum of all logs) / (count) == 21.83
Used logs (taller than 12m): Poplar -> 25, 30 Used logs price: (25 + 30) * 21.83 = $1200.65
Unused logs: Maple -> 20, 33; Oak -> 12; Poplar -> 11 Unused logs price: (20 + 33 + 12 + 11) * 21.83 * 0.25 = $414.77
Used + unused logs price: 1200.65 + 414.77 = $1615.42 |
Cherry -> 918 Oak -> 112 Maple -> 1423 Maple -> 9118 Poplar -> 122 Oak -> 232 chop chop Maple 250 |
Price per meter: $1987.50 Used logs price: $20950237.50 Unused logs price: $687675.00 CottageScraper subtotal: $21637912.50 |
Needed type: Maple Needed height: at least 250m
Price per meter = 918 + 112 + 1423 + 9118 + 122 + 232 / 6 = $1987.50
Used logs (taller than 250m): Maple -> 1423, 9118 Used logs price: (1423 + 9118) * 1987.50 = $20950237.50
Unused logs: Cherry -> 918, Oak -> 112, 232, Poplar -> 122 Unused logs price: (918+ 112+ 232+ 122) * $1987.50 * 0.25 = $687675.00
Used + unused logs price: $21637912.50 |
Apple -> 218 Pear -> 112 Apple -> 123 Apple -> 118 Pear -> 122 Cherry -> 232 chop chop Apple 120 |
Price per log: $154.00 Used logs price: $52514.00 Unused logs price: $22484.00 CottageScraper subtotal: $74998.00 |
|
Hints
- To flatten the dictionary’s values (so you can calculate the price per log), you can use the LINQ .SelectMany() method.
- The unused logs include not only the logs from different types than needed, but also the logs whose height was lower than the minimum height.
Благодаря! Linq и Lambda спестяват супер много писане.