Testdome" Path" Python - 无法确定我的解决方案为何100%无误

时间:2017-05-18 00:46:32

标签: python

所以我使用testdome的公共questions来练习python,其中一个就是这个路径问题。我只能获得50%的解决方案,但我无法弄清楚原因。我甚至无法创建我自己的测试失败。

class Path:
    def __init__(self, path):
        self.current_path = path

    def cd(self, new_path):
        new_path_list = new_path.split('/')
        for item in new_path_list:
            if item == '':
                self.current_path = '/'
            elif item == '..':
                self.current_path = self.current_path[:-2]
            else:
                self.current_path = self.current_path + '/' + item

        if '//' in self.current_path:
            self.current_path = self.current_path.replace('//','/')

编辑:根据第一个响应更新了代码。仍然是50%。

感谢大家的帮助。

4 个答案:

答案 0 :(得分:1)

猜测,你在哪里

for item in new_path_list:
    if new_path_list[0] == '':

你的意思是

for item in new_path_list:
    if item == '':

编辑:我以为我自己尝试一下;这是我的表现(得分100%):

# https://www.testdome.com/questions/python/path/8735

ROOT = "/"
DIV = "/"
PREV = ".."

class Path:
    def __init__(self, path):
        self.dirs = []
        self.cd(path)

    @property
    def current_path(self):
        return str(self)

    def cd(self, path):
        if path.startswith(ROOT):
            # absolute path - start from the beginning
            self.dirs = []
            path = path[len(ROOT):]

        # follow relative path
        for dir in path.split(DIV):
            if dir == PREV:
                self.dirs.pop()
            else:
                self.dirs.append(dir)

    def __str__(self):
        return ROOT + DIV.join(self.dirs)


path = Path('/a/b/c/d')
path.cd('../x')
print(path.current_path)

答案 1 :(得分:0)

root='/'
div='/'
parent='..'

class Path:
    def __init__(self, path):
        self.current_path = path
        self.current = path.split('/')


    def cd(self, new_path):
        if new_path[0]=='/':
            self.current_path= "/"
            self.current= ['']
        new_path_list = new_path.split('/')

        for item in new_path_list:
            if item != '' :
                if item == parent:
                    self.current.pop()                    
                else:
                    self.current.append(item)
                self.current_path = div.join(self.current)

path = Path('/a/b/')
path.cd('../x')
path.cd('/a/b')
print(path.current_path)

答案 2 :(得分:0)

我最近刚刚开始学习Python,感谢上面回复的那些对新手有帮助的人!

我创建了自己的100%分辨率,在这里分享它作为像我这样的新手的另一个参考:

class Path:
    def __init__(self, path):
        self.current_path = path

    def cd(self, new_path):
        PREV = '..'
        DIV = '/'

        #c_list = ['', 'a', 'b', 'c', 'd']
        c_list = self.current_path.split(DIV)
        #n_list = ['..', 'x']
        n_list = new_path.split(DIV)

        for item in n_list:
            if item == PREV:
                #delete the last item in list
                del c_list[-1]
            else:
                c_list.append(item)
        #add "/" before each item in the list and printout as string                    
        self.current_path = "/".join(c_list)
        return self.current_path


path = Path('/a/b/c/d')
path.cd('../x')
print(path.current_path)

答案 3 :(得分:0)

相同,相同但不同...

class Path:
    def __init__(self, path):
        self.current_path = path

    def cd(self, new_path):
        if new_path[0] == '/':
            self.current_path = new_path
        else:
            for part in new_path.split('/'):
                if part == '..':
                    self.current_path = '/'.join((self.current_path.split('/')[:-1]))
                else:
                    self.current_path = self.current_path + '/' + part