Solving split string in python without the use of split() function

后端 未结 5 409
野趣味
野趣味 2021-01-15 20:59

Faced an interview question in Python that was as follow?

ex: 
input = (\'192.168.15.1\', \'.\', -1) ===> output = (192, 168, 15, 1)
input = (\'192.168.15         


        
5条回答
  •  一生所求
    2021-01-15 21:26

    Simple and recursive.

    def split_str(s,c,i):
        if i == 0:
            return [s]
        else:
            head, _, rest = s.partition(c)
            if rest:
                return [head] + split_str(rest, c, i - 1)
            return [head]
    

提交回复
热议问题