-
Notifications
You must be signed in to change notification settings - Fork 6
/
WeIrD StRiNg CaSe.py
25 lines (21 loc) · 972 Bytes
/
WeIrD StRiNg CaSe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'''Write a function toWeirdCase (weirdcase in Ruby) that accepts a string,
and returns the same string with all even indexed characters in each word upper cased,
and all odd indexed characters in each word lower cased.
The indexing just explained is zero based, so the zero-ith index is even,
therefore that character should be upper cased.
The passed in string will only consist of alphabetical characters and spaces(' ').
Spaces will only be present if there are multiple words. Words will be separated by a single space(' ').'''
def to_weird_case(s):
s = list(s)
s_even = list(letter.upper() for letter in s[::2])
s[::2] = s_even
weird = ''
for char in s:
weird += char
return weird
'''
also we could do this:
def to_weird_case_word(string):
return "".join(c.upper() if i%2 == 0 else c for i, c in enumerate(string.lower()))
def to_weird_case(string):
return " ".join(to_weird_case_word(str) for str in string.split())'''