-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateEncoder.py
More file actions
30 lines (14 loc) · 1.04 KB
/
DuplicateEncoder.py
File metadata and controls
30 lines (14 loc) · 1.04 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
# The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
# Examples
# "din" => "((("
# "recede" => "()()()"
# "Success" => ")())())"
# "(( @" => "))(("
# Notes
# Assertion messages may be unclear about what they display in some languages. If you read "...It Should encode XXX", the "XXX" is the expected result, not the input!
def duplicate_encode(word):
return "".join(["(" if word.lower().count(x) == 1 else ")" for x in word.lower()])
# for this we need to check the count of each letter. So we create a dict or we can use .count. .count is easier but both
# are relavant. so we create a for loop and itterate over our word checking if the neword.count(letter) is greater than 1.
# if it is, we add ), if not we add (.
#then return encode or encoded_string