Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1.  
  2. indexOf = (a, b)->
  3. candidates = []
  4. seen = {}
  5. furthestVisited = -1
  6. seqStart = b[0]
  7. check = (idx)->
  8. valid = true
  9. for j from 1 to Math.floor b.length/2
  10. idxLeft = idx + j
  11. idxRight = b.length - j + i
  12. if idxRight >= a.length
  13. return -1
  14. left = a[idxLeft]
  15. right = a[idxRight]
  16. if !(left==b[j] and right==b[b.length - j])
  17. valid = false
  18. if idxLeft==idxRight
  19. if left == seqStart and !seen[idxLeft]
  20. seen[idxLeft] = true
  21. candidates.push idxLeft
  22. else
  23. if left == seqStart and !seen[idxLeft]
  24. seen[idxLeft] = true
  25. candidates.push idxLeft
  26. if right == seqStart and !seen[idxRight]
  27. seen[idxRight] = true
  28. candidates.push idxRight
  29. if valid
  30. return idx
  31. furthestVisited := Math.max(idx, b.length - 1)
  32. return -1
  33.  
  34. for i from 0 to a.length
  35. furthestVisited = Math.max(i, furthestVisited)
  36. if a[i] == seqStart
  37. possible = check i
  38. if possible == null
  39. return -1
  40. if possible > -1
  41. return possible
  42.  
  43. while candidates.length!=0
  44. # console.log candidates
  45. currCandidate = candidates.shift!
  46. possible = check currCandidate
  47. if possible == null
  48. return -1
  49. if possible > -1
  50. return possible
  51.  
  52. i = furthestVisited
  53.  
  54. return -1
  55.  
  56.  
  57. console.log indexOf('aaabaaaaaaaaaaab', 'aaaab')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement