23
\$\begingroup\$

Help! My Completely Automated Lights From Darks Separator V3001.01 broke! :(

Guidelines


Task

Write a program that will take an input of an array (or a list in some languages) of any amount of strings that are either the letter L or the letter D (representing lights or darks) and output an array that contains two arrays, one with all the L's, and one with all the D's.


Rules

  • It's code golf so the shortest answer in bytes wins
  • Input will only ever contain capitals
  • There must be the same number of L's in the output as there is in the input, same goes for the D's
  • The input may only have one element (or maybe even zero elements)
  • If one or both of the output arrays contain no elements, output an empty list (in some languages this may mean you need to output a string)
  • Always have the first array be the array of L's

Example output:

["L","D","L","D","D"] -> [["L","L"],["D","D","D"]]

["L","L","L"] -> [["L","L","L"],[]]

["D","D"] -> [[],["D","D"]]

[] -> [[],[]]

\$\endgroup\$
12
  • 4
    \$\begingroup\$ Can we input/output as a string? For example: "LDLDD" -> "LL DDD" or something like that? \$\endgroup\$
    – sporkl
    Jul 11, 2017 at 4:54
  • 4
    \$\begingroup\$ Or output perhaps as a list of the two strings, e.g. `"LDLDD" -> ["LL", "DDD"] \$\endgroup\$
    – sporkl
    Jul 11, 2017 at 7:27
  • \$\begingroup\$ Would also like an answer, as my answer depends on this \$\endgroup\$
    – Mayube
    Jul 11, 2017 at 8:46
  • \$\begingroup\$ How about mixed output? As in an array containing one string and one array of strings? e.g., ["L","L",["D","D","D"]]. \$\endgroup\$
    – Shaggy
    Jul 11, 2017 at 8:59
  • \$\begingroup\$ No, the output must be an array/list. @ComradeSparklePony \$\endgroup\$
    – aimorris
    Jul 11, 2017 at 9:04

35 Answers 35

1
2
0
\$\begingroup\$

Perl 5, 70 bytes

$"='","';@L=(L)x y/L//;@D=(D)x y/D//;$_="[[\"@L\"],[\"@D\"]]";s/""//g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 13 bytes

<@-."1 0&'DL'

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript, 38 bytes

q=>['L','D'].map(x=>q.filter(y=>x==y))
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Hello and welcome to the site! We do not allow answers that assume a variable as input, we call these snippets. Instead format your answer as a function (named on unnamed), or a full program with IO. \$\endgroup\$
    – Wheat Wizard
    Oct 2, 2017 at 6:47
  • \$\begingroup\$ Thanks @FunkyComputerMan. I edited it, let me know if it is correct. \$\endgroup\$ Oct 2, 2017 at 11:10
0
\$\begingroup\$

Retina, 21 11 bytes

-10 bytes thanks to CalculatorFeline

O^`.
LD
L D

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The 21 byte version can be O^`. LD L D. \$\endgroup\$ Jul 12, 2017 at 3:02
0
\$\begingroup\$

Vyxal, 53 bitsv2, 6.625 bytes

‛LDƛ?Oẋf

Try it Online!

Ports Thonnu 2

Explained

‛LDƛ?Oẋf­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
‛LDƛ      # ‎⁡To each character in the string "LD":
    ?O    # ‎⁢  Count the character in the input
      ẋ   # ‎⁣  And list repeat it that many times
       f  # ‎⁤  flatten that
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.