创建自己的红色和黑色


9

在悲惨世界的音乐作品中,有一首歌叫《红与黑》。这是这首歌的一部分:

红色-愤怒的人的鲜血!

黑色-过去的黑暗!

红色-一个即将黎明的世界!

黑色-最后的夜晚!

资源。

您的任务是将输入转换成响亮的“红色和黑色”国歌。

输入项

用换行符分隔的文本,或类似的合适文本输入。例如,

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

空输入未定义(超出范围)。

输出量

如果输入的长度(行数或输入数组的长度,或类似值)是奇数,则不输出任何内容或输出false。您提交的内容可能不会输出错误,也不会尝试输出正确的输出。

否则,将输入变成红色/黑色歌词。将行首的所有大写字母变为小写。添加Red加一个分隔符的奇数行的前面,Black加上一个(可见)分隔符的偶数行的前面。定界符也必须用空格包围,因此输出看起来是不拥挤的(和松散的;)。

现在您有了输出。

测试用例

输出定界符为-

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!


通过“或输入的长度是奇数”,您是在谈论线条还是字符?
图特曼(Tutleman)'17

@Tutleman行数,我会澄清
Stephen

3
我认为这个挑战照样可以解决,但总的来说,让提交处理无效输入并不会增加多少。仅供将来参考。
詹姆斯

@DJMcMayhem既然没有答案,我将使空输入变得不确定-更加有意义,谢谢(我想保持原样的奇数长度输入)
斯蒂芬

Answers:


4

05AB1E,26个字节

码:

|©v”†¾ƒÏ”#Nè… - yćlìJ®gÈ×,

使用05AB1E编码。在线尝试!

说明:

|                               # 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

5

V31,30字节

êuòIRed - 
IBlack - 
òñFRdH

在线尝试!

十六进制转储:

00000000: 16ea 75f2 4952 6564 202d 201b 0a49 426c  ..u.IRed - ..IBl
00000010: 6163 6b20 2d20 1b0a f2f1 4652 6448       ack - ....FRdH

在V中这是微不足道的,但是奇数输入的边缘情况使它变得棘手,因为V实际上没有条件。值得庆幸的是,我们可以以相对较小的+6字节代价处理此问题。


5

Haskell中104个 120 113 112 111 110字节

import Data.Char
f x|odd$length$x=mempty|1<2=Just$zipWith(\(h:t)x->x++toLower h:t)x$cycle["Red - ","Black - "]

在线尝试!

毫无解释

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是一个函数,它接受一个字符串列表(又称为Chars 列表),并返回Maybe相同的值。Haskell的函数非常“纯”,因此我们需要弄清楚该函数可能不会返回任何内容。(类型的函数Maybe a返回NothingJust a)。

|运营商是一个后卫-一种条件。如果even $ length $ x(是另一种书写方式even (length x))为,则遵循第一个分支True。否则,1<2接着第二个(在高尔夫球示例中,这总是正确的),然后返回Nothing

zipWith采用两个参数的函数,并将其应用于两个列表的每个元素。我们在这里使用的功能是\(h : t) x -> x ++ toLower h : th : t隐式地将第一个字符从第一个参数中分离出来,这是您可以在Haskell中完成的一件好事。第一个列表是输入(我们已经知道包含偶数行),第二个列表是无限交替的“ Red-”和“ Black-”(无限列表是另一种可能的方法,这次是因为Haskell是懒惰-它只在乎您使用的东西)。


(h:t)!x=x++toLower h:t保存一些字节。
Laikoni '17

切换这两种情况可以节省另一个字节:f x|odd$length$x=Nothing|1<2=Just ...
Laikoni '17

@Laikoni感谢这些!我对Haskell相当陌生,这是我第一次打高尔夫球-我真的很喜欢它!
felixphew

memptyNothing!相比节省了1个字节!
bartavelle

您可以通过定义c="Red - ":"Black - ":c和使用c而不是保存另一个字节cycle["Red - ","Black - "]在线尝试!
Laikoni '18

3

果冻,29 个字节

0
“ZœĠk»ḲṁJżj€“ - ”Y
ỴŒl1¦€LĿ

完整的程序。
对行数为奇数的输入使用“ falsey”输出选项。

在线尝试!

怎么样?

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.

3

Japt36 35 34字节

输出0false。在第二个之后包含不可打印的内容R

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

在线尝试


3

C,112个 107 105 103 99字节

-4多亏了ASCII码
-2多亏了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);}

采用“数组”作为输入。例:

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!

怎么运行的

  • ii在所有函数之外创建一个变量,这意味着它将自动初始化为0。
  • main(a,s)char**s;{声明主函数,该函数带有两个参数-一个int a(命令行参数的数量)和一个char ** s(命令行参数的数组)。
  • for(;a%2&++i<a;)是一个循环,检查是否a为偶数(a%2),以及是否小于传递的命令行参数数(i<a)。
  • printf("%s - %c%s\n",i%2"Red":"Black",tolower(*s[i]),s[i]+1 印刷品:
    • “红色” i为奇数,“黑色” i为偶数(i%2?"Red":"Black"
    • 当前命令行参数的首字母小写(tolower(*s[i])
    • 字符串,不带首字母(s[i]+1

在线尝试!


全局变量是默认初始化的,因此您可以省略该=0部分以节省另外2个字节。
科迪·格雷

@CodyGray哦,是的,谢谢!
MD XF





1

JavaScript(ES6),93个字节

将歌曲视为多行。

S=>S.length%2?0:S.map((L,i)=>(i%2?'Black':'Red')+' - '+L[0].toLowerCase()+L.slice(1)).join`
`


@ Stephen-S此输入是否有效?
Vitim.us

1

Python 2,215-> 184-> 165字节

根据Stephen S的评论保存了31个字节

Challenger5将其压缩到165个字节

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

在线尝试!


您可以通过使用单个空格缩进来节省一些字节吗?我不太了解Python,但我认为这行得通
Stephen

@Stephen S:是的,我认为你是对的。
LMD


1

Javascript,118个字节

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

测试

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.