2

Stretch the wordで再生するには、次の単語を定義して、この回答と同じ方法で問題に取り組みます。

USING: kernel math sequences sequences.repeating ;
IN: stretch-words

! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- ascending-seqs )
    dup length 1 + iota [ 0 swap pick subseq ] map
    [ "" = not ] filter nip ;

! expected: "bonobo" -> "bonoobbooo"
! actual:   "bonobo" -> "bbbooonnnooobbbooo"
: stretch-word ( string -- stretched ) 
    dup ascend-string swap zip
    [ 
      dup first swap last 
      [ = ] curry [ dup ] dip count 
      repeat 
    ] map last ;

stretch-word文字列内のその位置までに出現する回数だけ、文字列内の文字を繰り返すことになっています。ただし、私の実装では、取得した 1string のすべてのインスタンスを繰り返しています。

これは Factor で簡単に実装できると思いますが、よくわかりません。これを私が望むようにするにはどうすればよいですか?

4

2 に答える 2

2

うーん... 素晴らしいゴルフではありませんが、うまくいきます...

最初に、マイナーな変更を加えascend-stringて、スタックに文字列を残すようにしました。

: ascend-string ( string -- string ascending-seqs )
    dup length 1 + iota [ 0 swap pick subseq ] map
    [ "" = not ] filter ;

したがってstretch-word、次のように機能します。

: stretch-word ( string -- stretched ) 
    ascend-string zip         ! just zip them in the same order
    [ 
      first2 over             ! first2 is the only golf I could make :/
      [ = ] curry count       ! same thing
      swap <array> >string    ! make an array of char size count and make it a string
    ] map concat ;            ! so you have to join the pieces

編集:問題は繰り返しを使用して仕事をしていたと思います。

于 2016-04-24T03:19:12.107 に答える
1
: ascend-string ( string -- seqs )
    "" [ suffix ] { } accumulate*-as  ;
: counts ( string -- counts )
    dup ascend-string [ indices length ] { } 2map-as ;
: stretch-word ( string -- stretched )
    [ counts ] keep [ <string> ] { } 2map-as concat ;
"bonobo" stretch-word print
bonoobbooo

indices lengthすることもできます[ = ] with count

于 2016-06-04T12:42:16.657 に答える