Python2017/labs02/task10.py

77 lines
1.8 KiB
Python
Raw Normal View History

2017-11-18 16:45:28 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Napisz funkcję pokemon_speak, która zamienia w podanym napisie co drugą literę
na wielką. Np. pokemon_speak('pokemon') powinno zwrócić 'PoKeMoN'.
"""
2017-11-27 21:23:07 +01:00
#tekst=("edyta")
#def pokemon_speak(tekst):
#for litera in tekst:
#if litera in tekst[::2]:
#return litera.upper()
2017-11-18 16:45:28 +01:00
2017-11-27 21:23:07 +01:00
#def pokemon_speak(text):
2017-11-18 16:45:28 +01:00
2017-11-27 21:23:07 +01:00
#return [text.upper() for x in text[::2]]
#def pokemon_speak(text):
#return [x.upper() for x in text[::2]]
#if litera in text[::2]:
#return text[::2].upper()
#else:
#return text[::1].lower()
#pokemon_speak("edytarenkjacek")
2017-11-18 16:45:28 +01:00
def pokemon_speak(text):
2017-11-27 21:23:07 +01:00
indices=set([0,2,4,6,8,10,12,14,16,18])
#indices=set(index(text[::2]))
return("".join(c.upper() if i in indices else c for i, c in enumerate(text)))
2017-11-18 16:45:28 +01:00
2017-11-27 21:23:07 +01:00
# def fold(s):
# uppers = s[0::2].upper()
# lowers = s[1::2].lower()
# return zip(uppers, lowers)
2017-11-18 16:45:28 +01:00
2017-11-27 21:23:07 +01:00
# def fold(s):
# time_to_upper = True
# result = ""
# for ch in s:
# if time_to_upper:
# result += ch.upper()
# else:
# result += ch.lower()
# time_to_upper = not time_to_upper
# return result
#
# def fold(s):
# time_to_upper = True
# result = ""
# for ch in s:
# if time_to_upper:
# result += ch.upper()
# else:
# result += ch.lower()
# time_to_upper = not time_to_upper
# return result
#
# s="edyta"
#indices=set([text[::2]])
2017-11-18 16:45:28 +01:00
def tests(f):
2017-11-27 21:23:07 +01:00
inputs = [['pokemon'], ['do not want'], ['POKEMON']]
2017-11-18 16:45:28 +01:00
outputs = ['PoKeMoN', 'Do nOt wAnT', 'POKEMON']
for input, output in zip(inputs, outputs):
if f(*input) != output:
return "ERROR: {}!={}".format(f(*input), output)
break
return "TESTS PASSED"
if __name__ == "__main__":
print(tests(pokemon_speak))