File tree 5 files changed +53
-0
lines changed
5 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
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' ))
Original file line number Diff line number Diff line change
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' ))
Original file line number Diff line number Diff line change
1
+ # add two truth values it will give you 2
2
+ _str = 'aNiMaLa'
3
+ print ((_str [0 ::2 ].islower () + _str [1 ::2 ].isupper ()) == 2 )
Original file line number Diff line number Diff line change
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 ))
You can’t perform that action at this time.
0 commit comments