Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- aaba aaba
- b b
- ba ba
- aa
- abaaba
- bbababbb
- aabaaababbbaba
- babababa
- bbbbbbbbbbbb
- aaababbabbabbbababbaabaabaababaaba
- aaaabaab
- a
- ba
- baab
- abaabaaba
- bbbbbbbbbbbbbbb
- baababbabaaaab
- aaaaabbaaaaa
- ^((.+)2)+$
- ẆŒPẋ€€2F€ċ
- ẆŒPẋ€€2F€ċ Main link. Argument: s (string)
- Ẇ Window, generate the array of all substrings of s.
- ŒP Powerset; generate all subarrays of substrings of s.
- ẋ€€2 Repeat each substring in each subarray twice.
- For example, ["ab", "a", "b"] becomes ["abab", "aa", "bb"].
- F€ Flatten the subarrays by concatenating their strings.
- ċ Count how many times s appears in the generated strings.
- g(a:b:c)|a==b=g c
- g x=x==[]
- any(g.words.concat).mapM(c->[[c],c:" "])
- g(a:b:c) g on list with elements a, b and tail c,
- |a==b in the case that a==b,
- =g c recurses to the tail c.
- g x= g on any other list x
- x==[] checks that x is empty.
- This includes the case where a is not equal
- to b, resulting in False.
- any(g.words.concat).mapM(c->[[c],c:" "]) The main function:
- mapM(c->[[c],c:" "]) Replace each letter c with either "c" or "c "
- in all possible ways, return list of results.
- any( ). Check that at least one result satisfies this:
- concat Concatenate the 1- or 2-letter strings,
- words. split again at each space,
- g. apply g.
- ŒṖµœs€2ZEµ€S
- ŒṖµœs€2ZEµ€S Main link. Argument: s (string)
- ŒṖ Generate all partitions of s.
- µ µ€ Map the monadic chain between the µ's over the partitions.
- œs€2 Split each string in the partition into two chunks of equal length.
- Z Zip/transpose, collecting the first halves in one array and the
- second halves in another.
- E Test the arrays of halves for equality.
- S Return the sum of the results, counting the number of different
- ways s can be paired.
- f=lambda s,t='':''<s>f(s[1:],t+s[0])|f(t and s)*f(t)>-(s==t)
- sm.AqMcL2d./
- f=s=>!s|[...s].some((_,i)=>i&&s[e='slice'](0,i)==s[e](i,i+=i)&&f(s[e](i)))
- x=>/^((.+)2)+$/.test(x)
- lye~l:1j@2zcc?
- ly The list [0, …, length(Input)]
- e~l A list whose length is an element of the previous list
- :1j Append itself to this list
- @2zc Split in half, zip and concatenate so that the list contains pairs of
- consecutive identical elements
- c? The concatenation of that list must result in the Input
- <?=preg_match("#^((.+)\2)+$#",$argv[1]);
- (let((sl string-length)(ss substring))(if(odd?(sl s))(printf ".~n")(begin(let p((s s))(if(equal? s "")(printf "!")
- (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"))))
- (define (f s)
- (let ((sl string-length) ; create short names of built-in fns
- (ss substring))
- (if (odd? (sl s)) (printf ".~n") ; odd length strings cannot be pairable; end here.
- (begin
- (let loop ((s s)) ; start testing here
- (if (equal? s "") (printf "!") ; if no remaining string, it must be pairable
- (for ((i (range 1 (add1 (/(sl s)2))))) ; ch lengths varying from 1 to half of string length
- (when (equal? (ss s 0 i) ; ch if substrings are same
- (ss s i (* 2 i)))
- (loop (ss s (* 2 i) (sl s) )))))) ; if yes, loop to check remaining string.
- (printf ".~n"))))) ; End of testing.
- (println "Following should be pairable")
- (f "bbaabbaa") ; should produce 2 '!' since 2 ways are possible.
- (f "aa")
- (f "abaaba")
- (f "bbababbb")
- (f "aabaaababbbaba")
- (f "babababa") ; should be pairable in 2 ways.
- (f "bbbbbbbbbbbb") ; should be pairable in many ways.
- (f "aaababbabbabbbababbaabaabaababaaba")
- (f "aaaabaab")
- (println "Following should be unpairable")
- ; (f "a")
- (f "ba")
- (f "baab")
- (f "abaabaaba")
- (f "bbbbbbbbbbbbbbb")
- (f "baababbabaaaab")
- (f "aaaaabbaaaaa")
- "Following should be pairable"
- !!.
- !.
- !.
- !.
- !.
- !!.
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.
- !.
- !.
- "Following should be unpairable"
- .
- .
- .
- .
- .
- .
- say/^((.+)2)+$/
- perl -nlE 'say/^((.+)2)+$/'
- say/^((.+)2)+$/
- /^((.+)2)+$/ #The regex engine
- (.+)2 #Find any set of at least one character, followed by itself
- ( )+ #Do this at least one time
- /^ $/ #Make sure that this matches the entire string from start to end
- say #Output the result of the regex
- $ perl -nlE 'say/^((.+)2)+$/'
- aaababbabbabbbababbaabaabaababaaba
- baababaababaaba #Truthy
- baababbabaaaab
- #Falsy
- bbababbb
- bbb #Truthy
- aabaaababbbaba
- bababa #Truthy
- abaabaaba
- #Falsy
- s(X):-append(A,B,X),(A=B;A=X,B=X,s(A),s(B)).
- s(X):-X=[];append(A,B,X),B=X,append(C,C,A),s(B).
- | ?- s("aa").
- true ?
- yes
- | ?- s("aaababbabbabbbababbaabaabaababaaba").
- true ?
- yes
- | ?- s("baababbabaaaab").
- no
- | ?- s("bbbbbbbbbbbbbbb").
- no
- +[<<<<,]>>>>[>+[>+[[>>]<+<[<<]>+>-]<[>+<-]>>>,>>[>>]+<<[<+>>-<-]<[>+<-]>>[,>[-<<
- <<]<[<<<<]>]<[[<<]>]>>]>>[[>+>>>]>>>[>]<<<<[>+[-<<<,<]<[<<<[[<<<<]>>]<]>]>>]<[[-
- >>>>]>>[<]<]<<]<.
- +[<<<<,]
- >>>>
- [
- >+
- [
- >+
- [
- [>>]
- <+<[<<]
- >+>-
- ]
- <[>+<-]>
- >>,>>[>>]
- +<<[<+> >-<-]
- <[>+<-]>
- >
- [
- not equal
- ,>[-<<<<]
- <[<<<<]
- >
- ]
- <
- [
- equal
- [<<]
- >
- ]
- >>
- ]
- >>
- [
- mismatch
- [>+>>>]
- >>>[>]
- <<<<
- [
- backtrack
- >+[-<<<,<]
- <
- [
- not done yet
- <<<
- [
- [<<<<]
- >>
- ]
- <
- ]
- >
- ]
- >>
- ]
- <
- [
- match
- [->>>>]
- >>[<]
- <
- ]
- <<
- ]
- <.
- ~c~jᵐ
- ~c Reversed concatenate: find a list that, when concatenated, results in the input string
- This examines all partitions of the input
- ~jᵐ Map reversed juxtapose: for each string in that list, is it the result of concatenating
- a string to itself?
- #S::((? (!= (null) (match S "^((.+)\2)+$")) true false))
- % pairable_strings.lithp
- (
- (def f #S::((? (!= (null) (match S "^((.+)\2)+$")) true false)))
- (print (f "aa"))
- (print (f "aabaaababbbaba"))
- (print (f "aaababbabbabbbababbaabaabaababaaba"))
- (print (f "ba"))
- )
- # ./run.js pairable_strings.lithp
- AtomValue { value: 2, type: 'Atom', name: 'true' }
- AtomValue { value: 2, type: 'Atom', name: 'true' }
- AtomValue { value: 2, type: 'Atom', name: 'true' }
- AtomValue { value: 3, type: 'Atom', name: 'false' }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement