9
\$\begingroup\$

In the musical rendition of Les Miserables, a song appears called "Red and Black." Here is part of that song:

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!

Source.

Your task is to turn input into a resounding "Red and Black" anthem.

Input

Text delimited by newlines, or a similar suitable text input. For example,

The blood of angry men!
The dark of ages past!
A world about to dawn!
The night that ends at last!

Empty input is undefined (out of scope).

Output

If the length (number of lines, or length of input array, or similar) of the input is odd, either output nothing or output falsey. Your submission may not output errors or attempt to output the correct output.

Otherwise, turn the input into a Red/Black lyric. Turn any uppercase letters at the start of a line to lowercase. Add Red plus a delimiter to the front of odd lines, and Black plus a (visible) delimiter to the front of even lines. The delimiter must also be surrounded in spaces, so the output looks uncrowded (and ungolfed ;) ).

You now have your output.

Test Cases

Output delimiter is -.

In:
The blood of angry men!
The dark of ages past!
A world about to dawn!
The night that ends at last!
Out:
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!
In:
test test
1
[][][]
BBB
Out:
Red - test test
Black - 1
Red - [][][]
Black - bBB
In:
I feel my soul on fire!
The color of desire!
The color of despair!
Out:
falsey OR nothing
In:
Red - I feel my soul on fire!
Black - My world if she's not there!
Out:
Red - red - I feel my soul on fire!
Black - black - My world if she's not there!
asked May 26, 2017 at 17:56
\$\endgroup\$
12
  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$ Commented May 26, 2017 at 17:56
  • \$\begingroup\$ By "or the length of the input is odd", are you talking about lines or characters? \$\endgroup\$ Commented May 26, 2017 at 18:01
  • \$\begingroup\$ @Tutleman number of lines, I'll clarify \$\endgroup\$ Commented May 26, 2017 at 18:03
  • 3
    \$\begingroup\$ I think this challenge is OK as is, but in general, making submissions handle invalid input doesn't really add a lot. Just for future reference. \$\endgroup\$ Commented May 26, 2017 at 18:05
  • \$\begingroup\$ @DJMcMayhem since there have been no answers, I'll make empty input undefined - makes more sense, thanks (I want to keep odd length input as is) \$\endgroup\$ Commented May 26, 2017 at 18:07

13 Answers 13

5
\$\begingroup\$

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.

answered May 26, 2017 at 18:17
\$\endgroup\$
0
5
\$\begingroup\$

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 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).

answered May 27, 2017 at 9:55
\$\endgroup\$
5
  • \$\begingroup\$ (h:t)!x=x++toLower h:t saves some bytes. \$\endgroup\$ Commented May 27, 2017 at 13:56
  • \$\begingroup\$ Switching the two cases saves another byte: f x|odd$length$x=Nothing|1<2=Just ... \$\endgroup\$ Commented May 28, 2017 at 6:16
  • \$\begingroup\$ @Laikoni thanks for these! I'm pretty new to Haskell and this is my first time golfing in it - and I'm really liking it! \$\endgroup\$ Commented May 28, 2017 at 11:10
  • \$\begingroup\$ mempty saves 1 byte compared to Nothing! \$\endgroup\$ Commented Jun 4, 2017 at 16:42
  • \$\begingroup\$ You can save another byte by defining c="Red - ":"Black - ":c and using c instead of cycle["Red - ","Black - "]: Try it online! \$\endgroup\$ Commented Jan 24, 2018 at 10:15
4
\$\begingroup\$

05AB1E, 26 bytes

Code:

|©v"†3⁄4ƒÏ"#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
 "†3⁄4ƒÏ"# # 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
answered May 26, 2017 at 18:33
\$\endgroup\$
3
\$\begingroup\$

Jelly, 29 bytes

0
"ZœĠk»ḲṁJżj€" - "Y
ỴŒl1¦€LL·

A full program.
Uses the "falsey" output option for inputs with an odd number of lines.

Try it online!

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¦€LL· - Main link: string
Ỵ - split at newlines
 1¦€ - apply to index 1:
 Œl - convert to lower case
 L· - call link at index:
 L - length - Note: this is modular, so:
 - link 2 is called for even, and
 - link 1 is called for odd.
answered May 26, 2017 at 19:46
\$\endgroup\$
3
\$\begingroup\$

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

  • i creates a variable i outside of all functions, which means it's automatically initialized to 0.
  • main(a,s)char**s;{ declares the main function, which takes two arguments - an int a (# of command line arguments), and a char ** s (array of command line arguments).
  • for(;a%2&++i<a;) is a loop that checks if a is 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]+1 prints:
    • "Red" if i is odd, "Black" if i is 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)

Try it online!

answered May 27, 2017 at 3:20
\$\endgroup\$
1
  • \$\begingroup\$ Global variables are default-initialized, so you can omit the =0 part to save 2 more bytes. \$\endgroup\$ Commented May 27, 2017 at 11:07
3
\$\begingroup\$

Japt -R, (削除) 36 (削除ここまで) (削除) 35 (削除ここまで) (削除) 34 (削除ここまで) 29 bytes

Takes input as an array of lines, outputs 0 for false.

Êv ©¡`R Black` ̧gY ̧p-XhXÎv1 ̧

Try it

Êv ©¡`...` ̧gY ̧p-XhXÎv1 ̧ :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
 1 : End push
 ̧ :Implicit output joined with newlines
answered May 26, 2017 at 19:24
\$\endgroup\$
2
\$\begingroup\$

Röda, 97 bytes

f a{seq 0,#a-1|[_,a[_1]/""]|[["Red","Black"][_%2],` - `,lowerCase(_[0]),_2[1:]&"",`
`]if[#a%2<1]}

Try it online!

answered May 27, 2017 at 5:51
\$\endgroup\$
1
\$\begingroup\$

Jelly, 30 bytes

LḂṆẋ@Ỵμ"ZœĠk»ḲṁL;€" - "żŒl1¦€Y

Try it online!

answered May 26, 2017 at 19:20
\$\endgroup\$
0
1
\$\begingroup\$

Retina, 58 bytes

Tm`L`l`^.
m`^
Red - 
(Red - .*¶)Red
1ドルBlack
^(.*¶.*¶)*.*$

Try it online!

answered May 26, 2017 at 20:56
\$\endgroup\$
1
\$\begingroup\$

CJam, 41 bytes

qN/{(el\+}%2/"Red - 
Black - "N/f.\:~2/N*
answered Jun 4, 2017 at 16:35
\$\endgroup\$
1
\$\begingroup\$

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!'
]))

answered Jun 5, 2017 at 1:29
\$\endgroup\$
1
  • \$\begingroup\$ @Stephen-S does this input count as valid? \$\endgroup\$ Commented Jun 5, 2017 at 14:53
1
\$\begingroup\$

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:]

Try it online!

answered May 27, 2017 at 15:14
\$\endgroup\$
3
  • \$\begingroup\$ Can you save some bytes by using single spaces for indentation? I don't know Python that well but I think that works \$\endgroup\$ Commented May 27, 2017 at 15:42
  • \$\begingroup\$ @Stephen S : Yes, I think you are right. \$\endgroup\$ Commented May 27, 2017 at 15:47
  • \$\begingroup\$ Got it down to 163 with some basic golfs \$\endgroup\$ Commented Jun 4, 2017 at 16:47
1
\$\begingroup\$

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) );

answered Jun 4, 2017 at 23:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.