python – oddTuples

#Python Function which returns odd Tuples

”’

Write a procedure called oddTuples, which takes a tuple as input,

and returns a new tuple as output, where every other element of the input tuple is copied,

starting with the first one. So if test is the tuple (‘I’, ‘am’, ‘a’, ‘test’, ‘tuple’),

then evaluating oddTuples on this input would return the tuple (‘I’, ‘a’, ‘tuple’).

”’

def oddTuples(aTup):

”’

aTup: a tuple

returns: tuple, every other element of aTup.

”’

# Your Code Here

num=1

t1=()

for i in aTup:

if num%2 == 1:

t1= t1+(i,)

num+=1

else:

num+=1

return t1

print oddTuples((1, ‘two’, 3, ‘four’, 5))

Advertisements