From cd0523bb80bf32c56597212a26c963f07126dc61 Mon Sep 17 00:00:00 2001 From: anebz Date: Thu, 4 Apr 2019 16:41:32 +0200 Subject: [PATCH] hackerrank string exercise --- .../hackerrank/mars_exploration.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Chapter 1 Arrays and strings/hackerrank/mars_exploration.py diff --git a/Chapter 1 Arrays and strings/hackerrank/mars_exploration.py b/Chapter 1 Arrays and strings/hackerrank/mars_exploration.py new file mode 100644 index 0000000..c419f06 --- /dev/null +++ b/Chapter 1 Arrays and strings/hackerrank/mars_exploration.py @@ -0,0 +1,46 @@ +# Algorithms > Strings > Mars Exploration +# https://www.hackerrank.com/challenges/mars-exploration + +import unittest +import numpy as np + +''' O(n), O(n) +def difference(s): + inputs = 'SOS' * int(len(s)/3) + + if s is inputs: + return 0 + + count = 0 + for a, b in zip(inputs, s): + if a != b: + count += 1 + + return count +''' + +# O(n/3), O(1) +def difference(s): + count = 0 + for i in range(0, len(s), 3): + if s[i] != 'S': + count += 1 + if s[i+1] != 'O': + count += 1 + if s[i+2] != 'S': + count += 1 + return count + +class Test(unittest.TestCase): + + data = [('SOS', 0), + ('SOA', 1), + ('SOSSASSEDAAA', 6)] + + def test(self): + for test_string in self.data: + res = difference(test_string[0]) + self.assertEqual(res, test_string[1]) + +if __name__ == "__main__": + unittest.main()