Detect whether sequence is a multiple of a subsequence in Python

前端 未结 7 1595
轮回少年
轮回少年 2021-02-07 14:44

I have a tuple of zeros and ones, for instance:

(1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1)

It turns out:

(1, 0, 1, 1, 1, 0, 1, 1, 1,          


        
7条回答
  •  逝去的感伤
    2021-02-07 14:58

    This one is just a dumb recursive comparison in Haskell. It takes about one second for Knoothe's million long string (f a). Cool problem! I'll think about it some more.

    a = concat $ replicate 20000 
        [1,1,1,0,0,1,0,1,0,0,1,0,0,1,1,1,0,0,
         0,0,0,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,
         1,1,1,0,0,1,1,1,0,0,0,0,0,1]
    
    f s = 
      f' s [] where
        f' [] result = []
        f' (x:xs) result =
          let y = result ++ [x]   
          in if concat (replicate (div (length s) (length y)) y) == s
                then y
                else f' xs y
    

提交回复
热议问题