| Bytes | Lang | Time | Link |
|---|---|---|---|
| 029 | Japt R | 170526T192411Z | Shaggy |
| 118 | Javascript | 170604T234023Z | Vitim.us |
| nan | Python 2 | 170527T151440Z | Luatic |
| 093 | JavaScript ES6 | 170605T012949Z | darrylye |
| 110 | Haskell | 170527T095527Z | felixphe |
| 041 | CJam | 170604T163537Z | Esolangi |
| 099 | C | 170527T032024Z | MD XF |
| 097 | Röda | 170527T055128Z | user4180 |
| 030 | V | 170526T181732Z | DJMcMayh |
| 058 | Retina | 170526T205629Z | Calculat |
| 026 | 05AB1E | 170526T183302Z | Adnan |
| 029 | Jelly | 170526T194612Z | Jonathan |
| 030 | Jelly | 170526T192053Z | Erik the |
Japt -R, 36 35 34 29 bytes
Takes input as an array of lines, outputs 0 for false.
Êv ©¡`R Black`¸gY ¸p-XhXÎv¹¸
Êv ©¡`...`¸gY ¸p-XhXÎv¹¸ :Implicit input of array
Ê :Length
v :Parity
© :Logical AND with
¡ : Map each X at index Y in the array
`...` : Compressed string "Red Black"
¸ : Split on spaces
gY : Get the element at index Y
¸ : Split on spaces
p : Push
- : 1. "-"
Xh : 2. Replace the first character of X with
XÎ : First character of X
v : Lowercased
¹ : End push
¸ :Implicit output joined with newlines
Javascript, 118 bytes
v=s=>(s=s.split`\n`).length%2==0?s.map((l,i)=>(i%2==0?'Red':'Black')+' - '+l[0].toLowerCase()+l.substr`1`).join`\n`:!1
Test
//code
v=s=>(s=s.split`\n`).length%2==0?s.map((l,i)=>(i%2==0?'Red':'Black')+' - '+l[0].toLowerCase()+l.substr`1`).join`\n`:!1
//tests
t0 = "The blood of angry men!\nThe dark of ages past!\nA world about to dawn!\nThe night that ends at last!";
t1 = "test test\n1\n[][][]\nBBB";
t2 = "I feel my soul on fire!\nThe color of desire!\nThe color of despair!";
t3 = "Red - I feel my soul on fire!\nBlack - My world if she's not there!";
console.log( v(t0) );
console.log( v(t1) );
console.log( v(t2) );
console.log( v(t3) );
Python 2, 215->184->165 bytes
Saved 31 bytes according to Stephen S' comment
Challenger5 got it down to 165 bytes
l=[]
b=["Red","Black"]
d=" - "
while 1:
a=raw_input()
if a:l.append(a)
else:break
q=len(l)
if q%2<1:
for i in range(q):n=l[i];print b[i%2]+d+n[0].lower()+n[1:]
JavaScript (ES6), 93 bytes
Takes the song as an array of lines.
S=>S.length%2?0:S.map((L,i)=>(i%2?'Black':'Red')+' - '+L[0].toLowerCase()+L.slice(1)).join`
`
f=
S=>S.length%2?0:S.map((L,i)=>(i%2?'Black':'Red')+' - '+L[0].toLowerCase()+L.slice(1)).join`
`
console.log(f([
'The blood of angry men!',
'The dark of ages past!',
'A world about to dawn!',
'The night that ends at last!'
]))
console.log(f([
'The blood of angry men!',
'The dark of ages past!',
'A world about to dawn!'
]))
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 - "]
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 Chars), 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).
CJam, 41 bytes
qN/{(el\+}%2/"Red -
Black - "N/f.\:~2/N*
C, 112 107 105 103 99 bytes
-4 thanks to ASCII-only
-2 thanks to Mego
i;main(a,s)char**s;{for(;a%2&++i<a;)printf("%s - %c%s\n",i%2?"Red":"Black",tolower(*s[i]),s[i]+1);}
Takes an "array" as input. Example:
bash$ ./a.out "The blood of angry men!" "The dark of ages past!" "A world about to dawn!"
bash$ ./a.out "The blood of angry men!" "The dark of ages past!" "A world about to dawn!" "The night that ends at last!"
Red - the blood of angry men!
Black - the dark of ages past!
Red - a world about to dawn!
Black - the night that ends at last!
How it works
icreates a variableioutside of all functions, which means it's automatically initialized to 0.main(a,s)char**s;{declares the main function, which takes two arguments - anint a(# of command line arguments), and achar ** s(array of command line arguments).for(;a%2&++i<a;)is a loop that checks ifais even (a%2) and if it's less than the number of command line arguments passed (i<a).printf("%s - %c%s\n",i%2"Red":"Black",tolower(*s[i]),s[i]+1prints:- "Red" if
iis odd, "Black" ifiis even (i%2?"Red":"Black") - The first letter of the current command-line argument in lowercase (
tolower(*s[i])) - The string, without the first letter (
s[i]+1)
- "Red" if
Röda, 97 bytes
f a{seq 0,#a-1|[_,a[_1]/""]|[["Red","Black"][_%2],` - `,lowerCase(_[0]),_2[1:]&"",`
`]if[#a%2<1]}
V, 31, 30 bytes
êuòIRed -
IBlack -
òñFRdH
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.
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
Jelly, 29 bytes
0
“ZœĠk»ḲṁJżj€“ - ”Y
ỴŒl1¦€LĿ
A full program.
Uses the "falsey" output option for inputs with an odd number of lines.
How?
0 - Link 1, return a falsey value: no arguments
0 - well, yeah, zero - that's falsey.
“ZœĠk»ḲṁJżj€“ - ”Y - Link 2, format the lines: list of lists of characters, the lines
“ZœĠk» - "Red Black"
Ḳ - split at spaces -> ["Red","Black"]
J - range(length(lines)) -> [1,2,3,...,nLines]
ṁ - mould -> ["Red","Black","Red","Black",...,"Red","Black"]
ż - zip with lines -> [["Red",l1],["Black",l2],...]
“ - ” - " - " (really I cant find any save :/)
j€ - join for each -> ["Red - l1","Black - l2",...]
Y - join with newlines
ỴŒl1¦€LĿ - Main link: string
Ỵ - split at newlines
1¦€ - apply to index 1:
Œl - convert to lower case
Ŀ - call link at index:
L - length - Note: this is modular, so:
- link 2 is called for even, and
- link 1 is called for odd.