python编程提升1(答案篇)
#!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common(): def __init__(self,name,size=0,parent=None): self.name = name self.size=size self.parent=parent def get_full_path(self): if self.parent == None: return self.name return self.parent.get_full_path() + "/" +self.name def __str__(self): return self.get_full_path() class File(Common): def __init__(self, name,size,parent=None): Common.__init__(self,name,size,parent) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name,size=0,parent=None): Common.__init__(self,name,size,parent) self.files={} self.dirs={} self.size=size def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size,self) self.files[file_name]=file return file def new_dir(self,dir_name): dir_=Dir(dir_name,parent=self) self.dirs[dir_name]=dir_ return dir_ def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size
#!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common: def __init__(self, name,size=0): self.name = name self.size=size class File(Common): def __init__(self, name,size): Common.__init__(self,name,size) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name): Common.__init__(self,name) self.files={} self.dirs={} self.size=0 def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size) self.files[file_name]=file return(file) def new_dir(self,dir_name): dir=Dir(dir_name) self.dirs[dir_name]=dir return dir def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size
问题1
Code:
─ #!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common: def __init__(self, name): self.name = name class File(Common): def __init__(self, name): Common.__init__(self,name) def get_file(self): return self.name class Dir(Common): def __init__(self, name): Common.__init__(self,name)
self.files={}
self.dirs={}
def get_dir(self): return self.name
Test:
if __name__ == "__main__": file_a=File("a") print(file_a.get_file()) dir_b=Dir("b") print(dir_b.get_dir())
Output:
a
b
问题2:
Code:
#!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common: def __init__(self, name): self.name = name class File(Common): def __init__(self, name): Common.__init__(self,name) def get_file(self): return self.name class Dir(Common): def __init__(self, name): Common.__init__(self,name) self.files={} self.dirs={} def get_dir(self): return self.name def new_file(self,file_name): file=File(file_name) self.files[file_name]=file return(file) def new_dir(self,dir_name): dir=Dir(dir_name) self.dirs[dir_name]=dir return dir
Test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") print(file_a.get_file()) dir_c=dir_b.new_dir("c") print(dir_c.get_dir())
Output:
a
c
问题:3
Code:
#!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common: def __init__(self, name,size=0): self.name = name self.size=size class File(Common): def __init__(self, name,size): Common.__init__(self,name,size) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name): Common.__init__(self,name) self.files={} self.dirs={} self.size=0 def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size) self.files[file_name]=file return(file) def new_dir(self,dir_name): dir=Dir(dir_name) self.dirs[dir_name]=dir return dir def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size
Test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("d",30) file_a.set_size(10) print(file_a.get_size()) print(dir_b.get_size())
Output:
10
60
问题4:
code:
─ #!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common(): def __init__(self,name,size=0,parent=None): self.name = name self.size=size self.parent=parent def get_full_path(self): if self.parent == None: return self.name return self.parent.get_full_path() + "/" +self.name class File(Common): def __init__(self, name,size,parent=None): Common.__init__(self,name,size,parent) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name,size=0,parent=None): Common.__init__(self,name,size,parent) self.files={} self.dirs={} self.size=size def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size,self) self.files[file_name]=file return file def new_dir(self,dir_name): dir_=Dir(dir_name,parent=self) self.dirs[dir_name]=dir_ return dir_ def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size
test:
if __name__ == "__main__":
dir_b=Dir("b")
file_a=dir_b.new_file("a")
file_c=dir_b.new_file("c",20)
dir_d=dir_b.new_dir("d")
file_e=dir_d.new_file("e",30)
print(file_e.get_full_path())
print(dir_d.get_full_path())
output:
b/d/e
b/d
问题4:
code:
─ #!/home/utils/Python/builds/3.9.9-20220110/bin/python3 class Common(): def __init__(self,name,size=0,parent=None): self.name = name self.size=size self.parent=parent def get_full_path(self): if self.parent == None: return self.name return self.parent.get_full_path() + "/" +self.name def __str__(self): return self.get_full_path() class File(Common): def __init__(self, name,size,parent=None): Common.__init__(self,name,size,parent) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name,size=0,parent=None): Common.__init__(self,name,size,parent) self.files={} self.dirs={} self.size=size def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size,self) self.files[file_name]=file return file def new_dir(self,dir_name): dir_=Dir(dir_name,parent=self) self.dirs[dir_name]=dir_ return dir_ def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size
test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("e",30) print(file_e) print(dir_d)
output:
b/d/e
b/d
问题6
code:
class Common(): def __init__(self,name,size=0,parent=None): self.name = name self.size=size self.parent=parent def get_full_path(self): if self.parent == None: return self.name return self.parent.get_full_path() + "/" +self.name def __str__(self): return self.get_full_path() class File(Common): def __init__(self, name,size,parent=None): Common.__init__(self,name,size,parent) def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name class Dir(Common): def __init__(self, name,size=0,parent=None): Common.__init__(self,name,size,parent) self.files={} self.dirs={} self.size=size def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size,self) self.files[file_name]=file return file def new_dir(self,dir_name): dir_=Dir(dir_name,parent=self) self.dirs[dir_name]=dir_ return dir_ def get_size(self): for file_name, file in self.files.items(): self.size+=file.get_size() for dir_name, dir in self.dirs.items(): self.size+=dir.get_size() return self.size def get_files_count(self): count=0 for file_name, file in self.files.items(): count += 1 for dir_name, dir in self.dirs.items(): count += dir.get_files_count() return count
test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("e",30) print(dir_b.get_files_count())
output:
3
问题7
code:
def ls(self): files=[] dirs=[] for file_name, file in self.files.items(): files.append(file_name) for dir_name, dir_ in self.dirs.items(): dirs.append(dir_name) return {"files":files, "dirs":dirs}
test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("e",30) print(dir_b.ls())
output:
{'files': ['a', 'c'], 'dirs': ['d']}
问题8
code:
class File(Common): ... def dump(self): return {"name": self.name, "size": self.size} class Dir(Common): ... def dump(self): files=[] dirs=[] for file_name, file in self.files.items(): files.append(file.dump()) for dir_name, dir_ in self.dirs.items(): dirs.append(dir_.dump()) return {"name": self.name, "files": files, "dirs": dirs}
test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("e",30) print(file_e.dump()) print(dir_b.dump())
output:
{'name': 'e', 'size': 30}
{'name': 'b', 'files': [{'name': 'a', 'size': 0}, {'name': 'c', 'size': 20}], 'dirs': [{'name': 'd', 'files': [{'name': 'e', 'size': 30}], 'dirs': []}]}
问题9:
code:
class Common(): def __init__(self,name="",size=0,parent=None,info=None): self.name=name self.size=size self.parent=parent def get_full_path(self): if self.parent == None: return self.name return self.parent.get_full_path() + "/" +self.name def __str__(self): return self.get_full_path() class File(Common): def __init__(self,name="",size=0,parent=None,info=None): Common.__init__(self,name,size,parent) if info != None: self._load(info) def _load(self,info): self.name=info["name"] self.size=info["size"] def set_size(self,size): self.size = size def get_size(self): return self.size def get_file(self): return self.name def dump(self): return {"name": self.name, "size": self.size} class Dir(Common): def __init__(self,name="",size=0,parent=None,info=None): Common.__init__(self,name,size,parent,info) self.files={} self.dirs={} self.size=size if info != None : self._load(info) def _load(self, info): self.name=info["name"] for file_info in info["files"]: new_file=File(info=file_info) self.files[new_file.name]=new_file for dir_info in info["dirs"]: new_dir=Dir(info=dir_info) self.dirs[new_dir.name]=new_dir def get_dir(self): return self.name def new_file(self,file_name,size=0): file=File(file_name,size,self) self.files[file_name]=file return file def new_dir(self,dir_name): dir_=Dir(dir_name,parent=self) self.dirs[dir_name]=dir_ return dir_ def dump(self): files=[] dirs=[] for file_name, file in self.files.items(): files.append(file.dump()) for dir_name, dir_ in self.dirs.items(): dirs.append(dir_.dump()) return {"name": self.name, "files": files, "dirs": dirs}
test:
if __name__ == "__main__": dir_b=Dir("b") file_a=dir_b.new_file("a") file_c=dir_b.new_file("c",20) dir_d=dir_b.new_dir("d") file_e=dir_d.new_file("e",30) file_e1=File(info=file_e.dump()) print(file_e.dump()) print(file_e1) print(file_e1.dump()) print(dir_b.dump()) dir_b1=Dir(info=dir_b.dump()) print(dir_b1) print(dir_b1.dump())
output:
{'name': 'e', 'size': 30}
e
{'name': 'e', 'size': 30}
{'name': 'b', 'files': [{'name': 'a', 'size': 0}, {'name': 'c', 'size': 20}], 'dirs': [{'name': 'd', 'files': [{'name': 'e', 'size': 30}], 'dirs': []}]}
b
{'name': 'b', 'files': [{'name': 'a', 'size': 0}, {'name': 'c', 'size': 20}], 'dirs': [{'name': 'd', 'files': [{'name': 'e', 'size': 30}], 'dirs': []}]}