[Tutor] A Python Newbie Requesting help with Lambdas, Filters, and Maps

Rahul Alawani rsalawani at hotmail.com
Mon May 20 15:43:49 EDT 2019


Hello Tutors!

I am an ambitious Python newbie without any CS background!  I am currently taking an online 30-hour/36-lecture Python course which I am enjoying a lot.  I am religiously answering the quizzes and solving all the exercise problems (usually short and straightforward).  

So far, I have learned introductory topics such as data types, lists/dictionaries/tuples/sets, conditional statements, for and while loops, functions, and apparently uncommonly used lambdas (along with maps and filters).  I know that my real, long battle lies ahead.

Almost midway through the course, with the latest course content on lambdas, filters, maps, min/max built-in functions and created a long problem statement for an interesting self-exercise.  However, I might have gotten a tad bit too ambitious and need help.

Below is my long problem statement: Please ignore the details regarding some ultra-popular Bollywood song artists and only a fraction of their most popular song titles.  You can replace the artists' names and their Bollywood song titles with western artists' names and their respective song titles if the current names and titles are too distracting.

I would appreciate it if you could simply focus on the nature of the data to be used and the multi-part problem.  Those who might be willing to help, it would be easier if you copy and paste the entire code below into a Python 3 text editor such as Sublime Text (which I am using).

Also, I have added my personal comments on problems which I have attempted so far and either solved completely or have come dangerously close to solving.  I am not looking for a spoon-fed solution to every single unsolved problem.  Just something that would push me over to the next step would suffice.  I know conceptually how I would solve each of these problems anyone can solve them in their mind.  What I'm struggling the most with is the syntax and order of certain functions when solving.  Perhaps I am not considering some intermediate code steps that are required and/or helpful.

Please note that the primary objective is to solve these problems using lambdas, filters, maps, etc. and NOT for or while loops or list comprehensions.

Any help will be greatly appreciated with heartfelt thanks and email fist-bumps.  Thank you for your time and consideration.

Regards,
Rahul A.


So here it goes......................... 

# This function accepts a list of several dictionaries that contain song artists' names and their song titles and 
# returns the following outputs in tuples (remember to return a tuple for each output!):
# 1. The highest number of songs sung by any artist
# 2. The name of the artist with the highest number of songs
# 3. A tuple containing the name of the artist and their number of songs sung
# 4. The longest song title in the entire song list
# 5. The name of the artist with the longest song title
# 6. A tuple containing the name of the artist w/ longest song title and their longest song title
# 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title
# 8. A list of artists and their number of songs sung sorted (default ascending order) by the number of song titles
# 9. A list of artists and their longest song titles sorted in descending order of the length of the song title
# 9a. Similar list as original but longest title listed first and shortest title listed last for each artist
# 9b. BONUS: same as 9a above with artists sorted in default ascending order.  Thus the artists will be listed
# in regular alphabetical order, but their song titles will listed in the descending order of title length

songmix = [
	{"artist":"Rafi","titles":["Pukarta chala hoon main","Aapke haseen rukh pe","Thaheriye hosh main aaloon","Woh jab yaad aaye","Deewana hua badal","Ehsan tera hoga mujhpar"]},
	{"artist":"Asha","titles":["Aja aja main hun pyar tera","Dil cheez kya hai","Aaiye meherban","Aao huzur tum ko","In aankhon ki masti mein","Paan khaye saiyan humaro"]},
	{"artist":"Suman","titles":["Rahete kabhi jinke dil mein","Na tum hamen jano","Jo hum pe guzarti hai","Rahe na rahe hum","Aajkal tere mere pyar ke","Tujhe dekha tujhe chaha","Parbaton ke pedon par","Tumne pukara aur"]},
	{"artist":"Kishor","titles":["Beqarar dil","Nile nile ambar pe","Muqaddar ka sikandar","Mere mehboob kayamat hogi","Mere sapno ki rani kab","Pyar diwana hota hai","O mere dil ke chain","Yeh shaam mastani","Pal pal dil ke paas"]},
	{"artist":"Lata","titles":["Lag ja gale","Tera jana dil ke armanon ka","Tera mera pyar amar","Yoon hasraton ke daag","Awaz deke hamen tum bulao","Mujhe kitana pyar hai tumse","Mausam hai aashiqana","Tujhe dekha to jana sanam","Salam-e ishq meri jaan","Yeh dil aur unki"]},
	{"artist":"Hemant","titles":["Tumhe yaad hoga","Tum pukar lo","Jane wow kaise log the","Neend na mujhko aye","Beqarar karke humein",]},
	{"artist":"Talat","titles":["Jalte hain jisake liye","Jayen to jayen kahan"]},
	{"artist":"Manna","titles":["Zindagi kaisi hai paheli haye","Poochho na kaise maine rain bitayee","Laga chunari mein daag"]},
	{"artist":"Alka","titles":["Akele hain to kya gam hai","Jane kyun log pyar karte hain","Kuchh kuchh hota hai","Pardesi pardesi jana nahi"]}
]

# 1. The highest number of songs sung by any artist
# def most_titles (songmix):
# 	return len(max(songmix, key=lambda num: len(num['titles']))['titles']) # 1. Most titles
# print (f"Max song count for any artist", most_titles (songmix)) # !!! # Gives CORRECT answer (10)

# 2. The name of the artist with the highest number of songs
# def artist_most_titles (songmix):
# 	return max(songmix, key=lambda num: len(num['titles']))['artist'] # 2. Artist w/ most titles
# print (f"The artist with max song count is", artist_most_titles (songmix)) # Gives CORRECT answer (Lata)

# 3. A tuple containing the name of the artist w/ most titles and their number of songs sung
# def artist_most_titles (songmix):
# 	artist_titles = (max(songmix, key=lambda num: len(num['titles']))['artist'], len(max(songmix, key=lambda num: len(num['titles']))['titles']))
# 	return artist_titles
# print (artist_most_titles (songmix)) # Gives CORRECT pair as tuple ('Lata', 10)
# print (f"Artist, {artist_titles[0]}, has the highest song titles at, {artist_titles[1]}") # ERROR: Does NOT recognize artist_titles!

# 4. The longest song title in the entire songs list
# Sincen now we have to select the actual longest title, we likely need to use 'filter', 'map', and 'max' function all in one!
def longest_title (songmix):
	temp = max(songmix, key=lambda num: len(max(num['titles'])))
	return list(map(lambda x: max(songmix, key=lambda num: len(max(num['titles'])))['artist'],
		filter(lambda title: max(len('title')), temp)))
# DON'T MESS WITH # return max(songmix, key=lambda num: len(max(num['titles']))) # 4. Longest title
print (longest_title (songmix)) # !!! Returns the entire record {...} for the correct artist ('Manna') (but NOT actual song title)

# 5. The name of the artist with the longest song title
# def artist_longest_title (songmix):
# 	return max(songmix, key=lambda num: len(max(num['titles'])))['artist'] # 5. Artist w/ longest title
# print (f"The artist with longest song title is", artist_longest_title (songmix)) # !!! Gives CORRECT answer (Manna)

# 6. A tuple containing the name of the artist w/ longest song title and their longest song title

# 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title

# 8. A list of artists and their number of songs sung sorted (default ascending order) by the number of song titles

# 9. A list of artists and their longest song titles sorted in descending order of the length of the song title
# 9a. Similar list as original but longest title listed first and shortest title listed last for each artist
# 9b. Bonus: same as 9a above with artists sorted in default ascending order.  Thus the artists will be listed
# in regular alphabetical order, but their song titles will listed in the descending order of title length


More information about the Tutor mailing list