Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 获取字符串中所有移动的k大小分区_Clojure - Fatal编程技术网

Clojure 获取字符串中所有移动的k大小分区

Clojure 获取字符串中所有移动的k大小分区,clojure,Clojure,我试图让所有的“移动”分区大小为一个字符串的k。基本上,我想沿着字符串移动一个大小为k的窗口,得到这个k字 举个例子 k:3 输入:ABDEFGH 输出:ABD、EFG、BDE、FGH、DEF 我的想法是沿着输入走,放下一个头和一个分区,然后再从以前(现在是无头)的序列中放下一个头,但我不知道到底该怎么做……还有,也许有更好的方法?下面是我的想法 (#(partition k input) (collection of s where head was consecutively dropped

我试图让所有的“移动”分区大小为一个字符串的k。基本上,我想沿着字符串移动一个大小为k的窗口,得到这个k字

举个例子

k:3

输入:ABDEFGH

输出:ABD、EFG、BDE、FGH、DEF

我的想法是沿着输入走,放下一个头和一个分区,然后再从以前(现在是无头)的序列中放下一个头,但我不知道到底该怎么做……还有,也许有更好的方法?下面是我的想法

(#(partition k input) (collection of s where head was consecutively dropped))

Clojure中的字符串可以被视为字符序列,因此可以直接对它们进行分区。要获得重叠分区的序列,请使用接受大小和步骤的版本:

user> (partition 3 1 "abcdef")
((\a \b \c) (\b \c \d) (\c \d \e) (\d \e \f))
要将字符序列放回字符串中,只需对其应用
str

user> (apply str '(\a \b \c))
"abc"
总而言之:

user> (map (partial apply str) (partition 3 1 "abcdef"))
("abc" "bcd" "cde" "def")

下面是字符串的
partition
partition all
的实现,返回字符串的惰性序列,使用
subs
进行拆分。如果您需要高性能的字符串转换,那么这些转换将比创建字符序列快得多(平均快8倍,请参见下面的标准基准)

(defn partition-string
  "Like partition, but splits string using subs and returns a
   lazy-seq of strings."
  ([n s]
     (partition-string n n s))
  ([n p s]
     (lazy-seq
      (if-not (< (count s) n)
        (cons
         (subs s 0 n)
         (->> (subs s p)
              (partition-string n p)))))))

(defn partition-string-all
  "Like partition-all, but splits string using subs and returns a
   lazy-seq of strings."
  ([n s]
     (partition-string-all n n s))
  ([n p s]
     (let [less (if (< (count s) n)
                  (count s))]
       (lazy-seq
        (cons
         (subs s 0 (or less n))
         (if-not less
           (->> (subs s p)
                (partition-string-all n p))))))))

;; Alex answer:
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (map (partial apply str) (partition 3 1 test-str)))))
;; WARNING: Final GC required 1.010207840526515 % of runtime
;; Evaluation count : 773220 in 60 samples of 12887 calls.
;; Execution time mean : 79.900801 µs
;; Execution time std-deviation : 2.008823 µs
;; Execution time lower quantile : 77.725304 µs ( 2.5%)
;; Execution time upper quantile : 83.888349 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe    3 (5.0000 %)
;; Variance from outliers : 12.5585 % Variance is moderately inflated by outliers

;; KobbyPemson answer:
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (moving-partition test-str 3))))
;; WARNING: Final GC required 1.674347646128195 % of runtime
;; Evaluation count : 386820 in 60 samples of 6447 calls.
;; Execution time mean : 161.928479 µs
;; Execution time std-deviation : 8.362590 µs
;; Execution time lower quantile : 154.707888 µs ( 2.5%)
;; Execution time upper quantile : 184.095816 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe       2 (3.3333 %)
;; low-mild         1 (1.6667 %)
;; Variance from outliers : 36.8985 % Variance is moderately inflated by outliers

;; This answer
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (partition-string 3 1 test-str))))
;; WARNING: Final GC required 1.317098148979236 % of runtime
;; Evaluation count : 5706000 in 60 samples of 95100 calls.
;; Execution time mean : 10.480174 µs
;; Execution time std-deviation : 240.957206 ns
;; Execution time lower quantile : 10.234580 µs ( 2.5%)
;; Execution time upper quantile : 11.075740 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe    3 (5.0000 %)
;; Variance from outliers : 10.9961 % Variance is moderately inflated by outliers
(defn分区字符串
类似于分区,但使用subs拆分字符串并返回
懒惰的字符串序列。”
([ns]
(分区字符串ns))
([NPS]
(续)
(如果不是(<(计数s)n)
(缺点
(潜艇s 0 n)
(->>(潜艇s-p)
(分区字符串NP‘‘‘‘‘‘‘)
(defn分区字符串全部)
与分区all类似,但使用SUB拆分字符串并返回
懒惰的字符串序列。”
([ns]
(分区字符串全部n s))
([NPS]
(让[少(如果(<(计数s)n)
(s)]
(续)
(缺点
(潜艇s 0(或小于n))
(如果不少于
(->>(潜艇s-p)
(分区字符串全部n p(()()()())))
;; 亚历克斯回答:
;; (让[测试str“abcdefghijklmnopqrstuwxyz”]
标准
(道尔)
;(映射(部分应用str)(分区3 1测试str()())))
;; 警告:最终GC需要运行时间的1.010207840526515%
;; 评估计数:12887个呼叫的60个样本中有773220个。
;; 执行时间平均值:79.900801µs
;; 执行时间标准偏差:2.008823µs
;; 执行时间下分位数:77.725304µs(2.5%)
;; 执行时间上分位数:83.888349µs(97.5%)
;; 使用的开销:17.786101纳秒
;; 在60个样本中发现3个异常值(5.0000%)
;; 低严重程度3(5.0000%)
;; 异常值方差:异常值适度夸大了12.5585%的方差
;; KobbyPemson回答:
;; (让[测试str“abcdefghijklmnopqrstuwxyz”]
标准
(道尔)
(移动分区测试str3)
;; 警告:最终GC需要1.674347646128195%的运行时间
;; 评估计数:6447个呼叫的60个样本中有386820个。
;; 执行时间平均值:161.928479µs
;; 执行时间标准偏差:8.362590µs
;; 执行时间下分位数:154.707888µs(2.5%)
;; 执行时间上分位数:184.095816µs(97.5%)
;; 使用的开销:17.786101纳秒
;; 在60个样本中发现3个异常值(5.0000%)
;; 低严重程度2(3.3333%)
;; 低轻度1(1.6667%)
;; 异常值方差:36.8985%的方差被异常值适度夸大
;; 这个答案
;; (让[测试str“abcdefghijklmnopqrstuwxyz”]
标准
(道尔)
;;(分区字符串3 1测试字符串)))
;; 警告:最终GC需要运行时间的1.317098148979236%
;; 评估计数:95100个电话的60个样本中有5706000个。
;; 平均执行时间:10.480174µs
;; 执行时间标准偏差:240.957206纳秒
;; 执行时间下分位数:10.234580µs(2.5%)
;; 执行时间上分位数:11.075740µs(97.5%)
;; 使用的开销:17.786101纳秒
;; 在60个样本中发现3个异常值(5.0000%)
;; 低严重程度3(5.0000%)
;; 与异常值的差异:异常值适度夸大了10.9961%的差异

谢谢你,亚历克斯!带阶跃的配分函数正是我所需要的。
(defn partition-string
  "Like partition, but splits string using subs and returns a
   lazy-seq of strings."
  ([n s]
     (partition-string n n s))
  ([n p s]
     (lazy-seq
      (if-not (< (count s) n)
        (cons
         (subs s 0 n)
         (->> (subs s p)
              (partition-string n p)))))))

(defn partition-string-all
  "Like partition-all, but splits string using subs and returns a
   lazy-seq of strings."
  ([n s]
     (partition-string-all n n s))
  ([n p s]
     (let [less (if (< (count s) n)
                  (count s))]
       (lazy-seq
        (cons
         (subs s 0 (or less n))
         (if-not less
           (->> (subs s p)
                (partition-string-all n p))))))))

;; Alex answer:
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (map (partial apply str) (partition 3 1 test-str)))))
;; WARNING: Final GC required 1.010207840526515 % of runtime
;; Evaluation count : 773220 in 60 samples of 12887 calls.
;; Execution time mean : 79.900801 µs
;; Execution time std-deviation : 2.008823 µs
;; Execution time lower quantile : 77.725304 µs ( 2.5%)
;; Execution time upper quantile : 83.888349 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe    3 (5.0000 %)
;; Variance from outliers : 12.5585 % Variance is moderately inflated by outliers

;; KobbyPemson answer:
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (moving-partition test-str 3))))
;; WARNING: Final GC required 1.674347646128195 % of runtime
;; Evaluation count : 386820 in 60 samples of 6447 calls.
;; Execution time mean : 161.928479 µs
;; Execution time std-deviation : 8.362590 µs
;; Execution time lower quantile : 154.707888 µs ( 2.5%)
;; Execution time upper quantile : 184.095816 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe       2 (3.3333 %)
;; low-mild         1 (1.6667 %)
;; Variance from outliers : 36.8985 % Variance is moderately inflated by outliers

;; This answer
;; (let [test-str "abcdefghijklmnopqrstuwxyz"]
;;   (criterium.core/bench
;;    (doall
;;     (partition-string 3 1 test-str))))
;; WARNING: Final GC required 1.317098148979236 % of runtime
;; Evaluation count : 5706000 in 60 samples of 95100 calls.
;; Execution time mean : 10.480174 µs
;; Execution time std-deviation : 240.957206 ns
;; Execution time lower quantile : 10.234580 µs ( 2.5%)
;; Execution time upper quantile : 11.075740 µs (97.5%)
;; Overhead used : 17.786101 ns

;; Found 3 outliers in 60 samples (5.0000 %)
;; low-severe    3 (5.0000 %)
;; Variance from outliers : 10.9961 % Variance is moderately inflated by outliers