Skip to content

Instantly share code, notes, and snippets.

@jayceazua
Last active December 2, 2019 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayceazua/592d9844136b5021402ed589efa7d6f5 to your computer and use it in GitHub Desktop.
Save jayceazua/592d9844136b5021402ed589efa7d6f5 to your computer and use it in GitHub Desktop.
VMware last coding problem
"""
Compress String
input:
i j
'a 1 2 c 9 b 5 6 c 1'
output:
'a12b56c10'
Explanation:
The solution that had submitted I had line 26 flipped in my condition statement.
I had to put the length check first before checking if it is an alphabet letter.
"""
def compress_string(s):
if not s:
return s
number = ""
chars = {}
j = 0
for index, char in enumerate(s):
if char.isalpha():
j = index + 1
# get integer
while j < len(s) and not s[j].isalpha():
number += s[j]
j += 1
num = int(number)
# reset the integer
number = ""
if char in chars:
chars[char] += num
else:
chars[char] = num
s = ""
letters = list(chars.keys())
for letter in sorted(letters):
s += letter + str(chars[letter])
return s
compress_string('a12c9b56c1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment