Last active
August 29, 2015 14:22
-
-
Save bbn-bernard/b6df18c4c89ebbc8feee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
import unittest | |
# input : N positive integer, ex. N = 10 | |
# output : sequence as list, ex. 1 2 * 3 4 5 * 6 7 * 8 9 10 * | |
def fancy_pattern(N): | |
pattern = '11*111*' * int(math.ceil(N / 5.0)) | |
result = [] | |
n = 0 | |
i = 0 | |
while i < N: | |
if pattern[n] == '1': | |
result.append(str(i+1)) | |
i += 1 | |
else: | |
result.append(pattern[n]) | |
n += 1 | |
# NOTE: append '*' if number appeare 2 / 3 times at the end of result after last '*' | |
if ((N - 2) % 5) == 0 or (N % 5) == 0: | |
result.append(pattern[len(pattern) - 1]) | |
return result | |
class TestSequence(unittest.TestCase): | |
def test_fancy_pattern(self): | |
expected = [['1'], | |
['1', '2', '*'], | |
['1', '2', '*', '3'], | |
['1', '2', '*', '3', '4'], | |
['1', '2', '*', '3', '4', '5', '*']] | |
ok_cnt = 0 | |
len_data = len(expected) | |
for i in range(1, len_data + 1): | |
result = fancy_pattern(i) | |
if result == expected[i-1]: | |
ok_cnt += 1 | |
self.assertEqual(ok_cnt, len_data) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1 and sys.argv[1] == 'TestSequence': | |
unittest.main() | |
sys.exit() | |
N = 1000 | |
result = fancy_pattern(N) | |
if len(result) > 25: | |
text = ' '.join(result[:21]) | |
text += ' ... %s' % ' '.join(result[len(result) - 2:]) | |
else: | |
text = ' '.join(result) | |
print text | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment