- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在使用 testdome
的公共(public) 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%。
感谢大家的帮助。
最佳答案
猜猜你在哪里
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)
关于python - Testdome "Path"Python - 无法确定为什么我的解决方案不是 100% 正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44036874/
我遇到了一个用 Python 执行的问题: LeagueTable 类跟踪联赛中每个球员的得分。每场比赛结束后,玩家使用 record_result 函数记录他们的分数。 玩家在联赛中的排名使用以下逻
我一直在尝试解决链接here上的问题。我的解决方案在 testdome 上遇到了 stackoverflow 错误,尽管当我在 IDE 中运行它时它可以工作。 我的解决方案: private stat
我遇到了一个用 Python 执行的问题: LeagueTable 类跟踪联赛中每个球员的得分。每场比赛结束后,玩家使用 record_result 函数记录他们的分数。 玩家在联赛中的排名使用以下逻
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 2 年前。 Improve t
这是 question 的链接 这是我的答案 Spreadsheet td { text-align: right;
我正在为 testdome https://www.testdome.com/for-developers/solve-question/9708 中给出的测试样本写答案 问题是关于二叉搜索树的: 二
我正在编写一个函数,给定一个numbers 列表和一个target_sum,返回任何 两个不同元素的从零开始的索引其总和等于目标总和。如果没有这样的元素,该函数应返回 (-1, -1)。 这就是我想出
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我试图从 TestDome 回答这个 question 并得到 250877.19298245612 而不是建议的 250000。请告诉我出了什么问题。谢谢 import numpy as np fr
所以我正在使用 testdome 的公共(public) questions练习python,其中之一就是这个路径问题。我只能得到 50% 的解决方案,但我不明白为什么。我什至无法创建自己的失败测试。
关于 Test Dome 的问题. 实现一个 group_by_owners 函数: 接受包含每个文件名的文件所有者名称的字典。 返回一个字典,其中包含每个所有者名称的文件名列表,顺序不限。 例如,对
我正在尝试解决 testdome.com 中的 C# 编程问题,但我发现了有关性能的问题。如何解决? 二叉搜索树 using System; public class Node { publi
我是一名优秀的程序员,十分优秀!