i'm doing a hackerrank python exercise in converting strings to and from camelcase.
my problem is with splitting camelcase to lowercase space-separated strings. or rather just one (the first) of the test-case strings - my solution works fine in pycharm but not on the hackerrank site where "/r/n" seems to appear from nowhere (i'm sure it's really just my mistake!)
worth noting the strings are coming from stdin which i haven't done much of before, and in pycharm i'm manually defining the strings and obviously not adding the errant characters.
i split the given string at capitals using regex, then join on " ". for the purposes of debug i then print the string which shows there are no escaped chars in it, but once returned to the hackerrank system the carriage return and newline chars appear.
assuming it's not just a fault with the website i want to understand where they are coming from.
the website informs me that the input string which fails is "S;V;iPad" - note the lack of carriage return
my code:
ui = sys.stdin.readlines()
def split_camel(words):
split_list = re.split('(?=[A-Z])', words)
print(f"{words=}")
split_s = " ".join(split_list).lower().replace("(", "").replace(")", "")
return split_s
def combine_camel(kind, words):
for i in str(words):
if not i.isalnum():
words = words.replace(i, ' ')
s = words.lower().split()
s2 = s[0] + ''.join(i.capitalize() for i in s[1:])
if kind == "M":
s2 = s2 + ("()")
if kind =="C":
first = s2[0].upper()
s2 = first + s2[1:]
return s2
def parse_input(ui):
output = []
for line in ui:
op, kind, words = line.split(';')
if op == 'S':
output.append(split_camel(words))
elif op == 'C':
output.append(combine_camel(kind, words))
else:
print("something wrong")
return output
print(parse_input(ui))
input as per website:
S;V;iPad
C;M;mouse pad
C;C;code swarm
S;C;OrangeHighlighter
my output as per website:
words='iPad\r\n'
words='OrangeHighlighter'
['i pad\r\n', 'mousePad()', 'CodeSwarm', ' orange highlighter']
where did "\r\n" come from?
thanks!!