• "Elementary" One-liners Overview (Part II)

Hi!

In the last Weekly Overview we looked at first ten missions from the "Elementary" island.
Today we've got the second part of our Weekly Review for your reading pleasure.

Count Inversions

In this mission you need count the number of inversions in a sequence of numbers.

And we're opening up with @veky's solution "Gallery".
Here we see the interesting usage of a double loop in comprehension.

count_inversion = lambda s: sum(a > b for i, b in enumerate(s) for a in s[:i])

The end of other

In this task, you are given a set of words in lower case.
We must check whether there is a pair of words, such that one word is the end of another (a suffix of another).

Here's a simple and clear one-liner from @Apua "just one liner".

checkio=lambda S:any(a!=b and a.endswith(b) for a in S for b in S)

Days Between

How to find the difference in days between the given dates.

"oneliner" by @DiZ with the "import" variation which is designed specifically to write one-liners ;-)

days_diff=lambda f,t,d=__import__('datetime').date:abs(d(*f)-d(*t)).days

Pangram

Check if a sentence is a pangram or not.

And here's the typical type of "import" in @ciel's solution.

import string;check_pangram=lambda t:string.ascii_uppercase in str().join(sorted(list(set(t.upper()))))

Binary count

Convert a number to the binary format and count how many unities (1) are in the number spelling.

Everything is short, simple and clear in "lambda" solution by @mastak.

checkio = lambda n: bin(n).count('1')

Number Base

You are given a positive number as a string along with the radix for it.
Your function should convert it into decimal form.

Yes, it's really obviously for Python. So let's look at the short solution by @shiracamus

checkio=lambda s,r:int(('-1',s)[int(max(s),36)<r],r)

Common Words

You are given two strings with words separated by commas. Try to find what is common between these strings.

Yep, @somnambulism didn't use sets in his "oneliner".

checkio = lambda a, b: ','.join([x for x in sorted(a.split(',')) if x in b.split(',')])

Absolute sorting

An array (a tuple) has various numbers. You should sort it, but sort it by absolute value in ascending order.

And again we have an "Obvious" solution by @nickie.

checkio=lambda a:sorted(a,key=abs)

Building Base

This is not a base mission, players should write a class with the given requirement.

Here I've not found a formal one-liner, but I think this solition with the simple title "zzdgnczfgdmksjdgfjs"
by @samulih can be counted as one-liner.

class Building:
    __repr__ = lambda s: '%s%s' % (s.__class__.__name__, s.d)
    def __init__(s, *args, methods=('area', 'volume', 'corners')):
        s.d, ns, we, r = (args + (10,))[:5], ('sou', 'nor'), ('we', 'ea'), (0, 1)
        s.__dict__.update({k: lambda n=n: ({'%sth-%sst' % (ns[i], we[j]):
        [s.d[0]+s.d[3]*i, s.d[1]+s.d[2]*j] for i in r for j in r}) if n>1 else
        s.d[2]*s.d[3]*s.d[4]**(n&1) for n, k in enumerate(methods)})

Friends

And again a mission where you need to write a class.

Yes, this is also a not strictly a one-liner, but a set of one-liners in "lambda" solution by @jcg.

class Friends(set):
    __init__ = lambda self, connections: self.update(map(frozenset, connections))
    add = lambda self, connection: not(connection in self or super().add(frozenset(connection)))
    remove = lambda self, connection: connection in self and not super().discard(frozenset(connection))
    names = lambda self: set.union(*map(set,self))
    connected = lambda self, name: set().union(*filter(lambda x:name in x, self))-{name}
‚Äč

What next?

We finished our Elementary island with 20 missions in 29 strings (last two mission broke it).
If you have ideas for the next week solution overview -- feel free to let us know.
That's all for today, folks. Bye!

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