A little test for you Guys😜

Sven R. Kunze srkunze at mail.de
Tue Sep 22 15:42:41 EDT 2015


Hmm, why not. :D

On 22.09.2015 20:43, Python_Teacher via Python-list wrote:
> you have 10 minutes😂 Good luck!!
>
>
> 1. What is PEP8 ?

A PEP.

> 2. What are the different ways to distribute some python source code ?

unison, rsync, scp, ftp, sftp, samba, http, https, mail, git, ....

> 2 Lists
>
> Let's define the function plural :
>
> def plural(words):
>      plurals = []
>      for word in words:
>         plurals.append(word + 's')
>      return plurals
>
> for word in plural(['cabagge','owl','toy']):
>      print word
>
> Question : How could the code of the function plural be optimised?

Don't optimized until you need to. So, we leave it as is. ;)

> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
>      'foo1': 'bar1',
>      'chose': 'truc',
>      'foo2': 'bar2',
> }
> output = {
>      'bar1': 'foo1',
>      'truc': 'chose',
>      'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?

# :-P
def function(input):
     return output

> 4 Iterators
>
> Let's consider this program :
>
> def program_1():
>      yield 1
>      yield 2
>      yield 3
>
> g = program_1()
> a = list(g)
> b = list(g)
> c = g()
>
> Question : At the end of the program,
>
> 1. What is the type of g ?
> 2. What is the value of a ?
> 3. What is the value of b ?
> 4. What is the value of c ?

The program ends with a traceback. So, my variables are all gone. :(

> 5 Decorators
>
> Let's consider now :
>
> def str2print(f):
>      def str2print_wrap(*args, **kwargs):
>          """wrapper"""
>          s = f(*args, **kwargs)
>          print s
>     return str2print_wrap
>
> def hello(s):
>      """ Return "Hello $s" """
>      return "%s %s" % ("Hello", s)
>
> Questions :
>
> 1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.

@str2print
def hello(s):
     """ Return "Hello $s" but actually returns None """
     return "%s %s" % ("Hello", s)


> 2. What is the effect of the decorator on a call to the new method 'hello' ?
It prints "Hello {s}" and returns None.
> 3. What is the return value of hello.__doc__
'wrapper'

Best,
Sven



More information about the Python-list mailing list