-2

In Swift, I have a method that produces a number set like the following:

86.9238759555414
86.4558606813632
86.4277950105986
86.6055803862833
86.1875587264579
86.7055257286376
86.7445244949838
86.5632505027143
86.7381593407261 // This will trigger a function, because 4 consecutive numbers are within .3 range. 

A number gets added to this set every second. I would like to be able to detect when 4 consecutive numbers are within .3 of each other. In the number set above, this would occur right after the last number, 86.7381593407261, because the 3 previous numbers are all within .3 of each other.

My attempt at writing in swift is below:

Properties:

var counter = 0
var maxValue = 0.0
var minValue = 0.0

Formula:

// set max - works properly.
if currentValue > maxValue {
    maxValue = currentValue
}

// set min - not working. the min always prints 0.0
if currentValue < currentValue {
    minValue = currentValue
}

if maxValue - currentValue <= 0.3 && minValue + currentValue <= 0.3{
    //if it passes the previous 2 conditions, increment the counter and update max/min appropriately.
    counter += 1
}
// If it doesn't, reset the counter to 0, and reset max and min to the Int min and max values, respectively.
else {
    counter = 0
    // reset max and min to the Int min and max values, respectively.
    minValue = DBL_MAX
    maxValue = DBL_MIN
}
if counter == 4 {
    // celebrate
}
Joe
  • 3,772
  • 3
  • 33
  • 64
  • 3
    how is it achieved in any language? start with pseudocode for what you want and fill it out from there (then ask a specific question if you have a specific problem) – Wain Jun 07 '16 at 16:51
  • 5
    Buy a rubber duck. Set it on the desk. Explain to it in English how to achieve what you want. Once you've done that, you'll find that the computer version just falls into your lap. – matt Jun 07 '16 at 16:59
  • What are "4 consecutive numbers"? Added consecutively? Sets usually do not preserve the order of insertion. Or numbers in the set when sorted? Or don't you really mean a set in the CS sense at all? – Nikolai Ruhe Jun 07 '16 at 17:03

1 Answers1

1

Here's an outline of the solution:

  • keep three variables:
    • a count, which represents the current "streak" of passing numbers,
    • a max, which represents the max of values in the current streak,
    • a min, which represents the min of values in the current streak
  • Every time a new number comes in
    • if it's less than max, make sure it's within 0.3 of max
    • if it's greater than min, make sure it's within 0.3 of min
    • if it passes the previous 2 conditions, increment the counter and update max/min appropriately. If it doesn't, reset the counter to 0, and reset max and min to the Int min and max values, respectively.
  • when your counter reaches 4, celebrate
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • Thanks for your help here! I'm having difficulty with writing the syntax for: "make sure it's within 0.3 of max ... make sure it's within 0.3 of min." – Joe Jun 07 '16 at 18:18
  • Suppose the current value is `a`, `max` - `a` must be `<= 0.3` – Alexander Jun 07 '16 at 18:20
  • Here's a visual interpretation. (ignore that the count went over 4) http://imgur.com/FeqgoqN – Alexander Jun 07 '16 at 18:42
  • I think I am making baby steps towards the solution based on your outline! I've edited my original post with my swift attempt. I am having a tough time recording the minValue. for max, it's easy. I just set it as a class level property of 0.0. if currentValue > maxValue { maxValue = currentValue}. Additionally, i'm missing some steps in the else statement. Namely resetting max and min – Joe Jun 07 '16 at 18:53
  • `minValue + currentValue >= 0.3` should be `minValue + currentValue <= 0.3` – Alexander Jun 07 '16 at 18:54
  • When reseting `min`, set it to [`Int.max`](https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_Int_Structure/index.html#//apple_ref/swift/structdata/Int/s:ZvSi3maxSi), to ensure that it will be larger than any next possible number, and for `max` use [`Int.min`](https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_Int_Structure/index.html#//apple_ref/swift/structdata/Int/s:ZvSi3minSi) for the opposite reason. – Alexander Jun 07 '16 at 18:56
  • I've edited the original post again. I can't use Int.min / Int.max, because we're working with doubles. will DBL_MAX / DBL_MIN work? Additionally, I can't get a correct value set for minValue. Always prints 0.0. Would I be able to use the above DBL_MAX / MIN for storing the minValue / maxValue values? (and again, thank you for your help here!) – Joe Jun 07 '16 at 19:52
  • Joe, to find why minValue isn't changing, look at the code you pasted in carefully. Specifically, look at the conditional statement before you attempt to set it. Do you see any reason why it wouldn't evaluate to true? – R Moyer Jun 08 '16 at 02:56