Skip to content

Commit 9ebe345

Browse files
committed
Day 7: check a string's every even letter is uppercase, and every odd letter is lowercase.
1 parent 8a4c5f4 commit 9ebe345

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Define a function called myfunc that takes in a string,
3+
and returns a matching string where every even letter is uppercase,
4+
and every odd letter is lowercase.
5+
6+
Desired output:
7+
myfunc('aNiMaL')-->True
8+
"""
9+
10+
11+
def matching_string(input_string):
12+
even = ''
13+
odd = ''
14+
15+
for i in range(len(input_string)):
16+
if i % 2:
17+
even += (input_string[i])
18+
else:
19+
odd += (input_string[i])
20+
21+
if odd.islower() and even.isupper():
22+
return True
23+
return False
24+
25+
26+
print(matching_string('aNiMaL'))
27+
print(matching_string('animal'))

daily_challenges/day_7/2_improved.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def improved_matching_string(input_string):
2+
odd = input_string[::2]
3+
even = input_string[1::2]
4+
5+
if odd.islower() and even.isupper():
6+
return True
7+
return False
8+
9+
10+
print(improved_matching_string('aNiMaL'))
11+
print(improved_matching_string('animal'))
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# add two truth values it will give you 2
2+
_str = 'aNiMaLa'
3+
print((_str[0::2].islower() + _str[1::2].isupper()) == 2)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
4+
def matching_str():
5+
word = 'aNiMaL'
6+
func = lambda x: (x[0] % 2 == 0 and x[1].islower()) or (x[0] % 2 != 0 and x[1].isupper())
7+
return len(word) == len(list(filter(func, list(enumerate(word)))))
8+
9+
10+
# using regex
11+
def myfunc(s):
12+
return False if (len(s) == 0) else bool(re.fullmatch(r'([a-z][A-Z])*[a-z]?', s))

daily_challenges/day_7/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)