-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_decoding.py
More file actions
65 lines (56 loc) · 2.13 KB
/
string_decoding.py
File metadata and controls
65 lines (56 loc) · 2.13 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File: string_decoding.py
# Description: Decoding string, decompression task
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# [1] Valentyn N Sichkar. Decoding string, decompression task // GitHub platform [Electronic resource]. URL: https://github.com/sichkar-valentyn/String_Decoding (date of access: XX.XX.XXXX)
# Implementing the task
# Decoding input string
# Example
# Input string: 3a5Ba4CcF3f
# Decoded string: aaaBBBBBaCCCCcFfff
# Also, program will check if the number has more then one digit
# This task is so called basic compressing task
# Creating a function for dividing input string by groups
def string_grouping(input_string):
# Checking if input string is just a one letter
if len(input_string) == 1:
return input_string
else:
# Creating a list to add grouped elements
lst = []
# Variable for iteration
i = 0
# Going through elements of the string
while True:
# Checking if the element is a digit and the next element is not a digit
if input_string[i].isdigit() and not input_string[i + 1].isdigit():
lst += [int(input_string[i]) * input_string[i + 1]]
i += 2
# Checking if number has more then one digit
elif input_string[i].isdigit() and input_string[i + 1].isdigit():
# Collecting all digits together
# Variable for number
number = ''
# Variable for iteration
j = i
while True:
if input_string[j].isdigit():
number += input_string[j]
j += 1
else:
lst += [int(number) * input_string[j]]
break
i = j + 1
else:
lst += [input_string[i]]
i += 1
if i >= len(input_string):
break
return lst
# Input string and returning decoded result
print(''.join(string_grouping(input())))