-
"Elementary" One-liners Overview
As you may know we have a special island for our coding newbies -- "Elementary". On this island we've created very simple missions which include hints to help solve them. So if you just have finished the TreeHouse or CodeAcademy courses, or you already know some python syntax, then this island is absolutely for you.
But our more advanced players are having fun there too. For example, they are adding an extra challenge and writing 'one-liners'. A one-liner is a program which takes just one line of code. For this weekly overview we will look at some of the interesting one-liners from our players.
Fizz-Buzz
I think you all know about "Fizz Buzz" game by now, it's often used to teach children division.
And we're opening today's review with @nickie's "String arithmetic" solution.
He is using [:-1]
to cut some final whitespaces and if it's an empty string, converts the number.
checkio=lambda n:("Fizz "*(1-n%3)+"Buzz "*(1-n%5))[:-1]or str(n)
Index Power
Find the N-th power of the element in the array with the index N.
@DiZ has written a short solution named "45". I think it's not a mystery to get the meaning of this solution's title.
index_power=lambda a,n:-(len(a)<=n)or a[n]**n
Even The Last
Take the sum of the elements with even indexes, then multiply this summed number and the final element of the array.
Here I took a look at @aggelgian's solution. One small remark: "Clear" may not be the best category for this solution, but it sure is short and simple.
checkio=lambda x: sum(x[::2])*x[-1] if x else 0
Monkey Typing
You are given text and a set of words. Find how many of the listed words are in the given text.
And @suic's "Another one-liner" solution certainly has a functional style.
count_words = lambda t, w: len(list(filter(t.lower().count, w)))
Secret Message
Gather all of the capital letters in one word together in the order that they appear in the text.
I think in this case @veky's "filter" solution is deffinitely placed in the "Clear" section correctly. It's a simple and functional chunk of code in the PEP8 style.
find_message = lambda text: ''.join(filter(str.isupper, text))
Three Words
Check if the string contains three words (words contains only letters) in succession.
For this mission I will take @Tubis's solution. Yes, I know that's a fake account, and I know who is it. :-)
checkio=lambda x:"www" in "".join('w' if w.isalpha() else 'd' for w in x.split())
The Most Numbers
Find the difference between the maximum and minimum element from a number array.
Honestly for this mission we have many identical one-liners, so I've taken @Uladzimir's solution from the top of the list.
checkio = lambda *args: max(args) - min(args) if args else 0
Boolean Algebra
In this mission you should implement some boolean operations: conjunction, disjunction, implication, exclusive and equivalence.
This is not the shortest, but it's short enough "64 chars" by @Sim0000.
boolean=lambda x,y,o:(x&y,x|y,x<=y,x^y,x==y)['oimxq'.find(o[1])]
Right to Left
Just replace all the "right" words with the "left" and join them with commas.
For this we will look at harold_666's solution. The "join" is funny here.
left_join=lambda phrases: ''.join(list(''.join([i+',' for i in phrases]))[0:-1]).replace('right','left')
Digits Multiplication
You are given a positive integer. Your function should calculate the product of the digits excluding all the zeros.
Amachua used "eval" for this mission in his solution.
checkio = lambda n: eval("*".join(i for i in str(n) if i != '0'))
To be continued...
This is only first half. In the next article we will take look at the second set of missions I dug up from the Elementary Island.
Welcome to CheckiO - games for coders where you can improve your codings skills.
The main idea behind these games is to give you the opportunity to learn by exchanging experience with the rest of the community. Every day we are trying to find interesting solutions for you to help you become a better coder.
Join the Game