Advertisement
LucasSousa

String otimizing challenge

Feb 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. This challenge will work with Strings composed by groups of pairs of numbers. All pairs are separated by slashes and all numbers in each pair are separated by a comma. This is a example of input: -2,3/5,-4/6,0/-3,0/-5,-5/0,-3/-4,0/0,-3/.
  2.  
  3. The main aim is otimize the sequence by removing the excess of 0,0 pairs. When a 0,0 is removed from the middle of the sequence the surrounding pairs should be merged into a new single one.
  4.  
  5. Look to this format:
  6. a,b/0,0/c,d
  7.  
  8. To merge pairs after remove 0,0 the new pair should be:
  9. a+c,b+d.
  10.  
  11. Obs.: The input may contains more than one 0,0 pair, in addition, it can contains severals 0,0 in a row as well.
  12.  
  13. Obs.: 0,0 can appears at begin or end of the sequence. In these cases it just need to be removed (all leading slashes are turned to only 1 -> "/0,0/1,0/" -> "/1,0/").
  14.  
  15. Example:
  16.  
  17. Input: "3,2/0,0/-2,1/";
  18. Removing 0,0: "3,2//-2,1/";
  19. Merging previous and next groups: (3+(-2),2+1): "1,3/"
  20.  
  21. ------------------------------------------
  22. Another example:
  23.  
  24. Input: "6,-1/3,1/3,3/0,0/-3,-3/-3,-1/6,1"
  25. Removing 0,0: "6,-1/3,1/3,3//-3,-3/-3,-1/6,1"
  26. Merging previous and next groups (3+(-3),3+(-3)): "6,-1/3,1/0,0/-3,-1/6,1"*
  27.  
  28. *Removing 0,0: "6,-1/3,1//-3,-1/6,1"
  29. Merging previous and next groups (3+(-3), 1+(-1)): "6,-1/0,0/6,1"*
  30.  
  31. *Removing 0,0: "6,-1//6,1"
  32. Merging previous and next groups (6+(6), -1+(1)): "12,0"*
  33.  
  34. --------------------------------------------
  35. *FORMATTING RULES (Optional challenge)
  36.  
  37. The max value for numbers should be 6. This means that, if you get a sum case
  38. wich result is > 6, you need replace it to 12 - (result).
  39. Example:
  40. 6 + 6 = 12 (result 12, wrong);
  41. 6 + 6 = 12 - (12) = 0 (this means a 12 value takes same effect a 0 value).
  42. Other example:
  43. 5 + 4 = 9 (result 9, wrong)
  44. 5 + 4 = 12 - 9 = 3 (should be -3, look observation bellow)*.
  45.  
  46. *Obs.: If the result is > 0 (positive) the final value should be negative and if the result is negative, the final result should be 12 + result, intead 12 - result. Example:
  47. -5 + -4 = -9 (wrong and operation with 12 should be +, instead - )
  48. -5 + -4 = 12 + (-9) = 3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement