Los codificadores siempre están tratando de aplanar matrices en entidades aburridas unidimensionales y eso me entristece.
Su tarea es desplegar una cadena arbitraria de caracteres, generando un hermoso paisaje urbano.
Considere la cadena: aaabbbbbccqrrssstttttttPPw
Se ve mucho mejor así:
tt
tt
bb tt
bb tt
aabb sstt
aabbcc rrssttPP
aabbccqqrrssttPPww
(Ok, sí, las letras están duplicadas para que se vea más ciudad-horizonte-ery).
¡Toma una cadena de entrada, duplica cada subsección de caracteres coincidentes (no necesariamente letras alfabéticas) y construye una ciudad para mí!
Los bytes de código más cortos ganan.
De hecho, pensé que tenía los requisitos establecidos, pero para responder algunas preguntas:
- debe estar en el suelo
- puede tener cielo adicional si lo desea (líneas en blanco iniciales, espacio en blanco circundante), pero no entre los edificios
- las letras se pueden reutilizar dentro de la cadena (misma arquitectura, ubicación diferente)
- se supone que las letras son ASCII, pero se dará más estilo a quienes admitan codificaciones adicionales (UTF8, etc.)
aaabbbbaa
?Respuestas:
05AB1E , 6 bytes
Pruébalo en línea!
En una versión más nueva que el desafío,
ζ
se ha agregado como reemplazo para.Bø
05AB1E , 8 bytes
Explicación:
Pruébalo en línea!
fuente
z⁶
para.Bø
... pero también tieneŒgx'2
paraγ€D
> _>γ.BD)ø˜øR»
era lo que tenía sin mirar,€D
es mucho mejor; Sin embargo, creo que a los dos nos falta la solución de 1 byte para la duplicación en línea.CJam , 23 bytes
Pruébalo en línea!
Explicación:
fuente
Jalea , 9 bytes
Pruébalo en línea!
Explicación:
fuente
'
, which was to repeat the lists themselves and not the items inside them, but overall it's good. :)Python 3,
155136134132 bytes-19 bytes thanks to @LeakyNun
-2 bytes thanks to @officialaimm
-1 byte thanks to @Wondercricket
Try it online!
fuente
Python 2, 117 bytes
Try it online!
fuente
Java 8,
412400330324312319 bytes-6 bytes thanks to VisualMelon
-12 bytes thanks to Kevin Cruijssen
but +19 bytes because I forgot to include the imports in the byte count.
Try it online!
fuente
i=0
, or better,i=l
, and count downfor(;i-->0;h=d>h?d:h)
(and stuff theh=
bit in there). The same back-counting will work for the inner loop also. The innerif
also has no need for the braces{}
. And always be weary of<=
or>=
, you can turn the ternary around with>
and save a byte.import java.util.*;
forMap
andHashMap
, imports are part of the byte-count; and -1 by removing trailing semi-colon, which isn't part of the byte-count).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);}}}
HashMap<>
→HashMap
;Map n=
→,n
andn=
;m.put(c,d=m.get(c)!=null?d+1:1);
inside the for-loop to get rid of the brackets;k=x.charAt(i)
inside theif(n.get(k)!=null)
to get rid of the semi-colon and for-loop's brackets. Again, welcome and great answer! +1 from me. Also, in case you haven't seen it yet: Tips for golfing in Java and Tips for golfing in <any language> might be interesting to read through.Japt,
1918151312 bytesIncludes trailing spaces on each line.
Test it
Explanation
fuente
Mathematica, 150 bytes
fuente
R, 135 bytes
Try it online!
reads from stdin, writes to stdout (with a trailing newline).
Explanation:
rle
finds the lengths of the streaks of characters, the heights of each tower.sub
expression replaces each character with its double (so I didn't have to muck about with setting adjacent indices together)sapply
returns an array (in this case a matrix):sum(e$l|1)
is the number of distinct characters; we go from top to bottomifelse( ... )
is a vectorizedif...else
allowing us to build a matrix of towers and double spaceswrite
writes to console, with a few options to format.fuente
Pyth, 13 bytes
Try it online!
fuente
PHP, 138 bytes
Try it online!
fuente
MATL, 15 bytes
Try it online!
Explanation
fuente
Charcoal, 40 bytes:
Try it online! Link is to verbose version of code. I originally tried a simple loop over the input string to print an oblong every time the letter changed, but I switched to this list-building method as it saved 5 bytes. Explanation: The variable
l
contains a nested list of the input letters. Characters that match the current last list elements get pushed onto the last list otherwise a new sublist is created for that character. It then remains to join the letters in each sublist so that they can be printed vertically twice.fuente
C,
259231 BytesGolfed Code
Verbose Code
Compiled with GCC, no special Flags
Edit
Saved 28 bytes thanks to adelphus. His change allowed me to create a define. And I made the while loops into for loops to save 2 bytes each by rearranging the loop. I also fixed an issue where the code would break when the last character in input wasn't singleton. The code will fail if there is only one unique letter but should work in all other cases.
fuente
while (i < strlen(argv[1]))
can be shortened towhile (argv[1][i])
- loop until null characterPip, 22 bytes
21 bytes of code, +1 for
-l
flag.Try it online!
Explanation
fuente
QuadS, 15 + 1 = 16 bytes
+1 byte for the
1
flag.Try it online!
⊖⍵
post-process by flipping upside down(.)\1*
runs of identical characters2/⍪⍵M
duplicate the columnified MatchThe
1
flag causes the results to be merged together.fuente
Haskell, 144 bytes
I'm pretty confident I can do better than this, but this is the best I can come up with for the time being.
fuente
Data.List
which is not in scope by default. You have to either add theimport Data.List
to the byte count or specify a Haskell environment which does include it by default (e.g. change the language fromHaskell
toHaskell (lambdabot)
. -- Some tips: a) use pattern guards to bind variables instead oflet
and/or declare helper functions directly:l=length;f s|x<-groupBy(==)s,m<-... =concatMap
. b)map l x
isl<$>x
, c)concatMap("++\n"
isunlines
. d)groupBy(==)
is justgroup
. e)concat
isid=<<
. You usem
only once, so inline it()
aroundl y
,replicate ... ' '
andmap ... x
. All in all: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]
.groupBy(==)
=group
, altough I'm not sure whether one is in Prelude and the other isn't.concatMap
can be written>>=
, andmap
can be infixed as<$>
, andconcat[[z,z]|z<-…]
might be(replicate 2)=<<…
or(\z->[z,z])=<<…
(\z->[z,z])
is(:)<*>pure
, i.e....transpose$(:)<*>pure=<<map(\y...)x
Stacked, 42 bytes
Try it online!
fuente
Ruby, 116 bytes
Try it online!
fuente
puts a.map{...}
can be replaced withp(a.map{})
p
will output quote characters, so it's not fit hereAPL (Dyalog), 37 bytes
Try it online!
fuente
q/kdb+, 53 bytes
Solution:
Example:
Explanation:
fuente
Perl 5, 92 + 1 (-p) = 93 bytes
Try it online!
fuente