请教car shipment有没有注意事项# Automobile - 车轮上的传奇
c*4
1 楼
详细信息在下面网址:
https://www.thumbtack.com/challenges/simple-database
面试一道题目,题目是设计一个Simple Database,我提交的解法说有问题然后被拒了。
只想各位大神帮我看看code有哪些毛病,feedback说算法不是最优的,function大小写
没有注意。
目前我觉得自己问题在NUMEQUALTO复杂度n不是1,方程都大写了,想请教下还有什么问
题。
下面是我python的代码。再次感谢大家的帮助,我只想知道代码哪里还出了问题。
import sys
class Database(object):
def __init__(self):
self._history = [] #list to store transaction history
self._database = {} #dictionary store key value pair
def BEGIN(self):
self._history.append({})
def SET(self, name, val):
if self._history:
if name in self._database and name not in self._history[-1]:
self._history[-1][name] = self._database[name]
if name not in self._database:
self._history[-1][name] = None
self.WriteToDB(name, val)
def GET(self, name):
if name in self._database:
print self._database[name]
else:
print 'NULL'
def NUMEQUALTO(self, val):
cnt=0
for key,value in self._database.items():
if value==val:
cnt+=1
print cnt
def UNSET(self, name):
if name in self._database:
self.SET(name, None)
def ROLLBACK(self):
if self._history:
for key, val in self._history[-1].items():
self.WriteToDB(key, val)
self._history.pop()
else:
print "NO TRANSACTION"
def COMMIT(self):
self._history = []
def WriteToDB(self, name, val): #Interface write to DB, Delete when val=
None
if val != None:
self._database[name] = val
else:
del self._database[name]
if __name__ == "__main__":
print "-----Simple Database Start-----"
ops = ['BEGIN', 'SET', 'GET', 'NUMEQUALTO', 'UNSET', 'ROLLBACK', 'COMMIT
']
linecounter = 1
linelimit = 100 #In case forget putting END in the input file.
data = Database()
line = sys.stdin.readline().strip()
while line != 'END':
if linecounter>linelimit:
print "Hit the linelimit. Current linelimit is {0}".format(
linelimit)
break;
args = line.split(' ')
if args[0] in ops:
f = getattr(Database, args[0]);
f(data,*args[1:])
else:
print 'INVALID INPUT In line:{0}...PASS LINE...'.format(
linecounter)
line = sys.stdin.readline().strip()
linecounter+=1
print "-----Simple Database End-----"
https://www.thumbtack.com/challenges/simple-database
面试一道题目,题目是设计一个Simple Database,我提交的解法说有问题然后被拒了。
只想各位大神帮我看看code有哪些毛病,feedback说算法不是最优的,function大小写
没有注意。
目前我觉得自己问题在NUMEQUALTO复杂度n不是1,方程都大写了,想请教下还有什么问
题。
下面是我python的代码。再次感谢大家的帮助,我只想知道代码哪里还出了问题。
import sys
class Database(object):
def __init__(self):
self._history = [] #list to store transaction history
self._database = {} #dictionary store key value pair
def BEGIN(self):
self._history.append({})
def SET(self, name, val):
if self._history:
if name in self._database and name not in self._history[-1]:
self._history[-1][name] = self._database[name]
if name not in self._database:
self._history[-1][name] = None
self.WriteToDB(name, val)
def GET(self, name):
if name in self._database:
print self._database[name]
else:
print 'NULL'
def NUMEQUALTO(self, val):
cnt=0
for key,value in self._database.items():
if value==val:
cnt+=1
print cnt
def UNSET(self, name):
if name in self._database:
self.SET(name, None)
def ROLLBACK(self):
if self._history:
for key, val in self._history[-1].items():
self.WriteToDB(key, val)
self._history.pop()
else:
print "NO TRANSACTION"
def COMMIT(self):
self._history = []
def WriteToDB(self, name, val): #Interface write to DB, Delete when val=
None
if val != None:
self._database[name] = val
else:
del self._database[name]
if __name__ == "__main__":
print "-----Simple Database Start-----"
ops = ['BEGIN', 'SET', 'GET', 'NUMEQUALTO', 'UNSET', 'ROLLBACK', 'COMMIT
']
linecounter = 1
linelimit = 100 #In case forget putting END in the input file.
data = Database()
line = sys.stdin.readline().strip()
while line != 'END':
if linecounter>linelimit:
print "Hit the linelimit. Current linelimit is {0}".format(
linelimit)
break;
args = line.split(' ')
if args[0] in ops:
f = getattr(Database, args[0]);
f(data,*args[1:])
else:
print 'INVALID INPUT In line:{0}...PASS LINE...'.format(
linecounter)
line = sys.stdin.readline().strip()
linecounter+=1
print "-----Simple Database End-----"