Function Name: lyrics Inputs: 1. (char) A character vector containing the title
ID: 3749731 • Letter: F
Question
Function Name: lyrics
Inputs:
1. (char) A character vector containing the title and artist of a song, separated by a comma
2. (char) A character vector containing the lyrics to a song
Outputs:
1. (char) A vector of characters describing your opinion of the song
Background: All your life you have aspired to become the greatest DJ in the world. Soon, you will have the chance to prove yourself- as the headlining DJ of Music Midtown! Not wanting to ruin this once in a lifetime opportunity, you want to ensure all the songs you remix and sample are the best of the best. You realize there’s not enough time to go through thousands of lyrics, so you use your best friend MATLAB to help you get the job done!
Function Description: Write a function that uses the following criteria to rate a song's lyrics: 1. If the song mentions either 'MATLAB' or '1371' at least once, it earns 20 points. 2. If the song is performed by Taylor Swift, it earns 10 points. 3. If the song is performed by Kanye West, it loses 5 points. 4. If the song mentions your favorite color, 'gold', and does not mention your least favorite color, 'red', it earns 15 points. Your function should output a vector of characters in the following format: ' received a score of points.'
Example: >> out = lyrics('Delicate,Taylor Swift', 'This ain't for the best. My reputation's never been gold, so you must like me for me. Yeah, I want you. We can't make 1371 promises now, can we, babe?')
out => 'Delicate received a score of 45 points.'
Notes:
'Taylor Swift', 'Kanye West', and 'MATLAB' will appear exactly as shown, case sensitive.
You should ignore case when finding 'red' and 'gold'. If these words appear as part of another word, you should still count them. You should not use conditionals to solve this problem.Hints:
Performing a mathematical operation on a logical will result in its automatic conversion to a double.
Vectors of the same length can be multiplied together elementwise using .*
You may find the strtok() and sprintf() functions to be useful.
Explanation / Answer
The below code is written in C# language.
Inline Comments have been given to explain every stem properly.
public static string Lyrics(string titleArtist, string song)
{
string opinion = "";
int points = 0; // Initializing the point variable to 0. we will use this to store the total point of the song.
string[] titleArtistArray = titleArtist.Split(','); //stored the artist and title of the song in an array.
string songUpper = song.ToUpper(); //Converted the lyrics to check for case insensitive 'GOLD' and 'RED'
if (song.Contains("MATLAB") || song.Contains("1371")) //implementing Condition 1, if the lyrics contains MATLAb or 1371, then 20 points are added
{
points += 20;
}
if (titleArtistArray[1] == "Taylor Swift") //Implementing Condition 2, if the artist of song is 'Taylor Swift' then 10 points are added
{
points += 10;
}
if (titleArtistArray[1] == "Kanye West") //Implementing Condition 3, if the artist of the song is 'Kayne West', then 5 points are deducted.
{
points -= 5;
}
if (songUpper.Contains("GOLD") && !songUpper.Contains("RED")) //if the lyrics contains 'gold' and doesnot contain 'red' (both case insensitive) then 15 points are added.
{
points += 15;
}
//Accumulated the available data to produre the desired output.
opinion = titleArtistArray[0] + " received a score of " + points + " points.";
return opinion;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.