Skip to content

Commit 8c76dcc

Browse files
committed
Update ChapQ1.5.py
1 parent d51b9ad commit 8c76dcc

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

python/Chapter 1/Question1_5/ChapQ1.5.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,28 @@ def simpleCompress(compstring):
4242
else:
4343
print "Test 2 Failed"
4444

45+
46+
#Modulated version of the solution
47+
def countSame(string, pos):
48+
#Counts number of same characters following a position
49+
pos1 = pos
50+
count = 0
51+
while pos1 < len(string) and string[pos1] == string[pos]:
52+
count = count +1
53+
pos1 = pos1 + 1
54+
return count
55+
56+
57+
def compress(string):
58+
outstring = []
59+
pos = 0
60+
while pos < len(string):
61+
outstring.append(string[pos] + str(countSame(string, pos)))
62+
pos = pos + countSame(string, pos)
63+
result = ''.join(outstring)
64+
if len(result) < len(string):
65+
return result
66+
else:
67+
retunr string
68+
69+
print compress("aabccc ccaaa")

0 commit comments

Comments
 (0)