chapter 1 hackerrank problems

string category, easy problems
This commit is contained in:
anebz 2019-04-13 15:04:05 +02:00
parent 07ee63c9ef
commit 7d0c00f534
3 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,26 @@
# Algorithms > Strings > Making Anagrams
# https://www.hackerrank.com/challenges/making-anagrams
import unittest
from collections import Counter
def makingAnagrams(s1, s2):
counts = Counter(s1)
counts.subtract(s2)
return sum(abs(x) for x in counts.values())
class Test(unittest.TestCase):
data = [(('a', 'b'), 2),
(('ab', 'ab'), 0),
(('abc', 'amnop'), 6),
(('cde', 'abc'), 4),
(('cde', 'abcc'), 5)]
def test(self):
for test_string, expected in self.data:
res = makingAnagrams(*test_string)
self.assertEqual(res, expected)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,29 @@
# Algorithms > Strings > Anagram
# https://www.hackerrank.com/challenges/anagram
import unittest
from collections import Counter
def anagram(s):
if len(s) % 2 == 1:
return -1
counts = Counter(s[:len(s)//2])
counts.subtract(s[len(s)//2:])
return sum(abs(x) for x in counts.values())//2
class Test(unittest.TestCase):
data = [('aaabbb', 3),
('ab', 1),
('abc', -1),
('mnop', 2),
('xyyx', 0),
('xaxbbbxx', 1)]
def test(self):
for test_string, expected in self.data:
res = anagram(test_string)
self.assertEqual(res, expected)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,35 @@
# Algorithms > Strings > Strong Password
# https://www.hackerrank.com/challenges/strong-password
import unittest
def minimumNumber(s):
numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"
count = 0
if not any(x in numbers for x in s):
count += 1
if not any(x in lower_case for x in s):
count += 1
if not any(x in upper_case for x in s):
count += 1
if not any(x in special_characters for x in s):
count += 1
return max(count, 6-len(s))
class Test(unittest.TestCase):
data = [('Ab1', 3),
('#HackerRank', 1)]
def test(self):
for test_string, expected in self.data:
res = minimumNumber(test_string)
self.assertEqual(res, expected)
if __name__ == "__main__":
unittest.main()