Boring is good. I usually avoid oneliners and magical solutions.
A more "production ready" version of the above:
'''A "duplicate encoder"s.
Based on https://dev.to/thepracticaldev/daily-challenge-259-duplicate-encoder-2e8l
When running on command line, encodes standard input.
You can also use the following args:
-t : Executes automated testing
-h : Shows this help'''defencode(text):'''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.
Examples:
>>> encode('Success')
')())())'
>>> encode('(( @')
'))(('
>>> encode('din')
'((('
>>> encode('recede')
'()()()'
'''encoded=''forcharintext:encoded+=replace_char(char,text)returnencodeddefreplace_char(char,text):'''Returns ')' if char appears more than once in text. Otherwise,
returns '('. Case insensitive. Examples:
>>> replace_char('a', 'banana')
')'
>>> replace_char('B', 'banana')
'('
'''iftext.lower().count(char.lower())>1:return')'return'('if__name__=='__main__':importsysifsys.argv[-1]=='-t':importdoctestdoctest.testmod()elifsys.argv[-1]=='-h':importpydocprint(pydoc.getdoc(sys.modules[__name__]))else:print(encode(sys.stdin.read()))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A magical Python solution:
A boring Python solution:
Boring is good. I usually avoid oneliners and magical solutions.
A more "production ready" version of the above: