Create your own Red and Black
V, 31, 30 bytes
êuòIRed -
IBlack -
òñFRdH
Try it online!
Hexdump:
00000000: 16ea 75f2 4952 6564 202d 201b 0a49 426c ..u.IRed - ..IBl
00000010: 6163 6b20 2d20 1b0a f2f1 4652 6448 ack - ....FRdH
This is trivial in V, but the edge case of odd inputs makes it tricky because V doesn't really have conditionals. Thankfully, we can handle this at the relatively small cost of +6
bytes.
Haskell, 104 120 113 112 111 110 bytes
import Data.Char
f x|odd$length$x=mempty|1<2=Just$zipWith(\(h:t)x->x++toLower h:t)x$cycle["Red - ","Black - "]
Try it online!
Ungolfed with explanation
import Data.Char
f :: [String] -> Maybe [String]
f x
| even $ length $ x = Just $ zipWith (\(h : t) x -> x ++ toLower h : t) x $ cycle ["Red - ","Black - "]
| otherwise = Nothing
f
is a function that takes a list of strings (a.k.a. a list of lists of Char
s), and returns Maybe
the same. Haskell's functions are quite "pure", and so we need to make it clear that this function may not return anything. (A function of type Maybe a
returns either Nothing
or Just a
).
The |
operator is a guard - a kind of conditional. The first branch is followed if even $ length $ x
(which is another way of writing even (length x)
) is True
. Otherwise, the second one (1<2
in the golfed example, which is of course always true) is followed and we return Nothing
.
zipWith
takes a two-argument function and applies it to each element of two lists. The function we're using here is \(h : t) x -> x ++ toLower h : t
. h : t
implicitly splits the first character off our first argument, which is the kind of nice thing you can do in Haskell. The first list is the input (which we already know contains an even number of lines), and the second is just infinitely alternating "Red - " and "Black - " (infinite lists are another nice thing that's possible, this time because Haskell is lazy - it only cares about as much of something as you use).
05AB1E, 26 bytes
Code:
|©v”†¾ƒÏ”#Nè… - yćlìJ®gÈ×,
Uses the 05AB1E encoding. Try it online!
Explanation:
| # Take the input as an array of inputs
© # Keep a copy of this in the register
v # Map over the array
”†¾ƒÏ”# # Push the array ["Red", "Black"]
Nè # Get the Nth element (where N is the iteration index)
… - # Push the string " - "
yć # Push the current element and extract the first element
lì # Convert to lowercase and prepend back to it's
original place
J # Join the entire stack into a single string
®g # Get the length of the input
È # Check if it is even (results in 1 or 0)
× # String multiply the result by the string
, # Pop and print with a newline