0

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!!

prosody
  • 557
  • 1
  • 3
  • 11
  • My first thought from jail :p .. but seriously there is some syntax difference between windows and linux, i believe linux most of the time they use \n for newline, where windows in most editors uses \r\n\ some editors allow you to set the line ending unix or windows style (like notepad++) . So you might need to first remove \r . i dont like to spoil the solution here. – Peter Oct 25 '22 at 00:38
  • `op, kind, words = line.rstrip('\r\n').split(';')` should solve your problem – Nick Oct 25 '22 at 00:41
  • @Peter if you open the file in text mode which is the default, on Windows `\r\n` will be automatically converted to `\n` so that the same code works on any OS. – Mark Ransom Oct 25 '22 at 01:13
  • @Mark Ransom that depends on your favourite editor (settings). the origin of the two characters is from old typing machine \r tells a type machine to bring back the typing ball to the start, where only \n would be push line without moving to start position. Those where the days – Peter Oct 26 '22 at 22:14
  • @Peter I know the origins and why there are two characters. What I'm saying is that Python itself converts `\r\n` to just `\n` before your program sees it, if you open the file in text mode on Windows. – Mark Ransom Oct 27 '22 at 00:37

0 Answers0