Python算法笔试题目,破解Hash值,N进制法
2014-10-13 22:13
381 查看
Find the string whichhas this hash: 25267566250558
The string has length8.
Characters can befrom: c,e,i,a,r,w,u,s,p
The hash functionworks like this:
hash(str):
1.LETTERS = c, e, i, a, r, w, u, s, p
2. h =7
3. forc in str:
1.i = index of c in LETTERS
2.h = 37 * h + i
4.return h
Please send us thestring you found, and the code you used to find it.
使用N进制的解法的核心思想是:首先计算出解法的总数(possibilitites),然后从0到possibilitites遍历,将每一个遍历到的结果转换为N进制(这里N是8)。
代码如下:
The string has length8.
Characters can befrom: c,e,i,a,r,w,u,s,p
The hash functionworks like this:
hash(str):
1.LETTERS = c, e, i, a, r, w, u, s, p
2. h =7
3. forc in str:
1.i = index of c in LETTERS
2.h = 37 * h + i
4.return h
Please send us thestring you found, and the code you used to find it.
使用N进制的解法的核心思想是:首先计算出解法的总数(possibilitites),然后从0到possibilitites遍历,将每一个遍历到的结果转换为N进制(这里N是8)。
代码如下:
#coding=utf-8 # for every possiblity , hexadecimal convertion is involved, its complexity is O("pow()*len(需要的字符串的长度)" *每次hash的计算量) ## correct: I found 'surprise' when the index of c in LETTERS begins from 0 # 'uwasaeuc' when 1 #==================== ## result: the only string found is # my result is based on the condition that the index of c in LETTERS begins # from 1 rather 0, otherwise, no answer will be found. ## analysis: # we don't konw the what specific characters their order are, # there may also be duplicate chracters # the goal is to find the string which has the specified key ## may be three solutions, I just implemented the first one # 1. recursive # 2. nonrecursive with stack # 3. dynamic programming # related project: combination(!permutation is more precise) of 8 chracters,eg 8-nary #-- need it? 37 的二进制, ans: no ## simulate the step 3 in hash(str): # use h(x) to represent the value of the xth time h # 3.1 h(1) = 37*7 + LETTERS.index('c') # 3.2 h(2) = 37*h(1) + LETTERS.index('c') # .. # so the general formula is h(x) = # idx = 0...len(str_hash)-1 # c = str_hash[idx] # i = LETTERS.index(c) # calculate the hash number of the str_hash str_hash_length = 8 LETTERS = ['c', 'e', 'i', 'a', 'r', 'w', 'u', 's', 'p'] # str_hash_length = 2 # LETTERS = ['c', 'e', 'i'] hash_result = 25267566250558 def calculate_hash(str_hash): h = 7 for c in str_hash: # print c i = LETTERS.index(c) # + 1 ### # print i, h = 37*h + i return h ## for test of calculate_hash() # str_hash = 'ec' # print calculate_hash(str_hash) ## calculate the str_hash using n-nary and recursive # instialize a list to store the characters str_hash_list = ['' for i in xrange(str_hash_length)] # print 'the type is ',type(str_hash_list) from math import pow def main(): LETTERS_len = len(LETTERS) possibilitites = pow(LETTERS_len, str_hash_length) #print possibilitites # result is 43046721.0 possibilitites = int(possibilitites) # selection state, use [0...LETTERS_len-1], or['c','e'...]; choose the former one selection_state = [7 for i in xrange(str_hash_length)] #print selection_state # for num in xrange(possibilitites): ## # pos_range = [ elem for elem in xrange(possibilitites)] # pos_range.reverse() for num in xrange(possibilitites): #for num in pos_range: num_copy = num # hexadecimal conversion , decimal --> N-nary, N is LETTERS_len( number of choices very time ) # after conversion, the N-nary number is a reverse of what it should be for place in xrange(str_hash_length): selection_state[place] = num_copy%LETTERS_len num_copy /= LETTERS_len # print selection_state str_hash = [LETTERS[j] for j in selection_state ] str_hash_value = calculate_hash(str_hash) # print str_hash # print str_hash_value if str_hash_value == hash_result: str_hash_string = ''.join(str_hash) print "the string found is '%s' " % (str_hash_string) if __name__ == '__main__': main()
相关文章推荐
- Python算法笔试题目,破解Hash值,回溯法
- 2014年阿里巴巴笔试题目(28题):最小三元数组距离最优算法
- 百度2014校招笔试题目题解(更新了第1题的算法,10.9下午)
- 【python】【leetcode】【算法题目2—Add Two Numbers】
- 【python】【leetcode】【算法题目344—Reverse String】
- 【python】【leetcode】【算法题目9—Palindrome Number】
- 一个笔试题目,望更方便的算法
- 【python】【leetcode】【算法题目383—Ransom Note】
- 用于解答算法题目的Python3代码框架
- 算法之美一书附录中笔试面试题目参考答案
- 【python】【leetcode】【算法题目389—Find the Difference】
- 2014年9.28号百度校招笔试题目算法题目1----不重复数题解
- Python笔试题目:求最大的K个数子,解法二,适合小型数据集的情况
- Python笔试题目:求最大的K个数子,解法一,最快速实现的方法
- 【python】【leetcode】【算法题目1—Two Sum】
- 算法学习 并查集(笔试题目:找同伙)
- 百度2014校招笔试题目题解(更新了第1题的算法,10.9下午)
- 整理的一些PYTHON笔试面试题目
- 【python】【leetcode】【算法题目4—Median of Two Sorted Arrays】
- 【python】【leetcode】【算法题目283—Palindrome Number】