Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a179a23

Browse files
Create Alternating-Characters.py
1 parent d158f53 commit a179a23

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

‎String/Alternating-Characters.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
# Complete the alternatingCharacters function below.
10+
def checkalt(s1):
11+
c1=s1[0]
12+
for i in range(0,len(s1)):
13+
if i%2==0:
14+
if s1[i]!=c1:
15+
return False
16+
if i%2==1:
17+
if s1[i]==c1:
18+
return False
19+
return True
20+
def toggle_find(find):
21+
if find==65:
22+
find=66
23+
elif find==66:
24+
find=65
25+
return find
26+
27+
def a(i):
28+
count=0
29+
l=[ord(i1) for i1 in i]
30+
find=ord(i[0])
31+
for i in range(0,len(l)):#dont allow the first casw to go in
32+
if l[i]==find:
33+
find=toggle_find(find)
34+
else:
35+
count+=1
36+
return count
37+
38+
def alternatingCharacters(s):
39+
i=s
40+
if 'B' not in i:
41+
return len(i)-1
42+
elif 'A' not in i:
43+
return len(i)-1
44+
elif(checkalt(i)):
45+
return 0
46+
else:
47+
return a(i)
48+
return 0
49+
50+
51+
52+
if __name__ == '__main__':
53+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
54+
55+
q = int(input())
56+
57+
for q_itr in range(q):
58+
s = input()
59+
60+
result = alternatingCharacters(s)
61+
62+
fptr.write(str(result) + '\n')
63+
64+
fptr.close()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /