正在播放什么歌曲?


33

受此xkcd启发

在此处输入图片说明

您为Shazam工作,他们为您提供了一个项目。一些客户抱怨他们的应用程序占用了手机太多的空间,因此他们希望您编写精简版的应用程序代码。不幸的是,您现有的代码只能理解单词“ na”,因此您必须尽快发货。没关系,我们将竭尽所能。

挑战

您必须编写一个完整的程序来接受用户输入,或者接受命令行参数,然后打印歌曲的标题和艺术家。由于我们正在努力解决抱怨程序大小的客户,因此您的代码必须尽可能短。输入将是一个完全由na组成的字符串,它们之间有一个空格。小写/大写是任意的。这被认为是有效的输入:Na Na nA na NA这是无效的输入:nah nah NA naNa banana您必须确定正在播放的歌曲,并以这种格式将其打印出来:

Song: <trackname>
Artist: <artist>

如果输入正好是 8 na,则匹配两首独立的歌曲,因此您必须同时打印两首:

Song: Batman Theme
Artist: Neal Hefti

Song: Na Na Hey Hey Kiss Him Goodbye
Artist: Steam

如果输入正好是 10 na,则必须打印:

Song: Katamari Damacy
Artist: Yuu Miyake

如果输入正好是 11 na,则必须打印:

Song: Hey Jude
Artist: The Beatles

如果输入的数字是12或更多,则必须打印

Song: Land Of 1000 Dances
Artist: Wilson Pickett

最后,如果输入无效,少于8个na或任何单词都不是“ na”,则您的程序无法理解音乐。从逻辑上讲,可能只有另一首歌曲。您必须打印:

Song: Africa
Artist: Toto

像往常一样,存在标准漏洞,而最短的答案以字节为单位。


2
很棒的背景故事!
TanMath

嘿裘德不是12 na秒吗?我只是听了一下,以为quarter quarter quarter quarter / eighth sixteenth sixteenth quarter-quarter-quarter / eighth sixteenth quarter-quarter-quarter是12 na秒(就音符长度而言)。
Arcturus

4
@Ampora onnnnnnnnne-one-three-one-a-two-threeeeeeeeeee-one-a-two-threeeeeeee-hey-jude绝对是11
quintopia 2015年

1
蝙蝠侠是na na / na na / na na / na nax2 batman。我注意到我第二次看漫画。
wizzwizz4 2015年

2
现在改变挑战还为时3年,但我必须反对,Katamari Damacy主题的标题为“岩石上的Katamari”(或者,如果您是纯粹主义者,则正式将其命名为“岩石上的Katamari〜主主题”),因此不应仅被列为“ Katamari Damacy”!
价值墨水

Answers:


7

视网膜,242

在线尝试!

iG`^na( na)*$
iM`na
m`^8$
>Batman Theme,Neal Hefti$n>Na Na Hey Hey Kiss Him Goodbye,Steam
m`^10$
>Katamari Damacy,Yuu Miyake
m`^11$
>Hey Jude,The Beatles
[0-9].+
>Land Of 1000 Dances,Wilson Pickett
m`^[0-9]
>Africa,Toto
>
Song: 
,
$nArtist: 

怎么运行的:

IgnoreCase标志+ Grep模式标志+正则表达式^na( na)*$。如果输入有效,请按原样打印。如果没有,则不打印任何内容。

iG`^na( na)*$

IgnoreCase标志+匹配模式标志+正则表达式na。数数“ na”并打印数字。

iM`na

如果字符串恰好是“ 8”,请替换第二行。

m`^8$
>Batman Theme,Neal Hefti$n>Na Na Hey Hey Kiss Him Goodbye,Steam

如果字符串恰好是“ 10”,则用第二行替换。

m`^10$
>Katamari Damacy,Yuu Miyake

如果字符串恰好是“ 11”,请替换第二行。

m`^11$
>Hey Jude,The Beatles

如果字符串匹配[0-9].+,则用第二行替换。这对于单个数字,已经被替换掉的上述数字10以及11上述任何替换字符串都不成立。

[0-9].+
>Land Of 1000 Dances,Wilson Pickett

如果以上都不匹配,则字符串仍以数字开头。默认为非洲托托。

m`^[0-9]
>Africa,Toto

替换占位符>,并,通过Song:Artist:

>
Song: 
,
$nArtist: 

5

JavaScript(ES6),276个字节

alert(`Song: `+([,`Batman Theme,Neal Hefti
Song: Na Na Hey Hey Kiss Him Goodbye,Steam`,,`Katamari Damacy,Yuu Miyake`,`Hey Jude,The Beatles`,`Land Of 1000 Dances,Wilson Pickett`][+prompt(i=0).replace(/na( |$)/gi,_=>++i)&&(i>11?4:i-7)]||`Africa,Toto`).replace(/,/g,`
Artist: `))

说明

输入可以选择包含一个尾随空格。

alert(                 // output the result
  `Song: `+([          // insert the "Song:" label
      ,                // set the first element to undefined in case input is empty

      // Songs
      `Batman Theme,Neal Hefti
Song: Na Na Hey Hey Kiss Him Goodbye,Steam`,
      ,
      `Katamari Damacy,Yuu Miyake`,
      `Hey Jude,The Beatles`,
      `Land Of 1000 Dances,Wilson Pickett`

    ][
      +                // if the input string was made up only of "na"s, the replace would
                       //     return a string containing only digits, making this return a
                       //     number (true), but if not, this would return NaN (false)
        prompt(        // get the input string
          i=0          // i = number of "na"s in input string
        ).replace(     // replace each "na" with a number
          /na( |$)/gi, // find each "na"
          _=>++i       // keep count of the "na"s and replace with a (non-zero) number
        )
      &&(i>11?4:i-7)   // select the song based on the number of "na"s
    ]
      ||`Africa,Toto`  // default to Africa
  ).replace(/,/g,`
Artist: `)             // insert the "Artist:" label
)

测试


这对于9 na无效,它输出kamari。
Rɪᴋᴇʀ

@RikerW固定。我忘了一个逗号...
user81655

4

PowerShell,278个字节

  • 可以处理任意数量的空格
  • 没有正则表达式!
  • 隐式排版FTW!
@{8='Batman Theme/Neal Hefti','Na Na Hey Hey Kiss Him Goodbye/Steam'
10='Katamari Damacy/Yuu Miyake'
11='Hey Jude/The Beatles'
12='Land Of 1000 Dances/Wilson Pickett'}[[math]::Min($args.Count*!($args|?{$_-ne'na'}),12)]|%{'Song: {0}
Artist: {1}'-f($_+'Africa/Toto'*!$_-split'/')}

不打高尔夫球

@{8='Batman Theme/Neal Hefti','Na Na Hey Hey Kiss Him Goodbye/Steam' # array
10='Katamari Damacy/Yuu Miyake'
11='Hey Jude/The Beatles'
12='Land Of 1000 Dances/Wilson Pickett'} # Hashtable of songs
[   # Get value by key from hashtable
    # If key is invalid, silently return null value

    [math]::Min( # Clamp max value to 12
        $args.Count* # Multiply count of argumens
                     # true/false will be cast to 1/0
            ! # Negate result of expression
              # Will cast empty array to 'false'
              # and non-empty array to 'true'
            (
                # Return non-empty array if input arguments
                # contain anything other than 'na'
                $args | Where-Object {$_ -ne 'na'} 
            ),
        12
    )
] | ForEach-Object { # Send value from hashtable down the pipeline,
                     # This allows to process arrays in hasthable values
    'Song: {0}
    Artist: {1}' -f ( # Format string
        $_+ # Add to current pipeline variable
            'Africa/Toto'*!$_ # If pipeline variable is empty,
                              # then add default song to it
                              # Example: 'Test'*1 = 'Test'
                              #          'Test'*0 = null
        -split '/' # Split string to array for Format operator
    )
}

用法

PS > .\WhatSong.ps1 na na na na na na na na
Song: Batman Theme
Artist: Neal Hefti
Song: Na Na Hey Hey Kiss Him Goodbye
Artist: Steam

PS > .\WhatSong.ps1 Na na na na na na na na na Na
Song: Katamari Damacy
Artist: Yuu Miyake

PS > .\WhatSong.ps1 Na na na na na na na na na BanaNa
Song: Africa
Artist: Toto

1

sh + coreutils,290

尽管比我的其他论文要长,但它很简单并且几乎没有问题,因此无论如何我都将其包括在内。

grep -Ei "^na( na)*$"|wc -w|awk '{s="Song: ";a="\nArtist: ";p=s"Africa"a"Toto"}$1==8{p=s"Batman Theme"a"Neal Hefti\n"s"Na Na Hey Hey Kiss Him Goodbye"a"Steam"}$1>9{p=s"Katamari Damacy"a"Yuu Miyake"}$1>10{p=s"Hey Jude"a"The Beatles"}$1>11{p=s"Land Of 1000 Dances"a"Wilson Pickett"}{print p}'

怎么运行的:

如果输入有效,请按原样打印。如果没有,则不打印任何内容。

grep -Ei "^na( na)*$"

数数单词。

wc -w

简单查找表,Song:Artist:保存在变量中。

awk '
    {s="Song: ";a="\nArtist: ";p=s"Africa"a"Toto"}
    $1==8{p=s"Batman Theme"a"Neal Hefti\n"s"Na Na Hey Hey Kiss Him Goodbye"a"Steam"}
    $1>9{p=s"Katamari Damacy"a"Yuu Miyake"}
    $1>10{p=s"Hey Jude"a"The Beatles"}
    $1>11{p=s"Land Of 1000 Dances"a"Wilson Pickett"}
    {print p}
'

我知道已经有一段时间了,但是regex可以缩短为^(na ?)+$
凯文·克鲁伊森

1

蟒蛇453 440 406 380字节

编辑:感谢Cyoce减少了13个字节!

编辑:再次感谢Cyoce!

编辑:感谢RainerP。帮助我在某些无效情况下改进算法。

这是Python程序的草稿。我相信它绝对可以打高尔夫球,可能达到300-400字节。但是很快就会解决。

f=0
S='Song:'
A='\nArtist:'
l="Batman Theme,Neal Hefti,Na Na Hey Kiss Him Goodbye,Steam,Katamari Damacy,Yuu Miyake,Hey Jude,Beatles,Land of the 1000 Dances,Wilson Pickett,Africa,Toto".split(',')
s=raw_input().lower()+" "
n=s.count("na ")
n*=n*3==len(s)
if n>11:f=8
if n==10:f=4
if n==11:f=6
if n<8or n==9:f=10
if f:print S+l[f]+A+l[f+1]
else:print S+l[0]+A+l[1]+"\n"+S+l[2]+A+l[3]

试试这里!


而不是冗长的清单,请使用"Batman Theme,Neal Hefti,Na Na Hey Kiss Him Goodbye,Steam,Katamari Damacy,Yuu Miyake,Hey Jude,Beatles,Land of the 1000 Dances,Wilson Pickett,Africa,Toto".split(',')
Cyoce

另外:if i not in ["n","a"," "]: ...您可以使用代替我相信的if i not in 'na ': ...。此外,if f==0: somecode; else: somemorecode可以减少为if f: somemorecode; else: somecode(0为
Falsy

甚至更多(我应该将所有这些合而为一,哦):您有"\nArtist:"3次。尝试设置一个变量,例如A="\nArtist:",然后使用A代替字符串文字。可以使用进行相同操作"Song:"。另外,我认为if n<8or n==9:f=10可以移动的,如果报表的顶部和改变if n!=8:f=10
Cyoce

您的程序无法检测到无效输入。输出Batman Theme代替Africafor na na na nan na na na na
Rainer P.

@RainerP。谢谢...我知道我丢失了某些东西...我现在正在研究一种更新的算法
TanMath 2015年

1

朱莉娅,325字节

可能会打更多的高尔夫球。

p(s,a)=println("Song: $s\nArtist: $a");ismatch(r"^(na )*na$",ARGS[1])&&(c=length(split(ARGS[1],"na"))-1)==8?(p("Batman Theme","Neal Hefti"),p("Na Na Hey Hey Kiss Him Goodbye","Steam")):c==10?p("Katamari Damacy","Yuu Miyake"):c==11?p("Hey Jude","The Beatles"):c>=12?p("Land Of 1000 Dances","Wilson Pickett"):p("Africa","Toto")

我知道已经有一段时间了,但是regex可以缩短为^(na ?)+$
凯文·克鲁伊森

另外,可以使用<>代替==:来缩短检查时间&&(c=length(split(ARGS[1],"na"))-1)<9?(p("Batman Theme","Neal Hefti"),p("Na Na Hey Hey Kiss Him Goodbye","Steam"))c>11?p("Land Of 1000 Dances","Wilson Pickett"):c>10?p("Hey Jude","The Beatles"):c>9?p("Katamari Damacy","Yuu Miyake"):p("Africa","Toto")。题外话:我喜欢您的头像。上周看完SAO。;)
Kevin Cruijssen

1

锈, 501 477字节

fn main(){let(mut i,mut n)=(String::new(),0);let(s,a);std::io::stdin().read_line(&mut i);i=i.trim().to_lowercase();let o=i.split(" ");for w in o{if w!="na"{n=0;break}else{n+=1}}match n{8=>{println!("Song: Batman Theme\nArtist: Neal Hefti");s="Na Na Hey Hey Kiss Him Goodbye";a="Steam"}10=>{s="Katamari Damacy";a="Yuu Miyake"}11=>{s="Hey Jude";a="The Beatles"}_=>{if n>=12{s="Land Of 1000 Dances";a="Wilson Pickett"}else{s="Africa";a="Toto"}}}print!("Song: {}\nArtist: {}",s,a)}

不打高尔夫球

fn main() {
    let (mut input_string, mut na_counter) = (String::new(), 0);
    let (song_name, artist_name);

    std::io::stdin().read_line(&mut input_string);
    input_string = input_string.trim().to_lowercase();
    let output = input_string.split(" ");

    for word in output {
        if word != "na" {
            na_counter = 0;
            break;
        } else {
            na_counter += 1;
        }
    }

    match na_counter {
        8 => {
            println!("Song: Batman Theme\nArtist: Neal Hefti");
            song_name = "Na Na Hey Hey Kiss Him Goodbye";
            artist_name = "Steam";
        }
        10 => {
            song_name = "Katamari Damacy";
            artist_name = "Yuu Miyake";
        }
        11 => {
            song_name = "Hey Jude";
            artist_name = "The Beatles";
        }
        _ => {
            if na_counter >= 12 {
                song_name = "Land Of 1000 Dances";
                artist_name = "Wilson Pickett";
            } else {
                song_name = "Africa";
                artist_name = "Toto";
            }
        }
    }

    print!("Song: {}\nArtist: {}", song_name, artist_name);
}

编辑:删除了不必要的to_string和类型注释


1

Perl -pa 5、248字节

$_=/^(na ?)+$/&&(@F==8?",Batman Theme;Neal Hefti,Na Na Hey Hey Kiss Him Goodbye;Steam":@F==10?"Katamari Damacy;Yuu Miyake":@F==11?",Hey Jude;The Beatles":@F>11?",Land Of 1000 Dances;Wilson Pickett":0)||",Africa;Toto";s/;/
Artist: /gm;s/,/
Song: /gm

在线尝试!


1

Perl 5中312个 292字节

$_=lc<>;$n="(na ?)";/^(na ){7}na$|(na ){9,}na/ or$_="%Africa&Toto";s/$n{12,}/%Land Of 1000 Dances&Wilson Pickett/;s/$n{11}/%Hey Jude&The Beatles/;s/$n{10}/%Katamari Damacy&Yuu Miyake/;s/$n{8}/%Batman Theme&Neal Hefti\n%Na Na Hey Hey Kiss Him Goodbye&Steam/;s/&/\nArtist: /g;s/%/Song: /g;print

在线尝试!

取消高尔夫:

$_ = lc <STDIN>;
$_ =~ /^(na ){7}na$|(na ){9,}na/ or $_ = "%Africa&Toto";
$_ =~ s/(na ?){12,}/%Land Of 1000 Dances&Wilson Pickett/;
$_ =~ s/(na ?){11}/%Hey Jude&The Beatles/;
$_ =~ s/(na ?){10}/%Katamari Damacy&Yuu Miyake/;
$_ =~ s/(na ?){8}/%Batman Theme&Neal Hefti\n%Na Na Hey Hey Kiss Him Goodbye&Steam/;
$_ =~ s/&/\nArtist: /g;
$_ =~ s/%/Song: /g;
print $_

我错过了一些案例,现在就进行修复
pslessard

1

C(GCC) 403个 395 370 365字节

-8 -5个字节,感谢ceilingcat

尽可能简单明了。

f(char*s){int*a[]={"Neal Hefti","Steam","Yuu Miyake","The Beatles","Wilson Pickett","Toto","Batman Theme","Na Na Hey Hey Kiss Him Goodbye","Katamari Damacy","Hey Jude","Land Of 1000 Dances","Africa"},i=1,l=0,j=1;for(;*s;s+=s[2]?3:2)i=(*s|32)^'n'|(s[1]|32)^97|s[2]>32,l++;for(i=i?5:l^8?l^10?l^11?l>11?4:5:3:2:j++;j--;)printf("Song: %s\nArtist: %s\n",a[6+i--],a[i]);}

在线尝试!


0

Java 8,353字节

s->{int n=s.split(" ").length,b=s.matches("(na ?)+")?1:0;s="Africa";return"Song: "+(b>0?n<8?s:n<9?"Batman Theme\nArtist: Neal Hefti\nSong: Na Na Hey Hey Kiss Him Goodbye":n>11?"Land of 1000 Dances":n>10?"Hey Jude":n>9?"Katamari Damacy":"":s)+"\nArtist: "+(b>0?n<8?"Toto":n<9?"Steam":n>11?"Wilson Pickett":n>10?"The Beatles":n>9?"Yuu Miyake":"":"Toto");}

说明:

在线尝试。

s->{                             // Method with String as both parameter and return-type
  int n=s.split(" ").length,     //  The amount of words when split by spaces
      b=s.matches("(na ?)+")?1:0;//  Whether the input matches the regex "^(na ?)+$"
  s="Africa";                    //  Set the input we no longer need to "Africa"
  return"Song: "                 //  Return "Song: "
    +(b>0?                       //   +If the input matched the regex:
       n<8?                      //     If there are less than 8 "na"'s: 
        s                        //      Append "Africa"
       :n<9?                     //     Else-if there are exactly 8 "na"'s:
        "Batman Theme\nArtist: Neal Hefti\nSong: Na Na Hey Hey Kiss Him Goodbye"
                                 //      Append the String above
       :n>11?                    //     Else-if there are 12 or more "na"'s:
        "Land of 1000 Dances"    //      Append "Land of 1000 Dances"
       :n>10?                    //     Else-if there are exactly 11 "na"'s:
        "Hey Jude"               //      Append "Hey Jude"
       :n>9?                     //     Else-if there are exactly 10 "na"'s:
        "Katamari Damacy"        //      Append "Katamari Damacy"
       :                         //     Else (there are exactly 9 "na"'s):
        ""                       //      Append nothing
      :                          //    Else:
       s)                        //     Append "Africa"
    +"\nArtist: "                //   +Append a new-line and "Artist: "
    +(b>0?                       //   +If the input matched the regex:
       n<8?                      //     If there are less than 8 "na"'s:
        "Toto"                   //      Append "Toto"
       :n<9?                     //     Else-if there are exactly 8 "na"'s:
        "Steam"                  //      Append "Steam"
       :n>11?                    //     Else-if there are 12 or more "na"'s:
        "Wilson Pickett"         //      Append "Wilson Pickett"
       :n>10?                    //     Else-if there are exactly 11 "na"'s:
        "The Beatles"            //      Append "The Beatles"
       :n>9?                     //     Else-if there are exactly 10 "na"'s:
        "Yuu Miyake"             //      Append "Yuu Miyake"
       :                         //     Else (there are exactly 9 "na"'s):
        ""                       //      Append nothing
      :                          //    Else:
       "Toto");}                 //     Append "Toto"
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.