mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move string compress to a class, update test
This commit is contained in:
parent
a9d18bdcc0
commit
aa887db2e0
|
@ -81,9 +81,11 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def compress_string(string):\n",
|
"class CompressString(object):\n",
|
||||||
" # TODO: Implement me\n",
|
"\n",
|
||||||
" pass"
|
" def compress(self, string):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -120,13 +122,14 @@
|
||||||
" assert_equal(func(None), None)\n",
|
" assert_equal(func(None), None)\n",
|
||||||
" assert_equal(func(''), '')\n",
|
" assert_equal(func(''), '')\n",
|
||||||
" assert_equal(func('AABBCC'), 'AABBCC')\n",
|
" assert_equal(func('AABBCC'), 'AABBCC')\n",
|
||||||
" assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n",
|
" assert_equal(func('AAABCCDDDDE'), 'A3BC2D4E')\n",
|
||||||
" print('Success: test_compress')\n",
|
" print('Success: test_compress')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestCompress()\n",
|
" test = TestCompress()\n",
|
||||||
" test.test_compress(compress_string)\n",
|
" compress_string = CompressString()\n",
|
||||||
|
" test.test_compress(compress_string.compress)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
|
|
|
@ -97,21 +97,23 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def compress_string(string):\n",
|
"class CompressString(object):\n",
|
||||||
" if string is None or len(string) == 0:\n",
|
"\n",
|
||||||
" return string\n",
|
" def compress(self, string):\n",
|
||||||
" result = ''\n",
|
" if string is None or len(string) == 0:\n",
|
||||||
" prev_char = string[0]\n",
|
" return string\n",
|
||||||
" count = 0\n",
|
" result = ''\n",
|
||||||
" for char in string:\n",
|
" prev_char = string[0]\n",
|
||||||
" if char == prev_char:\n",
|
" count = 0\n",
|
||||||
" count += 1\n",
|
" for char in string:\n",
|
||||||
" else:\n",
|
" if char == prev_char:\n",
|
||||||
" result += prev_char + (str(count) if count > 1 else '')\n",
|
" count += 1\n",
|
||||||
" prev_char = char\n",
|
" else:\n",
|
||||||
" count = 1\n",
|
" result += prev_char + (str(count) if count > 1 else '')\n",
|
||||||
" result += prev_char + (str(count) if count > 1 else '')\n",
|
" prev_char = char\n",
|
||||||
" return result if len(result) < len(string) else string"
|
" count = 1\n",
|
||||||
|
" result += prev_char + (str(count) if count > 1 else '')\n",
|
||||||
|
" return result if len(result) < len(string) else string"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -153,7 +155,8 @@
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestCompress()\n",
|
" test = TestCompress()\n",
|
||||||
" test.test_compress(compress_string)\n",
|
" compress_string = CompressString()\n",
|
||||||
|
" test.test_compress(compress_string.compress)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
|
|
|
@ -13,7 +13,8 @@ class TestCompress(object):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestCompress()
|
test = TestCompress()
|
||||||
test.test_compress(compress_string)
|
compress_string = CompressString()
|
||||||
|
test.test_compress(compress_string.compress)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user