-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_encoding.py
44 lines (36 loc) · 1.48 KB
/
string_encoding.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File: string_encoding.py
# Description: Encoding string by grouping repeated letters with itertool library
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# [1] Valentyn N Sichkar. Encoding string by grouping repeated letters with itertool library // GitHub platform [Electronic resource]. URL: https://github.com/sichkar-valentyn/String_Encoding_with_Itertools (date of access: XX.XX.XXXX)
# Implementing the task
# Encoding input string
# Finding all repeated letters and calculating them
# Returning encoded string with number + letter
# No number for letters that are met only once
# Example
# input string: aaaBBBBBaCCCCcFfff
# Encoded string: 3a5Ba4CcF3f
# This task is so called basic compressing task
# Importing library to group the same letters in the string
import itertools
# Creating function for encoding
def encode(input_string):
# Going through the input string and returning encoded string
for letter, same in itertools.groupby(input_string):
# Calculating number of repetitions
number = sum(1 for _ in same)
# Returning just letter if it's alone and number+letter if it's not alone
# Returning a generator by using 'yield'
yield letter if number == 1 else str(number) + letter
# Input string
# Implementing function
# Joining results from function in one string
# Showing the result
# Everything in one line
print(''.join(encode(input())))