Dado um número inteiro de entrada n > 1
, produza um octógono de arte ASCII com comprimentos laterais compostos por n
caracteres. Veja exemplos abaixo:
n=2
##
# #
# #
##
n=3
###
# #
# #
# #
# #
# #
###
n=4
####
# #
# #
# #
# #
# #
# #
# #
# #
####
n=5
#####
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#####
and so on.
Você pode imprimi-lo em STDOUT ou retorná-lo como resultado de uma função.
Qualquer quantidade de espaço em branco estranho é aceitável, desde que os caracteres sejam alinhados adequadamente.
Regras e E / S
- A entrada e a saída podem ser fornecidas por qualquer método conveniente .
- Você pode usar qualquer caractere ASCII imprimível em vez de
#
(exceto espaço), mas o caractere "plano de fundo" deve ser espaço (ASCII 32). - Um programa completo ou uma função são aceitáveis.
- Lacunas padrão são proibidas.
- Isso é código-golfe, portanto todas as regras usuais de golfe se aplicam e o código mais curto (em bytes) vence.
code-golf
ascii-art
code-golf
geometry
code-golf
balanced-string
code-golf
cops-and-robbers
code-challenge
cops-and-robbers
code-golf
code-golf
random
cryptography
code-golf
array-manipulation
number
code-challenge
integer
code-golf
math
integer
code-golf
math
math
parsing
image-processing
test-battery
math
number
combinatorics
fastest-code
code-golf
code-golf
math
number-theory
rational-numbers
polynomials
code-golf
math
geometry
code-golf
code-golf
number-theory
primes
factoring
code-golf
restricted-source
code-golf
string
decision-problem
counting
code-golf
math
sequence
fibonacci
code-golf
array-manipulation
counting
code-golf
array-manipulation
number-theory
code-golf
array-manipulation
code-golf
random
code-golf
string
hexadecimal
code-golf
string
code-challenge
sorting
code-golf
number
floating-point
code-golf
sorting
code-golf
decision-problem
fibonacci
code-golf
number
combinatorics
code-golf
string
code-golf
math
code-golf
electrical-engineering
code-golf
javascript
code-golf
base-conversion
code-golf
array-manipulation
matrix
binary-matrix
code-golf
kolmogorov-complexity
python
perl
ruby
code-golf
number
code-golf
optimization
integer-partitions
code-golf
string
code-golf
ascii-art
AdmBorkBork
fonte
fonte
Respostas:
05AB1E , 3 bytes
Experimente online!
Explicação
Veja esta resposta para entender a tela 05AB1E.
fonte
JavaScript (ES6),
114106105104103 bytesExperimente online!
Quão?
Isso cria o caractere de saída por caractere.
Dada a entradan , calculamos:
Para cada caractere em( x , y) , calculamos ( h , v ) :
As células pertencentes ao octógono atendem a uma das seguintes condições:
Por exemplo, comn = 4 (e n′=3 ):
fonte
Charcoal, 5 bytes
My first answer with Charcoal!
Explanation:
Try it online!
fonte
PolygonHollow(:*, InputNumber(), "#");
.Canvas,
151412 bytesTry it here!
Explanation:
Alternative 12-byter.
fonte
R,
122117115 bytesTry it online!
Ports the logic from Arnauld's answer, specifically this revision in case there are further improvements. Another 2 bytes saved thanks to Arnauld's suggestion of inverting the logic!
fonte
h*v&h+v-n
in JS because&
is a bitwise operator; but it's a logical one in R, so that works).Python 2, 96 bytes
Try it online!
fonte
Python 2, 81 bytes
Try it online!
Python 2, 75 bytes
Try it online!
If mixing output characters is OK.
fonte
Powershell, 91 bytes
fonte
PowerShell,
10797 bytesTry it online!
If there was a cheap way to reverse the first half, this answer would feel a lot better. It builds the left half, then the core (which is either x
#'s
or spaces), then mirrors the left's logic to make the right. Fun fact, you don't need to copy over trailing white-space.Unrolled and explained:
fonte
C (clang),
-DP=printf(
-DF=for(i
+ 179 = 199180 bytesTry it online!
Ungolfed:
-19 bytes thanks to @ceilingcat
fonte
Python 2, 130 bytes
Try it online!
On mobile, so not incredibly golfed.
fonte
(n+2*i)
.Batch, 260 bytes
Outputs two leading spaces on each line. Explanation: Batch has no string repetition operator, limited string slicing capability and requires separate statements to perform arithmetic. It was therefore golfiest to make up a string of the input length in spaces (Batch can at least translate these to
#
s for the top and bottom lines) and then slice from or to a specific position ranging from 3 to the length to generate the diagonals (this is what the last line of the script achieves).fonte
Ruby, 96 bytes
Try it online!
Not very golfed yet. Might golf if I find the time.
fonte
Red, 171 bytes
Try it online!
Explanation:
fonte
APL (Dyalog Unicode), 46 bytesSBCS
This solution was provided by Adám - thanks!
Try it online!
My (almost) original solution:
APL (Dyalog Unicode), 61 bytesSBCS
Try it online!
Thanks to Adám for his help!
The idea is to find the "diamond" that lies partly in the square and apply an edge-detect filter to "outline" the octagone.
fonte
(' '@~5 6∊⍨1⊥⊢∘,)⌺3 3⊢<(⍉⌽⌊⊢)⍣2∘(∘.+⍨∘⍳¯2+3×⊢)
⌺
. Rather count 1 byte/char by referring to SBCS as per Meta.Perl 5,
201197188187186 bytes:Try it online!
Reads the size of the octagon from first line of
STDIN
.fonte
$"
instead of" "
.Perl 5, 176 bytes
Based on Nathan Mills' answer above (which I have insufficient rep to comment on!).
$e
can be simplified to$a-1
saving 6 bytes;$f
can be chain assigned; saving two bytes; Not sure where the other two come from!While
$e
can be replaced with$a-1
in the two places it occurs, the extra brackets needed means this only breaks even.Ungolfed:
fonte
Perl 6,
7673 bytes-3 bytes thanks to Jo King
Try it online!
Returns a list of lines.
fonte
C (gcc),
158153150 bytesfiveeight bytes thanks to ceilingcat.Try it online!
fonte
Python 3, 224 bytes
Try it online!
fonte
Perl 5,
170168166 bytesThis works by the magic of regex. The "if" is only needed to deal with the pathological case of n=2, which otherwise outputs something like:
probably this can be coded away.
I think there may be a lot more to gain by creating a string up to the mid-point then reversing it. Of course we then need to insert/delete an extra space if n is odd (or use thin-space :p).
Ungolfed
I think this can probably be golfed a bit more, quite apart from significant changes like pushing onto
$@
and printing that at the end.[Golfed spaces around
..
and moved print before assigns in two cases, saving on semicolons.]fonte
\s
and not just a space in last regexJ,
594541 bytesTry it online!
Will add explanation tonight.
fonte
Perl 5, 98 bytes
TIO
fonte