Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

shaved off 3 bytes.
Source Link
mkst
  • 5.3k
  • 13
  • 19

q/kdb+q/kdb+, 48(削除) 48 (削除ここまで) 45 bytes

-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev (<=':)x)_x}:;

Try it online!

Note: Link is to a K (oK) port of this solution as there is no TIO for q/kdb+.

Examples:

q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev (<=':)x)_x}:"STACKEXCHANGE";
 ST
A C K
 E X
 C H
A N
 G
 E
q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev (<=':)x)_x}:"BALLOON";
 B
A L
 L O
 O
 N

SolutionQ is interpreted right-to-left. The solution is split into two parts. First split the string where the next character is less than or equal to the current:

" ""__________________________" -> __________________ST______

-1{@[26#" ";.Q.A?x;:;x]}each{(0,where x<=prev (<=':)x)_x} cut x:; / ungolfed solution
-1 ; / print to stdout, swallow return value
 { } x: / firststore lambdainput functionas variable x
 cut _x / cut (_)slices x at these indices
 ( ) ) / do this together
 (<=':)x x<=prev x / is current char less-or-equal (<=) than each previous (':)?
 where / indices where this is true
 0, / prepended with 0
 each / take each item and apply function to it
 { } / second/ lambda function with x as implicit input
 @[ ; ; ; ] / apply[variable;indices;function;arguments]
 26#" " / 26 take " " is " "...
 .Q.A?x / lookup x in the uppercase alphabet, returns indicesindice(s)
 : / assignment
 x / the input to apply to these indices

Notes:

  • -3 bytes by replacing prev with the K4 version

q/kdb+, 48 bytes

-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x};

Examples:

q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x}"STACKEXCHANGE";
 ST
A C K
 E X
 C H
A N
 G
 E
q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x}"BALLOON";
 B
A L
 L O
 O
 N

Solution is split into two parts. First split the string where the next character is less than or equal to the current:

" " -> __________________ST______

-1{@[26#" ";.Q.A?x;:;x]}each{(0,where x<=prev x)_x}; / ungolfed solution
-1 ; / print to stdout, swallow return value
 { } / first lambda function
 _x / cut (_) x at these indices
 ( ) / do this together
 x<=prev x / is current char less-or-equal than previous?
 where / indices where this is true
 0, / prepended with 0
 each / take each item and apply function to it
 { } / second lambda function
 @[ ; ; ; ] / apply[variable;indices;function;arguments]
 26#" " / 26 take " " is " "...
 .Q.A?x / lookup x in the alphabet, returns indices
 : / assignment
 x / the input to apply to these indices

q/kdb+, (削除) 48 (削除ここまで) 45 bytes

-1{@[26#" ";.Q.A?x;:;x]}@/:(0,(&)(<=':)x)_x:;

Try it online!

Note: Link is to a K (oK) port of this solution as there is no TIO for q/kdb+.

Examples:

q)-1{@[26#" ";.Q.A?x;:;x]}@/:(0,(&)(<=':)x)_x:"STACKEXCHANGE";
 ST
A C K
 E X
 C H
A N
 G
 E
q)-1{@[26#" ";.Q.A?x;:;x]}@/:(0,(&)(<=':)x)_x:"BALLOON";
 B
A L
 L O
 O
 N

Q is interpreted right-to-left. The solution is split into two parts. First split the string where the next character is less than or equal to the current:

"__________________________" -> __________________ST______

-1{@[26#" ";.Q.A?x;:;x]}each(0,where (<=':)x) cut x:; / ungolfed solution
-1 ; / print to stdout, swallow return value
  x: / store input as variable x
 cut / cut slices x at these indices
 ( ) / do this together
 (<=':)x / is current char less-or-equal (<=) than each previous (':)?
 where / indices where this is true
 0, / prepended with 0
 each / take each item and apply function to it
 { } / lambda function with x as implicit input
 @[ ; ; ; ] / apply[variable;indices;function;arguments]
 26#" " / 26 take " " is " "...
 .Q.A?x / lookup x in the uppercase alphabet, returns indice(s)
 : / assignment
 x / the input to apply to these indices

Notes:

  • -3 bytes by replacing prev with the K4 version
Source Link
mkst
  • 5.3k
  • 13
  • 19

q/kdb+, 48 bytes

Solution:

-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x};

Examples:

q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x}"STACKEXCHANGE";
 ST
A C K
 E X
 C H
A N
 G
 E
q)-1{@[26#" ";.Q.A?x;:;x]}@/:{(0,(&)x<=prev x)_x}"BALLOON";
 B
A L
 L O
 O
 N

Explanation:

Solution is split into two parts. First split the string where the next character is less than or equal to the current:

"STACKEXCHANGE" -> "ST","ACK","EX","CH","AN","G","E"

Then take a string of 26 blanks, and apply the input to it at the indices where the input appears in the alphabet, and print to stdout.

" " -> __________________ST______

Breakdown:

-1{@[26#" ";.Q.A?x;:;x]}each{(0,where x<=prev x)_x}; / ungolfed solution
-1 ; / print to stdout, swallow return value
 { } / first lambda function
 _x / cut (_) x at these indices
 ( ) / do this together
 x<=prev x / is current char less-or-equal than previous?
 where / indices where this is true
 0, / prepended with 0
 each / take each item and apply function to it
 { } / second lambda function
 @[ ; ; ; ] / apply[variable;indices;function;arguments]
 26#" " / 26 take " " is " "...
 .Q.A?x / lookup x in the alphabet, returns indices
 : / assignment
 x / the input to apply to these indices

AltStyle によって変換されたページ (->オリジナル) /