Advertisement
Guest User

yummy

a guest
May 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int min, max
  2. bool failEVEN, failODD = false // init both sequence checks with false so we can check them each cycle
  3. vector = array of ints
  4.  
  5. foreach (var i in vector)
  6. {
  7.     // did both sequences fail already?
  8.     if (failEVEN && failODD)
  9.     {
  10.         // stop the loop as there is no point checking further...
  11.         break;
  12.     }
  13.    
  14.     // check if i is an EVEN number (using MODULO it will return 0 when it is an EVEN number (no failsafe used here as no mega numbers are expected!))
  15.     if (i % 2 == 0 && !failEVEN)
  16.     {
  17.         // is our current number bigger or the same as our last one?
  18.         if (i >= max)
  19.         {
  20.             // yeah we got a valid number.. now store it to check against the next one...
  21.             max = i;
  22.         }
  23.         else
  24.         {
  25.             // don't tell mom that we failed on our EVEN sequence... but tell our boss!
  26.             failEVEN = true;
  27.         }
  28.     }
  29.     else if (!failODD)
  30.     {
  31.         // is our current number smaller or the same as our last one?
  32.         if (i <= min)
  33.         {
  34.             // yeah we got a valid number.. now store it to check against the next one...
  35.             min = i;
  36.         }
  37.         else
  38.         {
  39.             // tell our superiors that we failed on the ODD sequence...
  40.             failODD = true;
  41.         }
  42.     }
  43. }
  44.  
  45. if (!failEVEN)
  46. {
  47.     print "EVEN sequence is valid!";
  48. }
  49. else
  50. {
  51.     print "EVEN sequence is NOT valid!";
  52. }
  53. if (!failODD)
  54. {
  55.     print "ODD sequence is valid!";
  56. }
  57. else
  58. {
  59.     print "ODD sequence is NOT valid!";
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement