Show notification every step of difference between two values
I have to answer this question that seems a riddle ... I don't know if there's a real solution or it's impossible...
Questions : Having two double values, one is the total amount of money in the safe, the other is threshold of maximum money recommended in the safe
For example: recommended value of money (threshold) : $1,500
Total amount is a variable that is calculated every 5 seconds by a timer, in this timer tick event i have the value of the recommended value of money and the value of the total amount of money in the safe.
At the timer tick event, I need to check if the value of total money is greater than recommended value, and show a notification to the user UI. But since the timer tick event happen every 5 seconds, I need to show a notification the first time that the total amount is greater than recommended amount, and every 50$ step of difference above the threshold.
An example (every row of this example is a timer tick event) :
Total : 1200$ − Recommended : 1500$ → No Notification开发者_如何学运维
Total : 1505$ − Recommended : 1500$ → Notification (first overcoming of threshold)
Total : 1520$ − Recommended : 1500$ → No Notification
Total : 1537$ − Recommended : 1500$ → No Notification
Total : 1558$ − Recommended : 1500$ → Notification (first overcoming of 50$ step)
Total : 1574$ − Recommended : 1500$ → No Notification
Total : 1586$ − Recommended : 1500$ → No Notification
Total : 1598$ − Recommended : 1500$ → No Notification
Total : 1612$ − Recommended : 1500$ → Notification (second overcoming of 50$ step)
Total : 1623$ − Recommended : 1500$ → No Notification
And so on. Is there a way (math calculation or algorithm) to show this notification knowing only this two value, without storing any other variable in memory ?
I Can't store the "total amount" previous value in a variable.
I don't know if there's a solution but someone have passed to me this question as a riddle.
Do you have any idea if is there a solution to this question ?
You need to know the $1500 because you're outputting that. You need to know the value coming in. You also need to know the previous context, otherwise you don't know what to do with a call like
NotifyOrNot($1537)
So wherever it's stored, you need that context. There are probably ways to do it with the number of notifications, with a delegate returned from the delegate you called originally, with differences between the values, etc. - they're just different ways of storing that context. You still need a 3rd piece of memory. Or even a 4th, since you're also storing the step difference of $50.
I believe that this is homework. I'll removed all comments because of that. You'll need to figure the code out by yourself.
public class ExampleApp
{
private int _currentMoney = 1450;
private int _lastNotificationStep = 29; // 50 * 30 = 1500
[STAThread]
public static void Main(string[] argv)
{
var app = new ExampleApp();
app.InYourLoop(50);
app.InYourLoop(30);
app.InYourLoop(40);
}
public void InYourLoop(int deposited)
{
int total = _currentMoney + deposited;
var currentStep = (int) Math.Floor(total/50d);
if (_lastNotificationStep != currentStep && total >= 1500)
{
for (int step = _lastNotificationStep; step < currentStep; ++step)
{
Console.WriteLine("Notification of step: " + currentStep + " for total " + total);
_lastNotificationStep = currentStep;
}
}
_currentMoney = total;
}
}
Show N notifications, where N = (currentMoney - recommendedMoney)/step
where step = 50?
You said you can't store the last total amount in a variable, but can you store other things? I'd think you'd only need the NextThreshold value.
int NextThreshold = 1500;
while (true)
{
int CurrentBalance = GetNextBalance();
if (CurrentBalance > NextThreshold)
{
Console.WriteLine("You spent too much, foo");
while (NextThreshold < CurrentBalance)
NextThreshold += 50;
}
}
精彩评论