Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double or one thing solution, python solution #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions solutions/double-or-one-thing/doubleOrOneThing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import numpy as np

# number of Test Cases
T = int(input())

# iterate through all cases
for t in range(1, T+1):
# read case string
# and append a ! to make it easier to iterate through the string
S = input()+"!"

# encode original string into a histogram-like format
# COOOMMETT - > [(C, 1), (O, 3), (E, 1), (T, 2)]
count = 0
occ = []
for i in range(len(S)-1):

# set current character and add one to its consecutive occurrences
s = S[i]
count = count + 1

# set next character
next_s = S[i+1]

# append occurrences
if next_s !=s:
occ.append([s, count])
count = 0
#else:
# continue

# append artificial tuple (!, 0) to facilitate iteration over list
occ.append(("!", 0))

# iterate over the histogram-like string
for i in range(len(occ)-1):
ch1, times = occ[i]
ch2, _ = occ[i+1]

# if the following character has a greater order than the current one,
# double the ocurrences of the current character
if ch2>ch1:
occ[i] = (ch1, 2*times)

# print output
print(f"Case #{t}: {''.join([a*b for a, b in occ])}")