What song is playing?
Retina, 242
Try it online!
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:
How it works:
IgnoreCase flag + Grep mode flag + Regex ^na( na)*$
. If the input is valid, print it as is. If not, print nothing.
iG`^na( na)*$
IgnoreCase flag + Match mode flag + Regex na
. Count the "na"s and print the number.
iM`na
If the string is exactly "8", replace by the second line.
m`^8$
>Batman Theme,Neal Hefti$n>Na Na Hey Hey Kiss Him Goodbye,Steam
If the string is exactly "10", replace by the second line.
m`^10$
>Katamari Damacy,Yuu Miyake
If the string is exactly "11", replace by the second line.
m`^11$
>Hey Jude,The Beatles
If the string matches [0-9].+
, replace by the second line. This is neither true for single digit numbers, 10
and 11
as they already have been repaced nor any of the above replacement strings.
[0-9].+
>Land Of 1000 Dances,Wilson Pickett
If none of the above matched, the string still starts with a number. Default to Toto, Africa.
m`^[0-9]
>Africa,Toto
Replace the placeholders >
and ,
by Song:
and Artist:
.
>
Song:
,
$nArtist:
JavaScript (ES6), 276 bytes
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: `))
Explanation
Input can optionally contain one trailing space.
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
)
Test
var prompt = () => input.value;
var alert = (text) => result.textContent = text;
var solution = () => {
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: `))
}
<input type="text" id="input" value="na na Na na NA na na nA" />
<button onclick="solution()">Go</button>
<pre id="result"></pre>
PowerShell, 278 bytes
- Can handle any amount of whitespace
- No regex whatsoever!
- Implicit typecasting 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'/')}
Ungolfed
@{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
)
}
Usage
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