We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d51b9ad commit 8c76dccCopy full SHA for 8c76dcc
1 file changed
python/Chapter 1/Question1_5/ChapQ1.5.py
@@ -42,3 +42,28 @@ def simpleCompress(compstring):
42
else:
43
print "Test 2 Failed"
44
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