forked from Kushal-Pareek/CodeRed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode1.py
More file actions
27 lines (26 loc) · 801 Bytes
/
Code1.py
File metadata and controls
27 lines (26 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Compressing a string
# representing repeating characters as a tuple of the character and their frequency
List = list(map(int, input("Enter the string you want to compress : ")))
Listr = []
for i in range(len(List)):
count = 1
if i == 0:
for j in range(i, len(List)-1):
if List[j] == List[j+1]:
count += 1
else:
break
Listr.append(str((count, List[i])))
continue
if List[i] == List[i-1]:
continue
else:
count = 1
for j in range(i, len(List)-1):
if List[j] == List[j+1]:
count += 1
else:
break
Listr.append(str((count, List[i])))
print("The compressed string is : ", end="")
print(*Listr)