Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. aaba aaba
  2. b b
  3. ba ba
  4.  
  5. aa
  6. abaaba
  7. bbababbb
  8. aabaaababbbaba
  9. babababa
  10. bbbbbbbbbbbb
  11. aaababbabbabbbababbaabaabaababaaba
  12. aaaabaab
  13.  
  14. a
  15. ba
  16. baab
  17. abaabaaba
  18. bbbbbbbbbbbbbbb
  19. baababbabaaaab
  20. aaaaabbaaaaa
  21.  
  22. ^((.+)2)+$
  23.  
  24. ẆŒPẋ€€2F€ċ
  25.  
  26. ẆŒPẋ€€2F€ċ Main link. Argument: s (string)
  27.  
  28. Ẇ Window, generate the array of all substrings of s.
  29. ŒP Powerset; generate all subarrays of substrings of s.
  30. ẋ€€2 Repeat each substring in each subarray twice.
  31. For example, ["ab", "a", "b"] becomes ["abab", "aa", "bb"].
  32. F€ Flatten the subarrays by concatenating their strings.
  33. ċ Count how many times s appears in the generated strings.
  34.  
  35. g(a:b:c)|a==b=g c
  36. g x=x==[]
  37. any(g.words.concat).mapM(c->[[c],c:" "])
  38.  
  39. g(a:b:c) g on list with elements a, b and tail c,
  40. |a==b in the case that a==b,
  41. =g c recurses to the tail c.
  42. g x= g on any other list x
  43. x==[] checks that x is empty.
  44. This includes the case where a is not equal
  45. to b, resulting in False.
  46. any(g.words.concat).mapM(c->[[c],c:" "]) The main function:
  47. mapM(c->[[c],c:" "]) Replace each letter c with either "c" or "c "
  48. in all possible ways, return list of results.
  49. any( ). Check that at least one result satisfies this:
  50. concat Concatenate the 1- or 2-letter strings,
  51. words. split again at each space,
  52. g. apply g.
  53.  
  54. ŒṖµœs€2ZEµ€S
  55.  
  56. ŒṖµœs€2ZEµ€S Main link. Argument: s (string)
  57.  
  58. ŒṖ Generate all partitions of s.
  59. µ µ€ Map the monadic chain between the µ's over the partitions.
  60. œs€2 Split each string in the partition into two chunks of equal length.
  61. Z Zip/transpose, collecting the first halves in one array and the
  62. second halves in another.
  63. E Test the arrays of halves for equality.
  64. S Return the sum of the results, counting the number of different
  65. ways s can be paired.
  66.  
  67. f=lambda s,t='':''<s>f(s[1:],t+s[0])|f(t and s)*f(t)>-(s==t)
  68.  
  69. sm.AqMcL2d./
  70.  
  71. f=s=>!s|[...s].some((_,i)=>i&&s[e='slice'](0,i)==s[e](i,i+=i)&&f(s[e](i)))
  72.  
  73. x=>/^((.+)2)+$/.test(x)
  74.  
  75. lye~l:1j@2zcc?
  76.  
  77. ly The list [0, …, length(Input)]
  78. e~l A list whose length is an element of the previous list
  79. :1j Append itself to this list
  80. @2zc Split in half, zip and concatenate so that the list contains pairs of
  81. consecutive identical elements
  82. c? The concatenation of that list must result in the Input
  83.  
  84. <?=preg_match("#^((.+)\2)+$#",$argv[1]);
  85.  
  86. (let((sl string-length)(ss substring))(if(odd?(sl s))(printf ".~n")(begin(let p((s s))(if(equal? s "")(printf "!")
  87. (for((i(range 1(add1(/(sl s)2)))))(when(equal?(ss s 0 i)(ss s i(* 2 i)))(p(ss s(* 2 i)(sl s)))))))(printf ".~n"))))
  88.  
  89. (define (f s)
  90. (let ((sl string-length) ; create short names of built-in fns
  91. (ss substring))
  92. (if (odd? (sl s)) (printf ".~n") ; odd length strings cannot be pairable; end here.
  93. (begin
  94. (let loop ((s s)) ; start testing here
  95. (if (equal? s "") (printf "!") ; if no remaining string, it must be pairable
  96. (for ((i (range 1 (add1 (/(sl s)2))))) ; ch lengths varying from 1 to half of string length
  97. (when (equal? (ss s 0 i) ; ch if substrings are same
  98. (ss s i (* 2 i)))
  99. (loop (ss s (* 2 i) (sl s) )))))) ; if yes, loop to check remaining string.
  100. (printf ".~n"))))) ; End of testing.
  101.  
  102. (println "Following should be pairable")
  103. (f "bbaabbaa") ; should produce 2 '!' since 2 ways are possible.
  104. (f "aa")
  105. (f "abaaba")
  106. (f "bbababbb")
  107. (f "aabaaababbbaba")
  108. (f "babababa") ; should be pairable in 2 ways.
  109. (f "bbbbbbbbbbbb") ; should be pairable in many ways.
  110. (f "aaababbabbabbbababbaabaabaababaaba")
  111. (f "aaaabaab")
  112. (println "Following should be unpairable")
  113. ; (f "a")
  114. (f "ba")
  115. (f "baab")
  116. (f "abaabaaba")
  117. (f "bbbbbbbbbbbbbbb")
  118. (f "baababbabaaaab")
  119. (f "aaaaabbaaaaa")
  120.  
  121. "Following should be pairable"
  122. !!.
  123. !.
  124. !.
  125. !.
  126. !.
  127. !!.
  128. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.
  129. !.
  130. !.
  131. "Following should be unpairable"
  132. .
  133. .
  134. .
  135. .
  136. .
  137. .
  138.  
  139. say/^((.+)2)+$/
  140.  
  141. perl -nlE 'say/^((.+)2)+$/'
  142.  
  143. say/^((.+)2)+$/
  144.  
  145. /^((.+)2)+$/ #The regex engine
  146. (.+)2 #Find any set of at least one character, followed by itself
  147. ( )+ #Do this at least one time
  148. /^ $/ #Make sure that this matches the entire string from start to end
  149. say #Output the result of the regex
  150.  
  151. $ perl -nlE 'say/^((.+)2)+$/'
  152. aaababbabbabbbababbaabaabaababaaba
  153. baababaababaaba #Truthy
  154. baababbabaaaab
  155. #Falsy
  156. bbababbb
  157. bbb #Truthy
  158. aabaaababbbaba
  159. bababa #Truthy
  160. abaabaaba
  161. #Falsy
  162.  
  163. s(X):-append(A,B,X),(A=B;A=X,B=X,s(A),s(B)).
  164.  
  165. s(X):-X=[];append(A,B,X),B=X,append(C,C,A),s(B).
  166.  
  167. | ?- s("aa").
  168. true ?
  169. yes
  170. | ?- s("aaababbabbabbbababbaabaabaababaaba").
  171. true ?
  172. yes
  173. | ?- s("baababbabaaaab").
  174. no
  175. | ?- s("bbbbbbbbbbbbbbb").
  176. no
  177.  
  178. +[<<<<,]>>>>[>+[>+[[>>]<+<[<<]>+>-]<[>+<-]>>>,>>[>>]+<<[<+>>-<-]<[>+<-]>>[,>[-<<
  179. <<]<[<<<<]>]<[[<<]>]>>]>>[[>+>>>]>>>[>]<<<<[>+[-<<<,<]<[<<<[[<<<<]>>]<]>]>>]<[[-
  180. >>>>]>>[<]<]<<]<.
  181.  
  182. +[<<<<,]
  183. >>>>
  184. [
  185. >+
  186. [
  187. >+
  188. [
  189. [>>]
  190. <+<[<<]
  191. >+>-
  192. ]
  193. <[>+<-]>
  194. >>,>>[>>]
  195. +<<[<+> >-<-]
  196. <[>+<-]>
  197. >
  198. [
  199. not equal
  200. ,>[-<<<<]
  201. <[<<<<]
  202. >
  203. ]
  204. <
  205. [
  206. equal
  207. [<<]
  208. >
  209. ]
  210. >>
  211. ]
  212. >>
  213. [
  214. mismatch
  215. [>+>>>]
  216. >>>[>]
  217. <<<<
  218. [
  219. backtrack
  220. >+[-<<<,<]
  221. <
  222. [
  223. not done yet
  224. <<<
  225. [
  226. [<<<<]
  227. >>
  228. ]
  229. <
  230. ]
  231. >
  232. ]
  233. >>
  234. ]
  235. <
  236. [
  237. match
  238. [->>>>]
  239. >>[<]
  240. <
  241. ]
  242. <<
  243. ]
  244. <.
  245.  
  246. ~c~jᵐ
  247.  
  248. ~c Reversed concatenate: find a list that, when concatenated, results in the input string
  249. This examines all partitions of the input
  250. ~jᵐ Map reversed juxtapose: for each string in that list, is it the result of concatenating
  251. a string to itself?
  252.  
  253. #S::((? (!= (null) (match S "^((.+)\2)+$")) true false))
  254.  
  255. % pairable_strings.lithp
  256. (
  257. (def f #S::((? (!= (null) (match S "^((.+)\2)+$")) true false)))
  258. (print (f "aa"))
  259. (print (f "aabaaababbbaba"))
  260. (print (f "aaababbabbabbbababbaabaabaababaaba"))
  261. (print (f "ba"))
  262. )
  263.  
  264. # ./run.js pairable_strings.lithp
  265. AtomValue { value: 2, type: 'Atom', name: 'true' }
  266. AtomValue { value: 2, type: 'Atom', name: 'true' }
  267. AtomValue { value: 2, type: 'Atom', name: 'true' }
  268. AtomValue { value: 3, type: 'Atom', name: 'false' }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement