How is this evaluated

A

Arturo B

I'm making this exercise: (Python 3.3)

Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant andplace an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

So I tried to solved it, but I couldn't, so I found many answers, but I selected this:

def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
return ''.join(l + 'o' + l if l in consonants else l for l in s)

print(translate('hello code solver'))


OUTPUT:
'hohelollolo cocodode sosololvoveror'

______________________________________________________________
So I want to question:
How is the

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :p )

''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)

Thank you guys
 
A

Antoon Pardon

Op 04-07-13 19:20, Arturo B schreef:
I'm making this exercise: (Python 3.3)

Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

So I tried to solved it, but I couldn't, so I found many answers, but I selected this:

def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
return ''.join(l + 'o' + l if l in consonants else l for l in s)

print(translate('hello code solver'))


OUTPUT:
'hohelollolo cocodode sosololvoveror'

______________________________________________________________
So I want to question:
How is the

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :p )

''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)

Thank you guys

This doesn't make much sence because you substituted a varible for
a character literal, so I'll go with the original.



l + 'o' + l if l in consonants else l for l in s

Evaluate this expression:

l + 'o' + l if l in consonants else l

for each l in s (so for each letter that in s).



l + 'o' + l if l in consonants else l

if l is part of the consonants produce l + 'o' + l
otherwise just produce l.


Hope this helps.
 
N

newspost2012

Am Donnerstag, 4. Juli 2013 19:20:43 UTC+2 schrieb Arturo B:
...
So I want to question:
How is the

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :p )

Although new to python I think I can solve this (if no one contradicts, I can guess that I understood it :) ):

Take every letter from the string, one after the other, (for l in s).
If you can find the letter in the string constants (if l in constants) then take it, append an "o" and then the letter again and return it (l + 'o' + l) if not, return the letter as it is (else l).

Or in applicable order:
take 'h' append 'o' append 'h' and return this, if you can find 'h' in constants, if not return just 'h' (and do this for every letter in string s)

hth,
Martin
 
J

Jussi Piitulainen

Arturo said:
I'm making this exercise: (Python 3.3)

Write a function translate() that will translate a text into
"rövarspråket" (Swedish for "robber's language"). That is, double
every consonant and place an occurrence of "o" in between. For
example, translate("this is fun") should return the string
"tothohisos isos fofunon".

So I tried to solved it, but I couldn't, so I found many answers,
but I selected this:

def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
return ''.join(l + 'o' + l if l in consonants else l for l in s)

print(translate('hello code solver'))


OUTPUT:
'hohelollolo cocodode sosololvoveror'

______________________________________________________________
So I want to question:
How is the

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :p )

That's nonsense in two different ways. The actual expression in the
solution you found makes sense. It's a generator expression of this
form:

expression for l in s

And the expression in it is this conditional expression:

l + 'o' + l if l in consonants else l

The value of this conditional expression is (l + 'o' + l) if l is in
consontants, for example 'tot' if l == 't'; else it is just l, for
example 'i' if l == 'i'.

Fully parenthesised the whole expression is this:

((l + 'o' + l) if (l in consonants) else l) for l in s

The value of this expression yields the values like 'tot' and 'hoh'
and 'i' for each letter l in s in turn.
''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)

This is still nonsense. To make sense, replace each 'h' with h.

Then ''.join can eat up the values of a generator expression to
produce the string like 'tothohi...'.

You can experiment with the components of this complicated expression:

I suppose you understand this:

Note that a similar-looking expression in brackets [] is a list
comprehension; in braces {} it is a set comprehension, or a dictionary
comprehension if the value expression is a key : value pair, with the
colon. And this is not all: there's nesting and filtering, too. The
different uses of the keywords 'for', 'in', 'if', 'else' are a bit
subtle but one sort of learns to see the whole expression.

I tend to use line breaks and parentheses in such expressions:

''.join(c + 'o' + c if c in consonants else c
for c in message)

''.join((c + 'o' + c
if c in consonants
else c)
for c in message)
 
S

Steven D'Aprano

I'm making this exercise: (Python 3.3)

Write a function translate() that will translate a text into
"rövarspråket" (Swedish for "robber's language"). That is, double every
consonant and place an occurrence of "o" in between. For example,
translate("this is fun") should return the string "tothohisos isos
fofunon".

So I tried to solved it, but I couldn't, so I found many answers, but I
selected this:

def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
return ''.join(l + 'o' + l if l in consonants else l for l in s)

The part inside the join(...) is called a generator expression.

I do not recommend generator expressions (or list comprehensions) for
beginners, because they can be confusing until you understand how they
work. As a beginner, if you have a choice between a one-line piece of
code that makes no sense to you, and a five-line piece of code that you
understand, you should choose the one that you understand.

So I want to question:
How is the

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :p )

That is the wrong question, because you have split the code in the wrong
places. Your generator expression looks like this:

l + 'o' + l if l in consonants else l for l in s

This is a "generator expression" of the form:

(x for l in s)

where x will be defined below. This part is easy enough to understand:
Python will iterate over the string s, extracting one letter at a time l,
and then evaluate the expression "x" below.

What is the expression "x"? It is this part:

(l + 'o' + l if l in consonants else l)

which forms the "ternary operator" if-clause:

value-if-true if condition-being-tested else value-if-false

If you know C, that's like:

?(condition-being-tested, value-if-true, value-if-false)

The condition being tested is, "l in consonants". The value if true is "l
+ 'o' + l". And the value if false is just l.

So putting this all together, we can convert the generator expression
version to this longer, but more readable, version:

def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
collector = []
for l in s:
if l in consonants:
collector.append(l + 'o' + l)
else:
collector.append(l)
return ''.join(collector)
 
C

Chris Angelico

If you know C, that's like:

?(condition-being-tested, value-if-true, value-if-false)

Or to be precise:

condition-being-tested ? value-if-true : value-if-false

ChrisA
 
S

Steven D'Aprano

Or to be precise:

condition-being-tested ? value-if-true : value-if-false


Oops. Sorry about that. I thought it looked wrong even as I was typing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top