Алгоритм увеличения списка цен на процент ⇐ JAVA
-
Anonymous
Алгоритм увеличения списка цен на процент
I have a task that will eventually be implemented in Java and need to come up with an algorithm of how to solve this. Unfortunately this is not my strong suit so I am looking for some advice on what I came up with.
The task is as follows: There is a list of prices, like so:
0.4 1 5.2 7 14 etc. I need to apply a price increase for all the values in the list, for example a 300% price increase. I will call this percentage value the modifier. The method will also allow for setting a max and min value for prices after the increase. For example you can set that no value is allowed to be under 10 or over 100. I will call these values lowerBound and upperBound.
The naive solution to this that I came up with is to simply traverse every item in the list, calculate the new value and if it breaks the bounds set it to the value of the corresponding limit, something like this:
Traverse list newPrice = currentPrice + (currentPrice * (modifier / 100)) if newPrice < lowerBound newPrice = lowerBound else if newPrice > upperBound new price = upperBound save new price My other idea was the find the highest and lowest value that would break the bounds, I will call these values upperBoundLimit and lowerBoundLimit. After this I can find the first value that is greater than lowerBoundLimit I can set all previous values ( assuming sorted list) to the lowerBound, then find the first value greater than upperBoundLimit and set all values after this to upperbound. After this I can proceed to calculate the remaining value in between. It would look something like this:
upperBoundLimit = upperBound / (1+modifier) lowerBoundLimit = lowerBound / (1+modifier) lowerBoundCutoff = Traverse list until first value >= lowerBoundLimit Create sublist of all values before lowerBoundCutoff set all values in sublist = lowerBound upperBoundCutoff = Traverse list until first value >= upperBoundLimit Create sublist of all values after upperBoundCutoff set all values in sublist = upperBound Traverse remaining values newPrice = currentPrice + (currentPrice * (modifier / 100)) I'm unsure if the second option is actually more efficient than the naive solution or if there are any smarter ways of doing this
Источник: https://stackoverflow.com/questions/780 ... percentage
I have a task that will eventually be implemented in Java and need to come up with an algorithm of how to solve this. Unfortunately this is not my strong suit so I am looking for some advice on what I came up with.
The task is as follows: There is a list of prices, like so:
0.4 1 5.2 7 14 etc. I need to apply a price increase for all the values in the list, for example a 300% price increase. I will call this percentage value the modifier. The method will also allow for setting a max and min value for prices after the increase. For example you can set that no value is allowed to be under 10 or over 100. I will call these values lowerBound and upperBound.
The naive solution to this that I came up with is to simply traverse every item in the list, calculate the new value and if it breaks the bounds set it to the value of the corresponding limit, something like this:
Traverse list newPrice = currentPrice + (currentPrice * (modifier / 100)) if newPrice < lowerBound newPrice = lowerBound else if newPrice > upperBound new price = upperBound save new price My other idea was the find the highest and lowest value that would break the bounds, I will call these values upperBoundLimit and lowerBoundLimit. After this I can find the first value that is greater than lowerBoundLimit I can set all previous values ( assuming sorted list) to the lowerBound, then find the first value greater than upperBoundLimit and set all values after this to upperbound. After this I can proceed to calculate the remaining value in between. It would look something like this:
upperBoundLimit = upperBound / (1+modifier) lowerBoundLimit = lowerBound / (1+modifier) lowerBoundCutoff = Traverse list until first value >= lowerBoundLimit Create sublist of all values before lowerBoundCutoff set all values in sublist = lowerBound upperBoundCutoff = Traverse list until first value >= upperBoundLimit Create sublist of all values after upperBoundCutoff set all values in sublist = upperBound Traverse remaining values newPrice = currentPrice + (currentPrice * (modifier / 100)) I'm unsure if the second option is actually more efficient than the naive solution or if there are any smarter ways of doing this
Источник: https://stackoverflow.com/questions/780 ... percentage