由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道dropbox面试题
相关主题
问一个面试题请教个G题目
问道Pocket Gems面试题Amazon 打印给定node距离最近的K个nodes
MS onsite 面经L家这题咋搞,巨变态
Lowest Common Ancestor大牛帮我看一段code
回馈本版,新鲜店面,新题新气象一道C面试题
热腾腾的 LinkedIn 电面题攒RPamazon一道面试题
一道google面试题贴个自己的答案:Binary Tree Max Path Sum
python里面怎么表示树?A家面经求Offer
相关话题的讨论汇总
话题: nodes话题: args话题: def话题: self话题: node
进入JobHunting版参与讨论
1 (共1页)
f**********t
发帖数: 1001
1
(Coding) Write a basic file system and implement the commands ls, pwd, mkdir
, create, rm, cd, cat, mv. This was done on my own computer, in the
interview room, with a 1.5 hour time limit.
1.5小时,大牛提示下咋写?谢谢
w**a
发帖数: 487
2
file system一般都是用tree,每个node就是一个folder,是不是这样啊?

mkdir

【在 f**********t 的大作中提到】
: (Coding) Write a basic file system and implement the commands ls, pwd, mkdir
: , create, rm, cd, cat, mv. This was done on my own computer, in the
: interview room, with a 1.5 hour time limit.
: 1.5小时,大牛提示下咋写?谢谢

j**********3
发帖数: 3211
3
mark
f**********t
发帖数: 1001
4
自己先写点抛砖引玉好了,还没写完
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from collections import defaultdict
from os.path import join, getsize
# Write a basic file system and implement the commands ls, pwd, mkdir ,
# create, rm, cd, cat, mv.
class TreeNode:
def __init__(self, name, isDir):
self.name = name
self.children = defaultdict()
self.isDir = isDir
class FileSystem(object):
def __init__(self):
self.root = TreeNode("", True)
self.cur = self.root
def mkdir_(self, cur, nodes):
for node in nodes:
if node not in cur.children:
cur.children[node] = TreeNode(node, True)
cur = cur.children[node]
def mkdir(self, *args):
for arg in args:
nodes = arg.strip().split('/')
if nodes[0] == "": # absolute path
self.mkdir_(self.root, nodes[1:])
else:
self.mkdir_(self.cur, nodes)
# print (arg, ' ')
def dispatch(self, *args):
if len(args) < 1:
return
cmd = args[0].lower()
handler = getattr(self, cmd)
handler(*args[1:])
def ls_(self, cur, nodes):
for node in nodes:
if node not in cur.children:
return
cur = cur.children[node]
for v in self.cur.children.values():
print(v.name)
# print(v.name for v in self.cur.children.values())
def ls(self, *args):
if len(args) == 0:
ls_(self, self.cur, [])
else:
for arg in args:
nodes = arg.strip().split('/')
if nodes[0] == "":
self.ls_(self.root, nodes[1:])
else:
self.ls_(self.cur, nodes)
def walkDir(dir):
for root, dirs, files in os.walk(dir):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
def main():
fs = FileSystem()
while 1:
cmd = input(">> ")
# print(cmd)
fs.dispatch(*cmd.strip().split())
if __name__ == '__main__':
main()

【在 w**a 的大作中提到】
: file system一般都是用tree,每个node就是一个folder,是不是这样啊?
:
: mkdir

w**********o
发帖数: 140
5
直接FUSE.
具體可以自己查一下FUSE API, 填空就好了.
1 (共1页)
进入JobHunting版参与讨论
相关主题
A家面经求Offer回馈本版,新鲜店面,新题新气象
请教个面试题热腾腾的 LinkedIn 电面题攒RP
请教一道面试题一道google面试题
请教一道leetcode的online judge题python里面怎么表示树?
问一个面试题请教个G题目
问道Pocket Gems面试题Amazon 打印给定node距离最近的K个nodes
MS onsite 面经L家这题咋搞,巨变态
Lowest Common Ancestor大牛帮我看一段code
相关话题的讨论汇总
话题: nodes话题: args话题: def话题: self话题: node