Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import Foundation
  2. import Dispatch
  3.  
  4. let input = FileHandle.standardInput.readDataToEndOfFile()
  5.  
  6. var sequence = String(data: input, encoding: .utf8)!
  7.  
  8. let inputLength = input.count
  9.  
  10. let regex: (String) -> NSRegularExpression = { pattern in
  11. return try! NSRegularExpression(pattern: pattern, options: [])
  12. }
  13.  
  14. sequence = regex(">[^\n]*\n|\n").stringByReplacingMatches(in: sequence, options: [], range: NSRange(location: 0, length: inputLength), withTemplate: "")
  15.  
  16. let codeLength = sequence.utf8.count
  17.  
  18. var resultLength: Int?
  19.  
  20. let group = DispatchGroup()
  21.  
  22. DispatchQueue.global(qos: .background).async {
  23. group.enter()
  24. resultLength = [
  25. (regex: "tHa[Nt]", replacement: "<4>"),
  26. (regex: "aND|caN|Ha[DS]|WaS", replacement: "<3>"),
  27. (regex: "a[NSt]|BY", replacement: "<2>"),
  28. (regex: "<[^>]*>", replacement: "|"),
  29. (regex: "\\|[^|][^|]*\\|", replacement: "-")
  30. ].reduce(sequence) { buffer, iub in
  31. return regex(iub.regex).stringByReplacingMatches(in: buffer, options: [], range: NSRange(location: 0, length: buffer.utf16.count), withTemplate: iub.replacement)
  32. }.utf8.count
  33. group.leave()
  34. }
  35.  
  36. [
  37. "agggtaaa|tttaccct",
  38. "[cgt]gggtaaa|tttaccc[acg]",
  39. "a[act]ggtaaa|tttacc[agt]t",
  40. "ag[act]gtaaa|tttac[agt]ct",
  41. "agg[act]taaa|ttta[agt]cct",
  42. "aggg[acg]aaa|ttt[cgt]ccct",
  43. "agggt[cgt]aa|tt[acg]accct",
  44. "agggta[cgt]a|t[acg]taccct",
  45. "agggtaa[cgt]|[acg]ttaccct"
  46. ].forEach { variant in
  47. print(variant, regex(variant).numberOfMatches(in: sequence, options: [], range: NSRange(location: 0, length: sequence.utf8.count)))
  48. }
  49.  
  50. group.wait()
  51. print("", inputLength, codeLength, resultLength!, separator: "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement