街を作って


34

コーダーは常に配列を退屈な1次元エンティティにフラット化しようとしているので、悲しくなります。

あなたの仕事は、任意の文字列を展開して、素敵な街の空の景色を出力することです。

文字列を考えてみましょう: aaabbbbbccqrrssstttttttPPw

次のようになります。

            tt
            tt
  bb        tt
  bb        tt
aabb      sstt
aabbcc  rrssttPP
aabbccqqrrssttPPww

(はい、はい、文字は都市のスカイラインのように見えるように複製されています)。

入力文字列を取得し、一致する文字(必ずしもアルファベット文字である必要はありません)の各サブセクションを複製し、都市を構築します!

最短のコードバイトが優先されます。

私は実際に要件が決まっていると思っていましたが、いくつかの質問に答えるために:

  • それは地面になければなりません
  • 必要に応じて余分な空を空けることができます(先頭の空白行、周囲の空白スペース)-ただし、建物の間ではありません
  • 文字列内で文字を再利用できます(同じアーキテクチャ、異なる場所)
  • 文字はASCIIであると想定されますが、追加のエンコード(UTF8など)をサポートする文字にはより多くの才能が与えられます

3
90度回転した街並みを出力できますか?
Okx

6
キャラクターは二度と繰り返しaaabbbbaaますか?
TheLethalCoder

14
@Okxは、90度回転した街を見たことがありますか?;)
トム


10
サイトへようこそ!将来の課題については、最初にSandboxに投稿することをお勧めします。Sandboxでは、課題として投稿する前に、コミュニティからフィードバックを得ることができます。
ダダ

回答:


11

05AB1E、6バイト

γ€DζR»

オンラインでお試しください!

チャレンジより新しいバージョンでは、ζfoの代替として追加されました.Bø

05AB1E、8つのバイト

γ€D.BøR»

説明:

γ            Convert into a list of consecutive equal elements
 €D          Duplicate each element
   .B        Squarify; pad each element with spaces so that they are the length of the longest element
     ø       Transpose
      R      Reverse (otherwise the city would be upside-down)
       »     Join by newlines

オンラインでお試しください!


1
興味深いことに、ジェリーは...を持っz⁶てい.BøますがŒgx'2γ€D> _> も持っています
エリック・ザ・アウトゴルファー

γ.BD)ø˜øR»見ずに持っていたもの€Dが、はるかに優れていました。ただし、インライン複製の1バイトのソリューションが不足しているように感じます。
魔法のタコUr

3
@MagicOctopusUrn待って、それを見なくても課題を解決しましたか?
Okx

@Okxまあ、自分でゴルフをする楽しみ全体が途切れる可能性があるので、以前に答えを見ないのが賢明です。
エリックアウトゴルファー

@EriktheOutgolferそれは冗談でした、そして、私が意味することは、彼が挑戦の内容を見ないでそれを解決したということです。
Okx

6

CJam、23バイト

qe`::*:__:,:e>f{Se[}zN*

オンラインでお試しください!

説明:

qe`::*:__:,:e>f{Se[}zN* Accepts (multi-line?) input
q                       Take all input
 e`::*                  Split into groups of equal elements
      :_                Duplicate each
        _:,:e>          Push maximal length without popping
              f{Se[}    Left-pad each to that length with space strings (NOT space chars, although not a problem here)
                    z   Zip
                     N* Join with newlines

うわー、CJamの回答> _>
Xcoder氏17年

6

ゼリー、9バイト

Œgx'2z⁶ṚY

オンラインでお試しください!

説明:

Œgx'2z⁶ṚY  Main Link
Œg         Group runs of equal elements
  x        Repeat
   '              the lists
    2                       twice without wrapping
     z⁶    Zip (transpose), filling in blanks with spaces
       Ṛ   Reverse the whole thing so it's upside-down
        Y  Join by newlines

1
説明を追加してもらえますか?ここで何が起こっているのか理解できません:o
ネイサン


@HyperNeutrino素敵な説明...
エリックアウトゴルファー

確かに、それは正しいですか?:P
HyperNeutrino

@HyperNeutrinoまあ、それは完全にの意図で'はありませんでした。リスト自体を繰り返すことであり、リスト内の項目ではありませんが、全体としては良いことです。:)
エリックアウトゴルファー

6

Pythonの3155の 136 134 132バイト

@LeakyNunのおかげで-19バイト@officialaimmのおかげで
-2バイト@Wondercricketの
おかげで-1バイト

s=input()+'+'
k=' '*len(s)
a=[]
c=b=''
while s:
 while c in b:b+=c;c,*s=s
 a+=b+k,b+k;b=c
for r in[*zip(*a)][:0:-1]:print(*r,sep='')

オンラインでお試しください!



5

Java 8、412 400 330 324 312 319バイト

VisualMelonのおかげで-6バイト
、Kevin Cruijssenのおかげで-12バイト
、バイトカウントにインポートを含めるのを忘れたため+19バイト。

import java.util.*;x->{Map m=new HashMap(),n;int l=x.length(),i=l,v,y,h=0,d=1;char c,k;for(;i-->0;m.put(c,d=m.get(c)!=null?d+1:1),h=d>h?d:h)c=x.charAt(i);for(y=h;y>0;y--){n=new HashMap(m);for(i=0;i<l;i++)if(n.get(k=x.charAt(i))!=null){v=(int)m.get(k);System.out.print((y>v?"  ":k+""+k)+(i==l-1?"\n":""));n.remove(k);}}}

オンラインでお試しください!


1
ゴルフのJavaとC#(私の部署)はとても楽しいです!頑張って!あなたは事前に割り当てることができますテストされ、私はあなたがforループrejiggingで数バイトを保存することができると思いませんi=0、またはより良い、i=lとカウントダウンfor(;i-->0;h=d>h?d:h)(とかh=そこではビット)。同じバックカウントは、内側のループでも機能します。インナーifもブレースを必要としません{}。そして、常に疲れている、<=または>=、三進法を好転させて>、バイトを保存することができます。
VisualMelon

おかげで、私はあなたのヒントのおかげでコードからさらに6バイト削ることができました。まあ、私は実際にそれが好きなので、私はJava Golfingにとどまるつもりだと思う;)。
-Twometer

1
PPCGへようこそ!私はあなたが原因必要に329(19バイトまでのバイト数を増やす必要があります怖いimport java.util.*;のためMapHashMap輸入はバイト数の一部であり、;および-1ではありませんセミコロンを、末尾に除去することにより、バイトカウントの一部)。
ケビンCruijssen


1
変更の概要:HashMap<>HashMap; Map n=,nn=; m.put(c,d=m.get(c)!=null?d+1:1);forループ内でブラケットを取り除きます。k=x.charAt(i)の中にif(n.get(k)!=null)セミコロンとforループの括弧を取り除きます。繰り返しになりますが、大歓迎です!私から+1。また、ケースには、あなたはまだそれを見ていない:ヒントは、Javaでのゴルフのために<任意の言語>でゴルフをするためのヒントをお読みも面白いかもしれません。
ケビンCruijssen

5

Japt19 18 15 13 12バイト

各行の末尾にスペースが含まれます。

ò¦
íU c ·z w

試して


説明

         :Implicit input of string U
ò        :Split U to an array by ...
¦        :   checking for inequality between characters.
í        :Pair each item in U with...
U        :   The corresponding item in U (i.e, duplicate each string)
c        :Flatten the array (í creates an array of arrays).
·        :Join to a string with newlines.
z        :Rotate 90 degrees.
w        :Reverse.
         :Implicit output of resulting string.

4

Mathematica、150バイト

(z=Characters[v=#];f=CharacterCounts[v][#]&/@(d=Union@z);Row[Column/@Map[PadLeft[#,Max@f,""]&,Table[Table[d[[i]]<>d[[i]],f[[i]]],{i,Length@d}],{1}]])&

4

R、135バイト

e=rle(sub('(.)','\\1\\1',strsplit(scan(,''),'')[[1]]));write(sapply(sum(e$l|1):1,function(x)ifelse(e$l>=x,e$v,'  ')),'',sum(e$l|1),,'')

オンラインでお試しください!

stdinから読み取り、stdoutに書き込みます(末尾の改行付き)。

説明:

  • rle 文字の縞の長さ、各塔の高さを見つけます。
  • このsub式は、各文字をその二重で置き換えます(したがって、隣接するインデックスを一緒に設定することについていじる必要はありませんでした)
  • sapply 配列(この場合は行列)を返します。
    • sum(e$l|1)個別の文字の数です。私たちは上から下に行きます
    • ifelse( ... ) ベクトル化されています if...else塔と二重空間のマトリックスを構築できるようにする
    • write フォーマットするいくつかのオプションを使用して、コンソールに書き込みます。



2

MATL、15バイト

'(.)\1*'XXtvc!P

オンラインでお試しください!

説明

'(.)\1*' % Push string to be used as regexp pattern
XX       % Implicit input. Regexp matching. Pushes row cell array of matching substrings
t        % Duplicate
v        % Concatenate vertically
c        % Convert to char. This reads cells in column-major order (down, then across)
         % and produces a 2D char array, right-padding with spaces
!        % Transpose
P        % Flip vertically. Implicitly display

2

木炭、40バイト:

A⟦⟦ω⟧⟧λFθ¿⁼ι§§λ±¹¦⁰⊞§λ±¹ι⊞λ⟦ι⟧FλF²↑⁺⪫ιω¶

オンラインでお試しください!リンクは、コードの詳細バージョンです。元々、文字が変更されるたびに長方形を印刷するために入力文字列を単純にループしようとしましたが、5バイトを節約したため、このリスト構築方法に切り替えました。説明:変数lには、入力文字のネストされたリストが含まれています。現在の最後のリスト要素に一致する文字は、最後のリストにプッシュされます。そうでない場合、その文字に対して新しいサブリストが作成されます。その後、各サブリストの文字を結合して、縦に2回印刷できるようにします。


2

C、259 231バイト

ゴルフコード

#define v a[1][i
i,k,l,x,h,w;main(char*s,char**a){for(;v];w+=2*!x,s=v++],h=x>h?x:h)x=(s==v])*(x+1);h++;s=malloc((x=h++*++w+1)+w);memset(s,32,h*w);for(i=k;v];s[x+1]=s[x]=k=v++],x=k==v]?x-w:(h-1)*w+l++*2+3)s[i*w]=10;printf("%s",s);}

冗長コード

//Variable Explanations:
//i - increment through argument string, must beinitialized to 0
//k - increment through argument string, must be initialized to 0
//l - record x coordinate in return value, must be initialized to 0
//x - record the actual character position within the return string
//arrheight - the height of the return string
//arrwidth - the width of the return string
//arr - the return string
//argv - the string containing the arguments
#define v argv[1][i

i,k,l,x,arrheight,arrwidth;

main(char*arr,char**argv){
  for(;v];                                 //For Length of input
    arrwidth+=2*!x,                        //increment width by 2 if this char is not the same as the last
    arr=v++],                              //set arr to current char
    arrheight=x>arrheight?x:arrheight      //see if x is greater than the largest recorded height
  )x=(arr==v])*(x+1);                     //if this character is the same as the last, increment x (using arr to store previous char)
  arrheight++;                             //increment height by one since its 0 indexed
  arr=malloc((x=arrheight++*++arrwidth+1)+arrwidth); //create a flattened array widthxheight and set x to be the bottom left position
  memset(arr,32,arrheight*arrwidth);       //fill array with spaces
  for(i=k;v];                              //For Length of input
    arr[x+1]=arr[x]=k=v++],                //set x and x+1 positions to the current character, store current character in i
    x=k==v]?x-arrwidth:(arrheight-1)*arrwidth+l++*2+3 //if next char is same as current move vertically, else set x to bottom of next column
  )arr[i*arrwidth]=10;                     //Add new lines to string at end of width

  printf("%s",arr);                        //output string

}

GCCでコンパイルされ、特別なフラグはありません

編集

adelphusのおかげで28バイト節約されました。彼の変更により、定義を作成することができました。そして、whileループをforループにして、ループを再配置することでそれぞれ2バイト節約しました。また、入力の最後の文字がシングルトンではない場合にコードが破損する問題を修正しました。一意の文字が1つしかない場合、コードは失敗しますが、他のすべての場合に機能するはずです。


いいね!しかし、ゴルフバージョンは何らかの理由で任意の入力では機能しないようです。サンプル入力から最後の「w」を削除すると、qが失われ、文字列が繰り返されるように見えます。確かに小さなものです
...-アデルファス

while (i < strlen(argv[1]))短縮することもできますwhile (argv[1][i])
-null

@adelphus興味深いことに、チャンスがあれば明日試してみます。与えられたテストケース(私が知っている怠lazな)以外はテストしませんでした。
dj0wns

それは実際に1トンを助けました、私は問題を修正し、ほぼ30バイト減らすことができました!
-dj0wns

1

ピップ、22バイト

21バイトのコード、-lフラグの場合は+1 。

Ya@`(.)\1*`RV:yWVyZDs

オンラインでお試しください!

説明

                       a is 1st cmdline arg; s is space (implicit)
 a@`(.)\1*`            Using regex, create list of runs of same character in a
Y                      Yank that into y variable
              yWVy     Weave (interleave) y with itself to duplicate each item
                  ZDs  Zip to transpose, with a default character of space filling gaps
           RV:         Reverse the resulting list (with the compute-and-assign
                        meta-operator : being abused to lower the precedence)
                       Auto-print, one sublist per line (implicit, -l flag)

1

QuadS、15 + 1 = 16バイト

1フラグ用に+1バイト。

⊖⍵
(.)\1*
2/⍪⍵M

オンラインでお試しください!

⊖⍵ 逆さまにして後処理する

(.)\1* 同一の文字の実行

2/⍪⍵M columnified複製M ATCHを

この1フラグにより​​、結果が一緒にマージされます。


1

Haskell、144バイト

f s=let x=groupBy(==)s;l=length;m=maximum(map l x)in concatMap(++"\n")$reverse$transpose$concat[[z,z]|z<-(map(\y->y++(replicate(m-(l y))' '))x)]

私はこれよりもうまくやれると確信していますが、これは今のところ思いつく最高の方法です。


1
まず悪いニュース:Data.Listデフォルトではスコープ内にない関数を使用します。import Data.Listバイトカウントに追加するか、デフォルトでそれを含むHaskell環境を指定する必要があります(たとえば、言語をからHaskellに変更しHaskell (lambdabot)ます-いくつかのヒント:a)letヘルパー関数の代わりにパターンガードを使用して、ヘルパー関数を宣言する直接:l=length;f s|x<-groupBy(==)s,m<-... =concatMap。b)map l xl<$>x、c)concatMap("++\n"unlinesです。d)groupBy(==)はただgroupです。e)concatid=<<です。m一度しか使用しないので、インラインで
nimi

1
... F)は必要ありません()周りにl yreplicate ... ' 'そしてmap ... x。全体として:import Data.List;l=length;f s|x<-group s=unlines$reverse$transpose$id=<<[[z,z]|z<-map(\y->y++replicate(maximum(l<$>x)-l y)' ')x]
-nimi

1
groupBy(==)= group、一方はPreludeにあり、もう一方はPreludeにないかどうかはわかりません。concatMap書き込むことができ>>=、かつmapとしてinfixedすることができ<$>、そしてconcat[[z,z]|z<-…]かもしれない(replicate 2)=<<…(\z->[z,z])=<<…
Bergi

@Bergiの優れたヒントからもう1バイト削ることができます:(\z->[z,z])is (:)<*>pure、すなわち...transpose$(:)<*>pure=<<map(\y...)x
-nimi


1

ルビー、116バイト

->s{a=s.scan(/(.)(\1*)/).map{|x,y|[x,y.size+1]}.to_h
m=a.values.max
m.times{|i|puts a.map{|k,v|v+i<m ?'  ':k*2}*''}}

オンラインでお試しください!


puts a.map{...}に置き換えることができますp(a.map{})
フィリップバルトゥジ

p出力引用文字は、それがここにフィットしていないだろうので、
アレックス・

ああ、すごい、ありがとう。あなたが毎日を学ぶ- stackoverflow.com/a/1255362/2047418
フィリップBartuzi


0

q / kdb +、53バイト

溶液:

{(|)(+)(,/)(max(#:)each c)$(+)2#(,)c:((&)differ x)_x}

例:

 q){(|)(+)(,/)(max(#:)each c)$(+)2#(,)c:((&)differ x)_x}"BBPPPPxxGGGGKKKKKKKkkkkEEeeEEEeeEEEEEOOO8####xxXXX"
 "        KK                      "
 "        KK                      "
 "        KK          EE          "
 "  PP  GGKKkk        EE    ##    "
 "  PP  GGKKkk    EE  EEOO  ##  XX"
 "BBPPxxGGKKkkEEeeEEeeEEOO  ##xxXX"
 "BBPPxxGGKKkkEEeeEEeeEEOO88##xxXX"

説明:

{reverse flip raze (max count each c)$flip 2#enlist c:(where differ x)_x} / ungolfed function
{                                                                       } / lambda function
                                                      (where differ x)    / indices where x differs
                                                                      _   / cut at these points aabbbc -> "aa","bbb","c"
                                                    c:                    / save in variable c
                                             enlist                       / put this list in another list
                                           2#                             / take two from this list (duplicate)
                                      flip                                / rotate columns/rows
                   (max count each c)                                     / find the longest run of characters
                                     $                                    / whitespace pad lists to this length
              raze                                                        / reduce down lists
         flip                                                             / rotate columns/rows
 reverse                                                                  / invert so buildings are on the ground

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.