In normal circumstances, the following 4 questions are pretty manageable and doable I guess. But their difficulty steps up when we are required to write them using ONE line of code. I'm guessing not many of you will be able to solve all 4 questions using ONE line of code, but I want you to prove me wrong.
Disclaimer — I've personally solved all 4 questions in one line, and they are doable. It's just not easy.
1) Sorting Fruits By Count
fruits = ["apple", "apple", "orange", "pear", "apple", "orange"]You are given a list of fruits, and some fruits appear multiple times. Write a function sort_count(fruits) that takes in this list, counts the number of times each fruit appears, and sorts them by this condition.
sort_count(fruits) # ["pear", "orange" ,"apple"]Here, pear appears once, orange appears twice and apple appears thrice, and that is why we get ["pear", "orange", "apple"]. Write this function using ONE line of code.
2) Replacing None Values With Average
lis = [1, 2, None, 3, 5, None, 4]You are given a list of numbers with some None values. Write a function replace_none(lis) that takes in this list, and returns a new version of this list where None are replaced with the average of the other numbers.
replace_none(lis) # [1, 2, 3, 3, 5, 3, 4]Here, the average of the numbers is 3 as (1+2+3+5+4)/5 == 3. We then replace all None values with 3. Write this function using ONE line of code.
3) Counting Holes On A Bridge
bridge = [1,0,1,1,0,0,0,1,0,0,1,1]You are given a list containing only 1's and 0's. This list represents a bridge, 1's represent wood, and 0's represent holes. 2 holes (0's) that are right next to each other make up a larger hole.
Write a function count_holes(bridge) that counts the number of holes (regardless of how big or small the hole it) on the bridge.
count_holes(bridge) # 3In this bridge, there are 3 holes — remember that multiple 0's directly next to one another make up a hole.
4) Extracting Keys From Messy Nested Dictionary
data = {
"type": "video",
"videoID": "vid001",
"links": [
{"type":"video", "videoID":"vid002", "links":[]},
{ "type":"video",
"videoID":"vid003",
"links": [
{"type": "video", "videoID":"vid004"},
{"type": "video", "videoID":"vid005"},
]
},
{"type":"video", "videoID":"vid006"},
{ "type":"video",
"videoID":"vid007",
"links": [
{"type":"video", "videoID":"vid008", "links": [
{ "type":"video",
"videoID":"vid009",
"links": [{"type":"video", "videoID":"vid010"}]
}
]}
]},
]
}You are given a messy dictionary with multiple levels of nesting (dictionaries & lists). Write a function extract(data, key) that takes in 1) this messy nested dictionary and 2) a string key, and returns a list containing all values with the key.
extract(data, "videoID")
# ['vid001', 'vid002', 'vid003', 'vid004', 'vid005', 'vid006', 'vid007', 'vid008', 'vid009', 'vid010']Remember to write this function using ONE line of code! HINT — recursion is needed to write this function in one line.
Conclusion
Did you manage to solve them all and prove me wrong?
Some Final words
If this article provided value and you wish to support me, do consider signing up for a Medium membership — It's $5 a month, and you get unlimited access to articles on Medium. If you sign up using my link below, I'll earn a tiny commission at zero additional cost to you.
Sign up using my link here to read unlimited Medium articles.
Get my free Ebooks: https://zlliu.co/books
I write programming articles (mainly Python) that would have probably helped the younger me a lot. Do join my email list to get notified whenever I publish.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.