Pasting python code to leetcode from pycharm

I like to write code on pycharm.
It helps me understand how to parse inputs, etc.

But whenever I paste the code to leetcode, all the formatting goes haywire.

I am unable to understand how to correct this behaviour.
Here is an example:

Pycharm code:

def to_int(s):
    refer_map = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    result = 0

    for i in range(0, len(s)-1):
        if refer_map[s[i]] < refer_map[s[i+1]]:
            result -= refer_map[s[i]]
        else:
            result += refer_map[s[i]]

    result += refer_map[s[len(s) - 1]]

    return result

Leetcode formatting after paste:

def romanToInt(self, s: str) -> int:
       refer_map = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    result = 0

    for i in range(0, len(s)-1):
        if refer_map[s[i]] < refer_map[s[i+1]]:
            result -= refer_map[s[i]]
        else:
            result += refer_map[s[i]]

    result += refer_map[s[len(s) - 1]]

    return result

I use vim, so I can ctrl-v + xj + shift-i + tab and just move the code, but was checking if there is a way to actually paste correctly.

Comments (1)