Code Golf Asked on October 27, 2021
This is the logo for LAPACK, a widely used software library for numerical linear algebra.
Your task is to print the logo (color and matrix brackets not required) as this exact string. Trailing newline is allowed.
L A P A C K
L -A P -A C -K
L A P A -C -K
L -A P -A -C K
L A -P -A C K
L -A -P A C -K
++++++++[>+++++>++++++++>++++>+++++++++>+<<<<<-]>+++++>+>>++++>++<.<..<.>..>++++.<..<.>..<++.>..>-----.>.<+.<.<<.>--.>..>++++.<.<<.>.>..<++.>.<<.>>>-----.>.<+.<..<--.>..>++++.<..<.>.<<.>++.>.<<.>>>-----.>.<+.<.<<.>--.>..>++++.<.<<.>.>.<<.>++.>..>-----.>.<+.<..<--.>.<<.>>>++++.<.<<.>.>..<++.>..>-----.>.<+.<.<<.>--.>.<<.>>>++++.<..<.>..<++.>.<<.>>>-----.>.
There is the version, line by line (The 1st one is used to store the different chars)
++++++++[>+++++>++++++++>++++>+++++++++>+<<<<<-]>+++++>+>>++++>++
<.<..<.>..>++++.<..<.>..<++.>..>-----.>.
<+.<.<<.>--.>..>++++.<.<<.>.>..<++.>.<<.>>>-----.>.
<+.<..<--.>..>++++.<..<.>.<<.>++.>.<<.>>>-----.>.
<+.<.<<.>--.>..>++++.<.<<.>.>.<<.>++.>..>-----.>.
<+.<..<--.>.<<.>>>++++.<.<<.>.>..<++.>..>-----.>.
<+.<.<<.>--.>.<<.>>>++++.<..<.>..<++.>.<<.>>>-----.>.
Answered by The random guy on October 27, 2021
Port of the Arnauld's alternative answer. Very nice! Delicious! Thanks.
We avoid the problem with 2 trailing spaces on the last row
because we use the array of strings, not the string with repeated LAPACKn
.
,'LAPACK'*6-replace'B',{' '+' -'[(214811968-shr++$i)%2]}
port of C (gcc) answer. Thanks @Noodle9 and @Arnauld.
-join(29..0|%{'CAPAL'[$_%5];' ';' -'[(22141337-shr$_)%2];'K
'*!($_%5)})
Answered by mazzy on October 27, 2021
-17 bytes thanks to mazzy
-join("L A P A C K
"*6|% t*y|%{"$_-"[++$i+12-in' &,:=BHKYVdgp'[0..12]]})
Just checking for the indexes we need saves 9 bytes over just writing out the block. However, if we represent the indexes using their char values, we save loads more. We add 12 to the current iteration to get everything into the printable ASCII range. ' &,:=BHKYVdgp'[0..12]
converts our index string into an index array so we can use -in
.
Answered by Veskah on October 27, 2021
s,a,i="0"*19+bin(0x208200090824009002402)[2:],list("L A P A C Kn"*6),0
for c in s:
if(int(c)):a[i]="-"
i+=1
print "".join(a)
Yes, I realise this is longer than a simple print statement, but I spent too long trying to get this to work and I liked the approach (for larger matrices, this method would become much more efficient). There must be at least a few ways to shave a couple bytes off this answer
Answered by Daniel H. on October 27, 2021
' -'[(6⍴2)⊤⎕A⍳'AVDWMZ'],¨6 6⍴'LAPACK'
⎕IO←0
.We encode the dashes matrix by
1
s (and spaces as 0
s)0 21 3 22 12 25
)AVDWMZ
).We decode it the same way:
⎕A⍳
- retrieves index into alphabet(6⍴2)⊤
converts to binary columns' -'[...]
- 1
becomes dash.Then we generate the LAPACK
matrix with 6 6⍴'LAPACK'
, and concatenate each pair with ,¨
.
Answered by Uriel on October 27, 2021
" - - - -- - -- -- -- - "foreach(x,i,("LAPACK"exSlice(x%6,x%6+1).." ".. i asCharacter .. if(x%6>4,"
",""))print)
Answered by user92069 on October 27, 2021
s='LAPACK';e=enumerate
for i,_ in e(s):print(*[' -'[(j%2+i%2==2)^(i//2+j//2==3)]*(j!=0)+k for j,k in e(s)])
How it works: blue squares are in the form
$begin{pmatrix}1&1\1&-1end{pmatrix}$ hence j%2+i%2==2
and red squares (when i//2+j//2==3
) are in the opposite form
$begin{pmatrix}-1&-1\-1&1end{pmatrix}$ thus we simply xor the expressions with ^
.
$$
begin{array}{rr|rr|rr}
color{blue}{mathrm{L}}&
color{blue}{mathrm{A}}&
color{blue}{mathrm{P}}&
color{blue}{mathrm{A}}&
color{blue}{mathrm{C}}&
color{blue}{mathrm{K}}\
color{blue}{mathrm{L}}&
color{blue}{mathrm{-A}}&
color{blue}{mathrm{P}}&
color{blue}{mathrm{-A}}&
color{blue}{mathrm{C}}&
color{blue}{mathrm{-K}}\
hline
color{blue}{mathrm{L}}&
color{blue}{mathrm{A}}&
color{blue}{mathrm{P}}&
color{blue}{mathrm{A}}&
color{red}{mathrm{-C}}&
color{red}{mathrm{-K}}\
color{blue}{mathrm{L}}&
color{blue}{mathrm{-A}}&
color{blue}{mathrm{P}}&
color{blue}{mathrm{-A}}&
color{red}{mathrm{-C}}&
color{red}{mathrm{K}}\
hline
color{blue}{mathrm{L}}&
color{blue}{mathrm{A}}&
color{red}{mathrm{-P}}&
color{red}{mathrm{-A}}&
color{blue}{mathrm{C}}&
color{blue}{mathrm{K}}\
color{blue}{mathrm{L}}&
color{blue}{mathrm{-A}}&
color{red}{mathrm{-P}}&
color{red}{mathrm{A}}&
color{blue}{mathrm{C}}&
color{blue}{mathrm{-K}}
end{array}
$$
Other techniques used:
print(*[x])
instead of print(' '.join(x))
,
s*(j==0)
instead of s if j else ''
,
[falsy,truthy][expr]
instead of truthy if expr else falsy
, where the former list is just a string ' -'
,
the rest is pretty straightforward.
Answered by Alexey Burdin on October 27, 2021
$"=" { ,-}";say+(<"@{[L,A,P,A,C,K]}n">)[0,21,3,22,12,25]
First $"
(which is a magic variable that is used as a field separator when lists are interpolated into strings - default is " "
) is set to { ,-}
. Then say
is called which is a newline-terminated print
function, passing in the listed indexes (0, 21, 3, 22, 12, 25) from the result of the glob
(<...>
is shorthand for calling glob
) <"@{[L,A,P,A,C,K]}n">
. This glob expands to:
L { ,-}A { ,-}P { ,-}A { ,-}C { ,-}K
Which, due to the { ,-}
s, will generate a list containing all permutations of the string with either
or -
before every letter (except the leading L
). The chosen indices are the ones we need for the logo.
Answered by Dom Hastings on October 27, 2021
-R
, 30 bytesHappy to be beating 05AB1E after quite a bit of work on this but still feel I could do much better. Originally based on Neil's Charcoal solution.
"?*<)3&"¬®csSi-)í"LAPACK")ò ¸x
Answered by Shaggy on October 27, 2021
library(magrittr)
A <- matrix(c(1,1,1,-1),nrow = 2)
B <- matrix(c(1,1,1,1,1,-1,1,-1,1),nrow = 3)
kronecker(B,A) %>%
apply(1, function(x) {paste0(x,strsplit("LAPACK","")[[1]]) %>%
gsub("-1","-",.) %>% gsub("1"," ",.)}) %>%
apply(2,function(x){paste0(x, collapse = " ")}) %>%
cat(sep = "n")
This gives on my console:
L A P A C K
L -A P -A C -K
L A P A -C -K
L -A P -A -C K
L A -P -A C K
L -A -P A C -K
With the function kronecker() we construct a block matrix by replicating A by B coefficients.
A is a 2x2:
> A
[,1] [,2]
[1,] 1 1
[2,] 1 -1
which are the signs that we want to replicate in 3x3 blocks multiplied by the coefficients in B:
> B
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 -1
[3,] 1 -1 1
Then we explode LAPACK with strsplit() and prepend 1 or -1. With gsub() we substitute 1 and -1. Then, we collapse the strings and print out to the console.
Answered by Domenico Spidy Tamburro on October 27, 2021
/$/ -//#/A@//!/
L //@/ /L@#P@#C@K!-#P$#C$K! #P@A$C$K!-#P$A$C@K! A$P$#C@K!-A$P@#C$K
Answered by nph on October 27, 2021
•x—o—Õ•₃вε„ -Åв’la•Î’u.ι2ôJðý¦,
Explanation:
•x—o—Õ• # Push compressed integer 251957282837
₃в # Convert it to base-95 as list: [32,53,35,54,44,57]
ε # Foreach over the integers:
„ -Åв # Convert it to custom base-" -",
# which basically means to convert it to base-2 and index it into " -"
’la•Î’ # Push dictionary string "lapack"
u # Uppercase it: "LAPACK"
.ι # Interleave the characters in the two strings
2ô # Split it into pairs of characters
J # Join each pair together
ðý # Join the list by spaces
¦ # Remove the first character in front of the "L"
, # And output it with trailing newline
See this 05AB1E tip of mine (sections How to use the dictionary?, How to compress large integers?, and How to compress integer lists?) to understand why ’la•Î’
is "lapack"
; •x—o—Õ•
is 251957282837
; and •x—o—Õ•₃в
is [32,53,35,54,44,57]
.
([32,53,35,54,44,57]
is [100000,110101,100011,110110,101100,111001]
in binary.)
Answered by Kevin Cruijssen on October 27, 2021
puts"L%sA%sP%sA%sC%sK
"*6%(0..29).map{|i|' -'[644276896[i],2]}
Builds the output string by successive substitution from an array of prefixes. For each letter other than L
, the appropriate two-character prefix (either
or -
) is selected by using the binary digits of 644276896
(100110011001101110001010100000
in binary) to index into the three-character string -
.
Answered by Dingus on October 27, 2021
This is way too long.
The markdown parser breaks terribly if I use the TIO's post snippet generator, so there's an extra leading newline in the snippet below: there is actually only 1 leading newline!
bbcccbc-cc-b--b-- c
b
c
-
L`.{6}
.
$0X
Y`X`LAPACK
.L
L
The first 2 lines replace the empty string with bbccc... c
, the next 4 lines decode it into spaces and dashes by simple substitutions (b
-> 3 spaces, c
-> -
), the next line splits it into 6 lines of length 6 (producing a 6x6 sign matrix), the next two lines replace each character c
by cX
, the next line cyclically transliterates all X
s into LAPACK
s, and the last 2 lines remove leading whitespace.
Try it online!
Answered by the default. on October 27, 2021
print"L %sA %sP %sA %sC %sKn"*6%tuple(' - - - --- -'+'- -'*3)
Kind-of a boring solution. Makes a string template for the output with slots to put in minuses, then inserts in hardcoded string of minuses and spaces for those slots. I didn't find a way to compress or generate this length-30 binary sequence shorter than hardcoding it. The only optimization the code uses is that the sequence ends in 3 copies of '- -'
. The output includes a trailing newline which the challenge allows.
The template could also use %2s
in place of %s
which would allow also putting in empty string for the spaces, but I don't see how to use this.
Answered by xnor on October 27, 2021
i=0
for c in'LAPACK'*6:print' -'[chr(i+33)in'(*,12467;<@AD']*(c!='L')+c+'n'*(c=='K'),;i+=1
I know most previous answers already beat this, but it's my first golf and I quite enjoyed the result :-)!
Edit: Thanks very much to @xnor for the tip with all the tips!'n'*(c=='K')
Answered by yyyyyyyan on October 27, 2021
Saved 7 bytes thanks to the man himself Arnauld!!!
Saved a byte thanks to ceilingcat!!!
f(i){for(i=30;i--;i%5||puts("K"))printf("%c %c","CAPAL"[i%5]," -"[22141337>>i&1]);}
Answered by Noodle9 on October 27, 2021
n←96⍴¯2↓∊'LAPACK',¨⊂' '⋄n[⎕av⍳'ì↑⍋+.28;EHRU^']←'-'⋄6 16⍴n
Explanation:
'LAPACK',¨⊂' ' concatenate 2 spaces to each letter in LAPACK
96⍴¯2↓∊ convert to a vector, drop last 2 spaces and replicate to form a 96 element vector
⎕av⍳'ì↑⍋+.28;EHRU^' convert characters to ascii code point integers
n[.....]←'-' use integers as index positions to assign - character
6 16⍴n reshape vector as a 6 16 matrix
Answered by Graham on October 27, 2021
E?*<)3&✂⭆⍘℅ι- ⁺ ⁺λ§LAPACKμ²
Try it online! Link is to verbose version of code. Edit: Saved 2 bytes by appropriating @KevinCruijssen's custom base conversion idea. Explanation:
?*<)3& Literal string of code points
E Map over characters
ι Current character
℅ Take the ordinal
⍘ - Convert to custom base `- `
⭆ Map over characters and join
λ Current character
⁺ Concatenated with
LAPACK Literal string `LAPACK`
§ Indexed by
μ Inner index
⁺ Prefixed with a space
✂ ² Slice off the leading spaces
Answered by Neil on October 27, 2021
Binary:
00000000: b106 be25 01ad cd29 84e4 740e b020 cd29 ...%...)..t.. .)
00000010: d2ec 7302 b02d cd29 ebeb b00d cd29 b00a ..s..-.).....)..
00000020: cd29 e2de c34c 1541 0350 1641 0c43 194b .)...L.A.P.A.C.K
00000030: 00 .
Build and test using xxd -r
on your favorite DOS VM.
Listing:
B1 06 MOV CL, 6 ; loop 6 rows
ROWLOOP:
BE 0121 MOV SI, OFFSET LS ; letter string into SI
COLLOOP:
AD LODSW ; letter into AL, dash pattern into AH
CD 29 INT 29H ; write to screen
84 E4 TEST AH, AH ; is AH = 0?
74 0E JZ END_NL ; if so break loop, write NL
B0 20 MOV AL, ' ' ; space char into AL
CD 29 INT 29H ; write to screen
D2 EC SHR AH, CL ; shift dash bit into CF
73 02 JNC NO_DASH ; is a dash?
B0 2D MOV AL, '-' ; dash char in AL
NO_DASH:
CD 29 INT 29H ; write to screen
EB EB JMP COLLOOP ; loop until end of string
END_NL:
B0 0D MOV AL, 0DH ; CR char
CD 29 INT 29H ; write to screen
B0 0A MOV AL, 0AH ; LF char
CD 29 INT 29H ; write to screen
E2 DE LOOP ROWLOOP ; loop until end of rows
C3 RET ; return to DOS
LS DB 'L',15H,'A',3H,'P',16H,'A',0CH,'C',19H,'K',0
How?
The "letter string" data contains two bytes for each letter - the high byte is the letter and the low byte is a bitmap describing if that letter should be followed by a dash for each row. The rows are indexed 6 to 1 starting from the top, where the bit in the corresponding order represents whether or not there's a dash.
Examples:
Row 5, Col 0: Data 'L'
, 0x15
(010101)
The fifth bit is a 1
indicating that for the fifth row after the L
there is a dash after.
Row 2, Col 3: Data 'A'
, 0xC
(001100)
The second bit is a 0
indicating that for the fifth row after the A
there is not a dash after.
Or looking at it a different way, the odd bytes [ 0x15, 0x3, 0x16, 0xC, 0x19, 0x0 ]
form the bitmap of the dashes (only rotated and flipped):
0x15 010101
0x3 000011
0x16 010110
0xC 001100
0x19 011001
0x0 000000
Runtime:
A standalone IBM PC DOS COM executable. Output to console.
Answered by 640KB on October 27, 2021
_=>`L A P A C K
`.repeat(i=6).replace(/ /g,c=>c+' -'[863064083>>++i&1])
We build a string consisting of the pattern "L A P A C Kn"
repeated 6 times and match all spaces. We replace each of them with either " "
or " -"
depending on the result of a test on a bit mask.
In binary, the constant 863064083 is:
bit 31 bit 7 bit 0
v v v
00110011011100010101000000010011
___/___/___/___/___/ ___/
row: 4 3 2 1 0 5
Because we start with i=6
and pre-increment i
at each iteration, the first row is encoded by the bits 7 to 11 (0-indexed).
As stated in the ECMAScript specification, bitwise shifts are processed modulo 32. So there's a wrap-around when i
exceeds 31 and the last row can safely be encoded by the bits 0 to 4.
For 69 bytes, we could do:
_=>`LAPACK
`.repeat(i=6).replace(/B/g,c=>' '+' -'[863064083>>++i&1])
But the corresponding output includes 2 trailing spaces on the last row1. Because the challenge looks very strict about leading and trailing whitespace, this is probably invalid. ¯_(ツ)_/¯
1: Now, would you have noticed them if I didn't tell ya?! :-p
Answered by Arnauld on October 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP