Попробуйте онлайн!
"song artist"
Как это работает:
Флаг IgnoreCase + флаг режима Grep + регулярное выражение „ŽŸŒá
. If the input is valid, print it as is. If not, print nothing.
"africa0 Toto\nbatman theme0Neal Hefti0ÿ hey hey kiss him goodbye0 steam\n\nKatamari damarcy0Yuu Miyake\nhey Jude0 the beatles\nland of 2 dances0 wilson pickett"
Флаг IgnoreCase + Флаг режима сопоставления + Regex “‹…0Toto ߃›Ì0Neal Hefti0ÿ¤·¤··—„š‚¿bye0ÁÜ\n\nKatamariÊÖacy0Yuu Miyake\n¤· Jude0€€ÊÄ\nˆÑ€‚ 2–›s0¦‹šèett“
. Count the "na"s and print the number.
l # Convert the (implicit) input-string to lowercase
# # Split it on spaces (if there are any)
„naQ # Check for each word if it's equal to "na" (1 if truthy; 0 if falsey)
W # Push its minimum, without popping the list itself
* # Multiply that to all integers
# (if any word wasn't "na", the entire list now only contains 0s)
O # Sum this list to get the amount of "na"s
5Ý # Push the list [0,1,2,3,4,5]
8+ # Add 8 to each: [8,9,10,11,12,13]
@ # Check for each if the sum is larger than or equal to this value
# (1 if sum>=n, 0 if sum<n)
O # Sum to get the amount of truthy values
I # Push the input-string
5£ # Pop and only leave its first 5characters (if it's a valid string,
# this will be "na na" - with variable casing)
“‹…0Toto
߃›Ì0Neal Hefti0ÿ¤·¤··—„š‚¿bye0ÁÜ
KatamariÊÖacy0Yuu Miyake
¤· Jude0€€ÊÄ
ˆÑ€‚ 2–›s0¦‹šèett“
# Push dictionary string "africa0 Toto\nbatman theme0Neal Hefti0ÿ hey hey kiss him goodbye0 steam\n\nKatamari damarcy0Yuu Miyake\nhey Jude0 the beatles\nland of 2 dances0 wilson pickett",
# where the `ÿ` is automatically replaced with the 5-char string
¶¡ # Split this string on newlines
0δ¡ # And then split each inner string on 0s
# [["africa"," Toto"],
# ["batman theme","Neal Hefti","na na hey hey kiss him goodbye"," steam"],
# [],
# ["Katamari damarcy","Yuu Miyake"],
# ["hey Jude"," the beatles"],
# ["land of 2 dances"," wilson pickett"]]
2₄: # Replace the 2 with 1000
¬ # Get the first pair (without popping): ["africa"," Toto"]
2ǝ # Insert it at (0-based) index 2, replacing the empty list
„ŽŸŒá # Push dictionary string "song artist"
# # Split it on spaces: ["song","artist"]
Þ # Cycle this list infinitely: ["song","artist","song","artist",...]
r # Reverse the values on the stack
è # Modulair 0-based index the sum of truthy values into the list of lists of
# artist(s)/song(s), so 6 will wrap back to the first item
δ # Map over each string inside each inner list:
ð Û # And trim all leading spaces
ø # Create pairs with the infinitely cycled list, where it will only create
# as many pairs as the smallest list (so either 2 or 4 in this case)
„: ý # Join each inner pair with ": " delimiter
» # Then join everything by newlines
™ # And finally titlecase each word in the string
# (after which the result is output implicitly)
Если строка равна ровно «8», замените ее второй строкой.
l#„naQW*O5Ý8+@OI5£“‹…0Toto
߃›Ì0Neal Hefti0ÿ¤·¤··—„š‚¿bye0ÁÜ
KatamariÊÖacy0Yuu Miyake
¤· Jude0€€ÊÄ
ˆÑ€‚ 2–›s0¦‹šèett“¶¡0δ¡2₄:¬2ǝ„ŽŸŒá#ÞrèðδÛø„: ý»™
Если строка равна ровно «10», замените ее второй строкой.
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"
Если строка равна ровно «11», замените ее второй строкой.
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");}
Если строка соответствует ; sh $THE_FILE na na na na na na na
Song: Batman Theme
Artist: Neil Hefti
Song: Na Na Hey Kiss Him Goodbye
Artist: Steam
, replace by the second line. This is neither true for single digit numbers, uudecode -p<<<'begin-base64 0 -
/Td6WFoAAATm1rRGAgAhARwAAAAQz1jM4AE9AIRdACmbycaLOYv90Azj3HBohRZGa6a5FikeJU+8JbGlB4f9wmi2aQ+mn81aypMRM4nEKPxW439Z2C4WfGeyrREjk427H1GrCRUMHdth+9lVBDiPfCaD8WpStyS0MtJenHKB+tou8rD9INq0yw9IPXuQe7zLM3p14ZDFO8UoJp66scd4bKQAAACSwUb7ERvesgABoAG+AgAARAEAGbHEZ/sCAAAAAARZWg==
===='|unxz|sed -n $#s/,/\\n/gp|grep .||echo $'Song: Land Of 1000 Dances\nArtist: Wilson Pickett'
и 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,l=0,j=1;for(;*s;s+=3-!s[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]);}
as they already have been repaced nor any of the above replacement strings.
$_ = 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 $_
Если ничего из вышеперечисленного не соответствует, строка все равно начинается с цифры. По умолчанию — Тото, Африка.
$_=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
Замените заполнители $_=/^(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
и -pa
к 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);
}
and 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)}
.
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")
||answer||
JavaScript (ES6), 276 байт
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]
Объяснение
Ввод может содержать один конечный пробел.
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}
'
Тест
Artist:
Song: