-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternate even odd.py
61 lines (48 loc) · 1.62 KB
/
alternate even odd.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Given a string containing at least one special character, one even-digit and one-odd-digit, return an output string outstr based on the number of special characters as below:
- If the number of special characters is odd,append all the odd digits and even digits alternatively to the outstr, starting with the first odd digit.
- If the number of special character is even, append all the even digits and odd digits alternatively the outstr,starting wiht the first even digit.
- After arranging the digits based on the above two points,if there are any additional digits remaining, append them at the end of outstr.
Input: A5c67r21i@p#8t
Output: 652781
Input: h93@5213#w4rld&
Output: 9234513
"""
string = input()
num = []
count = 0
for i in string:
if i.isalpha():
continue
elif i.isdigit():
num.append(int(i))
else:
count+=1
# print(num)
even = list(filter(lambda x:x%2 == 0,num))
odd = list(filter(lambda x:x%2 != 0,num))
# print(even)
# print(odd)
# approach1 for printing even odd
if count&1 == 1: # for odd
for i in range(max(len(even),len(odd))):
if i < len(odd):
print(odd[i],end="")
if i < len(even):
print(even[i],end="")
else: #for even
for i in range(max(len(even),len(odd))):
if i < len(even):
print(even[i],end="")
if i < len(odd):
print(odd[i],end="")
"""
approach 2 for printing even odd
if count&1 == 1:
odd,even = even,odd
for i in range(max(len(even),len(odd))):
if i < len(even):
print(even[i],end="")
if i < len(odd):
print(odd[i],end="")
"""