Jeri Lim
Jeri Lim

Reputation: 55

Python sequences (tuples)

Write a function square_odd_terms that accepts a tuple as an argument and returns a tuple with the odd terms in the tuple squared. Even terms will remain the same.

My attempt was:

def square_odd_termms(tpl):
    for i in (tpl):
        if i%2==0:
            return i
        else:
            return i**2
    return tuple(i)

Any help?

Attempt 2:

def square_odd_terms(tp1):
    for num in tp1:
        if num%2 != 0:
            return (num**2)
        else:
            return (num)
    return tuple(num)

Upvotes: 2

Views: 1860

Answers (4)

pylang
pylang

Reputation: 44545

You are close. However, your return statements in your first attempt will short-circuit the function. A simple way to do this would be to iterate any iterable (tuple, list, etc.) and add new items to a new tuple.

Here is an example of tuple concatenation:

def square_odd_terms(iterable):
    result = tuple()
    for i in iterable:
        if i % 2 != 0:
            result += (i**2,)
        else:
            result += (i,)
    return result


numbers = (3, 6, 9, 10, 12)
square_odd_terms(numbers)
# (9, 6, 81, 10, 12)

The next level might be a generator that yields each element and is transformed to a tuple:

def square_odd_terms(iterable):
    """Yield squared odds and unchanged evens."""
    for i in iterable:
        if i % 2 != 0:
            yield i**2
        else:
            yield i


tuple(square_odd_terms(numbers))
# (9, 6, 81, 10, 12)

Next, you can mimic the latter with a generator expression cast to tuple():

def square_odd_terms(iterable):
    """Return a tuple of squared odds and unchanged evens."""
    return tuple(i**2 if i % 2!=0 else i for i in iterable)


square_odd_terms(numbers)
# (9, 6, 81, 10, 12)

Upvotes: 1

Abr001am
Abr001am

Reputation: 591

f=lambda x: map(lambda i: i**2 if i%2 else i,x)

Output:

>>> t=(1,2,5,4)
>>> f=lambda x: tuple(map(lambda i: i**2 if i%2 else i,x))
>>> f(t)
(1, 2, 25, 4)
>>>

Upvotes: 0

Raju Pitta
Raju Pitta

Reputation: 606

Using generator comprehension if the number is odd square the number else just keep the number as is.

def square_odd_termms(tpl):
    return tuple((num**2 if num%2 else num for num in tpl))

>>> square_odd_termms((1,2,3,4,5,6))
(1, 2, 9, 4, 25, 6)

Answer for your attempt 2:You should not return the values in your "if" and "else" conditions which stops the function flow right there. Instead declare an empty array, assign these values for that array and return at the end

def square_odd_terms(tp1):
    result = []
    for num in tp1:
        if num%2 != 0:
            result.append(num**2)
        else:
            result.append(num)
    return tuple(result)

Upvotes: 4

rajeshv90
rajeshv90

Reputation: 584

Modifying your attempt 2:

def S_O(a):

l = []
for i in a:
    if i%2 != 0:
        l.append(i**2)
    else:
        l.append(i)
return tuple(l)

returns tuple

Execute your program twice. You will know what is wrong with default args.

>>> S_O((1,2,3))
(1, 2, 9)
>>> S_O((1,2,3,4))
(1, 2, 9, 1, 2, 9, 4)

Upvotes: -1

Related Questions