parent
44805d8516
commit
4f83176521
|
|
@ -0,0 +1,31 @@
|
||||||
|
# Algorithms > Strings > Alternating Characters
|
||||||
|
# https://www.hackerrank.com/challenges/alternating-characters
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
def alternatingCharacters(s):
|
||||||
|
if len(set(s)) == 1:
|
||||||
|
return len(s) - 1
|
||||||
|
|
||||||
|
res = len(s) - max(s.count("AB"), s.count("BA"))*2
|
||||||
|
if s[0] == s[-1]:
|
||||||
|
res -= 1
|
||||||
|
return res
|
||||||
|
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
|
||||||
|
data = [('AAAA', 3),
|
||||||
|
('BBBBB', 4),
|
||||||
|
('ABABABAB', 0),
|
||||||
|
('BABABA', 0),
|
||||||
|
('AAABBB', 4),
|
||||||
|
('ABABABAA', 1)]
|
||||||
|
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
for test_string, expected in self.data:
|
||||||
|
res = alternatingCharacters(test_string)
|
||||||
|
self.assertEqual(res, expected)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Algorithms > Strings > Funny string
|
||||||
|
# https://www.hackerrank.com/challenges/funny-string/
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
def funnyString(s):
|
||||||
|
if len(s) < 2:
|
||||||
|
return "Funny"
|
||||||
|
|
||||||
|
for i in range(1,len(s)//2):
|
||||||
|
if abs(ord(s[i]) - ord(s[i-1])) != abs(ord(s[-i]) - ord(s[-i-1])):
|
||||||
|
return "Not Funny"
|
||||||
|
|
||||||
|
return "Funny"
|
||||||
|
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
|
||||||
|
data = [('acxz', 'Funny'),
|
||||||
|
('bcxz', 'Not Funny'),
|
||||||
|
('bcccd', 'Funny')]
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
for test_string, expected in self.data:
|
||||||
|
res = funnyString(test_string)
|
||||||
|
self.assertEqual(res, expected)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
# Algorithms > Strings > The Love-Letter Mystery
|
||||||
|
# https://www.hackerrank.com/challenges/the-love-letter-mystery
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
def theLoveLetterMystery(s):
|
||||||
|
|
||||||
|
if len(s) == 1:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
for i in range(len(s)//2):
|
||||||
|
count += abs(ord(s[i]) - ord(s[-1-i]))
|
||||||
|
|
||||||
|
return count
|
||||||
|
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
|
||||||
|
data = [('abc', 2),
|
||||||
|
('abcba', 0),
|
||||||
|
('abcd', 4),
|
||||||
|
('cba', 2)]
|
||||||
|
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
for test_string, expected in self.data:
|
||||||
|
res = theLoveLetterMystery(test_string)
|
||||||
|
self.assertEqual(res, expected)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue