首页 > 代码库 > 百度2016笔试(算法春招实习)
百度2016笔试(算法春招实习)
4.23 10:00更新。编程题1的Python实现。仅供參考。源代码见页尾
4.23 20:35更新,编程题2的Python实现。源代码见尾页
百度的题还是很偏重算法的。总体来讲难度比較高。尤其是编程题,以下附上原题:
选择题
问答题
主观题
编程题
编程题1源代码
#coding:utf-8 data = http://www.mamicode.com/[]>?)'], [1,2], [2,8]] data.append(item) #data = http://www.mamicode.com/[ ['(?
?)', [1,2], [2,8]], ...... ] # 生成全部括号的可能性 def allThePosibility(theString,data): # 对于该问号有改成)和(这两种可能 if theString.count('?
') == 0: data.append(theString) else: theStringToLeft = '' theStringToRight = '' theIndex = theString.index('?') # 第一个问号的位置 theStringToLeft = theString[:theIndex] + '(' + theString[theIndex+1:] #print theStringToLeft theStringToRight = theString[:theIndex] + ')' + theString[theIndex + 1:] #print theStringToRight allThePosibility(theStringToLeft,data) allThePosibility(theStringToRight,data) return data # ['((()', '(())', '()()', '()))'] # 是否正则化 def isRegularization(theString): # theString = '((()' if theString.count('(') != theString.count(')'): return 0 stack = [] # 设置一个栈 for alphabet in theString: if alphabet == ')' and stack == []: return 0 else: if alphabet == '(': stack.append(0) # 入栈 else: # 遇到右括号 stack.pop() # 出栈 if stack != []: return 0 else: return theString # 每一个问号的位置 def positionOfQuestionMark(theString): # theString = '(?
?
)' i = 0 position = [] while True: if '?
' in theString[i:]: theIndex = theString[i:].index('?
') # 更新下一个问号的位置 i += theIndex position.append(i) i += 1 else: break return position # [1,2] # 处理数据 for item in data: # item = ['(??)', [1,2], [2,8]] regularzations = [] # 全部括号的位置 position = positionOfQuestionMark(item[0]) # position = [1,2] # 列出全部能加括号的情况 posibilities = allThePosibility(item[0], data=http://www.mamicode.com/[]) # posibilities = ['((()', '(())', '()()', '()))']>
编程题2
#coding:utf-8 # 百度笔试题2 # 先处理输入 theString = raw_input() base = int(theString.split(' ')[0]) # string型 luckyNum = int(theString.split(' ')[1]) # string型 if base < luckyNum: print luckyNum elif base > luckyNum and len(str(base)) > len(str(luckyNum)): x = len(str(luckyNum)) # 幸运数的位数 if int(str(base)[len(str(base)) - x : ]) <= luckyNum: # 假设base的后x位小于等于幸运数 print str(base)[:len(str(base)) - x] + str(luckyNum) else: # base的后x为大于幸运数 tagNum = int(str(base)[len(str(base)) -x -1:len(str(base)) -x]) # base比x高一位的数,int型 tagNum += 1 answer = (str(base)[:len(str(base)) - x - 1]) + str(tagNum) + str(luckyNum) print answer elif base > luckyNum and len(str(base)) == len(str(luckyNum)): # 在luckNum的左面写个1即可了 print '1' + str(luckyNum)
百度2016笔试(算法春招实习)