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!
\$\endgroup\$
  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$ – Stephen May 26 '17 at 17:56
  • \$\begingroup\$ By "or the length of the input is odd", are you talking about lines or characters? \$\endgroup\$ – Tutleman May 26 '17 at 18:01
  • \$\begingroup\$ @Tutleman number of lines, I'll clarify \$\endgroup\$ – Stephen May 26 '17 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\$ – DJMcMayhem May 26 '17 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\$ – Stephen May 26 '17 at 18:07

13 Answers 13

4
\$\begingroup\$

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
\$\endgroup\$
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.

\$\endgroup\$
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).

\$\endgroup\$
  • \$\begingroup\$ (h:t)!x=x++toLower h:t saves some bytes. \$\endgroup\$ – Laikoni May 27 '17 at 13:56
  • \$\begingroup\$ Switching the two cases saves another byte: f x|odd$length$x=Nothing|1<2=Just ... \$\endgroup\$ – Laikoni May 28 '17 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\$ – felixphew May 28 '17 at 11:10
  • \$\begingroup\$ mempty saves 1 byte compared to Nothing! \$\endgroup\$ – bartavelle Jun 4 '17 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\$ – Laikoni Jan 24 '18 at 10:15
3
\$\begingroup\$

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.

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¦€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.
\$\endgroup\$
3
\$\begingroup\$

Japt, 36 35 34 bytes

Outputs 0 for false. Includes an unprintable after the second R.

èR u ©¡[`R Black`¸gY '-XhXg v]¸}R

Try it online

\$\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!

\$\endgroup\$
  • \$\begingroup\$ Global variables are default-initialized, so you can omit the =0 part to save 2 more bytes. \$\endgroup\$ – Cody Gray May 27 '17 at 11:07
  • \$\begingroup\$ @CodyGray Oh yeah, thanks! \$\endgroup\$ – MD XF May 27 '17 at 14:03
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!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 30 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Retina, 58 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 41 bytes

qN/{(el\+}%2/"Red - 
Black - "N/f.\:~2/N*
\$\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!'
]))

\$\endgroup\$
  • \$\begingroup\$ @Stephen-S does this input count as valid? \$\endgroup\$ – Vitim.us Jun 5 '17 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!

\$\endgroup\$
  • \$\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\$ – Stephen May 27 '17 at 15:42
  • \$\begingroup\$ @Stephen S : Yes, I think you are right. \$\endgroup\$ – LMD May 27 '17 at 15:47
  • \$\begingroup\$ Got it down to 163 with some basic golfs \$\endgroup\$ – Esolanging Fruit Jun 4 '17 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) );

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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