My Take on Yandex Pre-interview Python Assignment

I’ve applied for a junior Python position at Russian internet giant Yandex (very similar to Google). And although my application has been rejected, due to lack of experience, I think their little pre-interview test and my take on that may be of interest to any inquisitive pythonista. Note, that this has never been properly translated into English before, so this is probably exclusive in that regard.

Assignment I

There are two lists of different length. The first one contains keys, the second – values. Write a function, that would create a dict out of these lists. If the key doesn’t have a value – it should equal None, if the value doesn’t have a key, it should be omitted.

def get_dict(list1, list2):
    ret = dict(map(None, list1, list2))
    if ret.get(None, False):
    return ret

Assignment II

Login should start with latin symbol, contain latin symbols, digits, dots and hyphens, but end only with a latin symbol or a digit. Minimum length is 1 symbol, maximum – 20 symbols. Write a function that checks strings for correspondence with these rules. Think of several methods of solving this problem and compare them.

import re
import time

def check1(login):
    ret = False
    if re.match('^[a-zA-Z][a-zA-Z0-9\-\.]{0,19}(?<![\-\.])$', login):
        ret = True
    return ret

def check2(login):
    ret = False
    if (len(login) >= 1 or len(login) <= 20) and login[0].isalpha() and (login[-1].isalpha() or login[-1].isdigit()):
        for a in login[1:-1]:
            if a.isalpha() or a.isdigit() or a == '-' or a == '.':
                ret = True
    return ret

def compare(login):
    tm = time.time()
    print(time.time() - tm)
    tm = time.time()
    print(time.time() - tm)

Assignment III

There are two tables users and messages (I changed names and messages to non-Cyrillic):

UID name
1 John Doe
2 Natalie Knaph
3 Johnatan Yozo
UID msg
1 Hello, John!
3 Send me the card, quickly.
3 I’m waiting on the corner of 5th and Lafayette
1 This is me again. Please message me more often.

Create a SQL query, that would return two fields: “User name” and “Total amount of messages”.

SELECT AS "User name",count(*) AS "Total amount of messages" 
FROM users 
JOIN messages ON users.uid = messages.uid 
GROUP BY users.uid

Assignment IV

Suppose you have a generic access.log. How to get 10 most frequent IP-addresses using standard terminal tools? How to do that with Python?

grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' access.log | sort -n | uniq -c | sort -n -r | head -10

import sys
import re

all = re.findall("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", open(sys.argv[1], 'r').read())
srt = sorted(all, key=all.count, reverse=True)
unq = []
for m in srt:
    if not m in unq:
print unq[0:10]

If you can think of a better way to solve any of these, let me know.


16 Replies to “My Take on Yandex Pre-interview Python Assignment”

  1. You could use zip() for problem 1:

    >>> test1 = [‘key1’, ‘key2’, ‘key3’]
    >>> test2 = [1, None, 3]
    >>> dict(zip(test1, test2))
    {‘key3’: 3, ‘key2’: None, ‘key1’: 1}

  2. Assignment I:
    pythonic way:
    def get_dict(k_list, v_list):
    return {key:v_list[i] if i<len(v_list) else None for i,key in enumerate(k_list)}
    with zip:
    def get_dict2(k_list, v_list):
    d = dict.fromkeys(k_list)
    d.update((dict(zip(k_list, v_list))))
    return d

  3. Assignment I Incorrect, in python 3 all strings is unicode: str.isalpha() return True also with cyrillic symbols, example: “ЫЮЯ”.isalpha() == True !!!
    my version:
    def check2(login):
    from string import ascii_letters, digits
    test_char = lambda c, last=”: c in ascii_letters+digits+last
    return 1<=len(login)<=20 and test_char(login[0],"-.") and test_char(login[-1])

  4. You can’t query column, because you’re not using it in group by clause.
    Bettew solution is
    SELECT AS “User name”
    FROM (SELECT messages.uid
    ,COUNT(*) AS message_total
    FROM messages
    GROUP BY messages.uid) mc
    JOIN users
    ON users.uid = mc.uid

  5. Assignment I
    # need provide short keys (see ‘If the key doesn’t have a value – it should equal None’)
    k = [str(i) for i in range(3)]
    v = [i for i in range(2)]

    def create_dict(keys, values):
    values += [None]*(len(keys)-len(values))
    return dict(zip(keys, values))

    print k
    print v
    print create_dict(k, v)

    Assignment II
    import string

    login = ‘asdfasga234wegaweg’
    # don’t like re 😉
    correct_chars = string.ascii_letters + string.digits + ‘.-‘
    redundant_chars = set(login) – set(correct_chars)

    start_login = lambda c: c.isalpha()
    end_login = lambda c: c.isalpha() or c.isdigit()
    lenght_login = lambda s: True if 1<len(s)<=20 else False

    correct_login = lambda s: not redundant_chars and start_login(login[0]) and end_login(login[-1]) and lenght_login(login)
    print correct_login(login)

    use for training

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s