programming-pearls

“Programming Pearls” binary search help

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 13:01:40
问题 I just can't seem to understand how this would work. Question: Given a sequential file that contains at most four billion 32 bit integers in random order, find a 32-bit integer that isn't in the file (and there must be at least one missing) Answer: it is helpful to view this binary search in terms of the 32 bits that represent each integer. In the first pass of the algorithm we read the (at most) four billion input integers and write those with a leading zero bit to one sequential file and

Why is modulus operator slow?

半城伤御伤魂 提交于 2019-11-30 07:12:58
Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations ( + , - , * ) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why there is that much difference? How does a modulus operator work internally? Is it same as division ( / ) in terms of time? The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side effect or counterpart to division. Except for some degenerate cases (where the divisor is a power of the

“Programming Pearls” binary search help

非 Y 不嫁゛ 提交于 2019-11-30 05:01:45
I just can't seem to understand how this would work. Question: Given a sequential file that contains at most four billion 32 bit integers in random order, find a 32-bit integer that isn't in the file (and there must be at least one missing) Answer: it is helpful to view this binary search in terms of the 32 bits that represent each integer. In the first pass of the algorithm we read the (at most) four billion input integers and write those with a leading zero bit to one sequential file and those with a leading one bit to another file. One of those files contains at most two billion integer, so

Debugging and Binary Search

时光毁灭记忆、已成空白 提交于 2019-11-29 08:37:55
"Programming Pearls" in the column 2 ("AHA! Algorithm") talks about how binary search aids in various processes like sorting, tree traversals. But it mentions that binary seach can be used in "program debugging". Can somebody please explain how this is done? Another possibility is that you have a bug, and you know it wasn't there in your February release, but it was in your April release (or rather, your April release candidate -- you would never actually ship a bug to your users, right?). You can do a manual binary search through your revision-control history to narrow down when the bug was

Why is modulus operator slow?

我怕爱的太早我们不能终老 提交于 2019-11-29 03:02:20
问题 Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations ( + , - , * ) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why there is that much difference? How does a modulus operator work internally? Is it same as division ( / ) in terms of time? 回答1: The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side

Find a missing 32bit integer among a unsorted array containing at most 4 billion ints

拈花ヽ惹草 提交于 2019-11-28 09:27:16
This is the problem described in Programming pearls . I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks. EDIT: I can understand binary search in general. I just can not understand how to apply binary search in this special case. How to decide the missing number is in or not in some range so that we can choose another. English is not my native language, that is one reason I can not understand the author well. So, use plain english please:) EDIT: Thank you all for your great answer and comments ! The most important lesson I leant from

Debugging and Binary Search

☆樱花仙子☆ 提交于 2019-11-28 02:06:19
问题 "Programming Pearls" in the column 2 ("AHA! Algorithm") talks about how binary search aids in various processes like sorting, tree traversals. But it mentions that binary seach can be used in "program debugging". Can somebody please explain how this is done? 回答1: Another possibility is that you have a bug, and you know it wasn't there in your February release, but it was in your April release (or rather, your April release candidate -- you would never actually ship a bug to your users, right?

Effcient way to find longest duplicate string for Python (From Programming Pearls)

偶尔善良 提交于 2019-11-27 22:22:23
From Section 15.2 of Programming Pearls The C codes can be viewed here: http://www.cs.bell-labs.com/cm/cs/pearls/longdup.c When I implement it in Python using suffix-array: example = open("iliad10.txt").read() def comlen(p, q): i = 0 for x in zip(p, q): if x[0] == x[1]: i += 1 else: break return i suffix_list = [] example_len = len(example) idx = list(range(example_len)) idx.sort(cmp = lambda a, b: cmp(example[a:], example[b:])) #VERY VERY SLOW max_len = -1 for i in range(example_len - 1): this_len = comlen(example[idx[i]:], example[idx[i+1]:]) print this_len if this_len > max_len: max_len =

Effcient way to find longest duplicate string for Python (From Programming Pearls)

孤者浪人 提交于 2019-11-27 19:10:30
问题 From Section 15.2 of Programming Pearls The C codes can be viewed here: http://www.cs.bell-labs.com/cm/cs/pearls/longdup.c When I implement it in Python using suffix-array: example = open("iliad10.txt").read() def comlen(p, q): i = 0 for x in zip(p, q): if x[0] == x[1]: i += 1 else: break return i suffix_list = [] example_len = len(example) idx = list(range(example_len)) idx.sort(cmp = lambda a, b: cmp(example[a:], example[b:])) #VERY VERY SLOW max_len = -1 for i in range(example_len - 1):

Find a missing 32bit integer among a unsorted array containing at most 4 billion ints

一笑奈何 提交于 2019-11-27 02:55:49
问题 This is the problem described in Programming pearls . I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks. EDIT: I can understand binary search in general. I just can not understand how to apply binary search in this special case. How to decide the missing number is in or not in some range so that we can choose another. English is not my native language, that is one reason I can not understand the author well. So, use plain english please: