Programming pearls are unique problems or solutions that might puzzle a programmer, they have grown from real problems that have irritated real programmers, just as natural pearls grow from grains of sand that irritate oysters.
Questions tagged [programming-pearls]
41 questions
3
votes
2 answers
Programming Pearls: Finding the missing integer in a file of 4 billion integers
Question: The input is on a sequential file. The file contains at most 4Billion integers. Find a missing integer.
Solution as per my understanding:
make two temporary files one with leading 0 and the other with leading 1
one of the two MUST( 4.3B…

user2885475
- 49
- 3
3
votes
2 answers
PHP: Comparing NULL and FALSE - Casted to ~Negative Infinity
I have recently stumbled upon a situation that seems to be a bug at best. Both null and false appear to be evaluated to lower but not equal to negative infinity when used in comparisons.
My current test case:
var_dump(
PHP_OS,
PHP_VERSION,
…

Khez
- 10,172
- 2
- 31
- 51
3
votes
2 answers
“Programming Pearls”: Searching
We can avoid many calls to a storage allocator by keeping a collection
of available nodes in his own structure.
This idea can be applied to Binary search tree data structure.
The author said that :"Allocating the nodes all at once can greatly…

Fihop
- 3,127
- 9
- 42
- 65
2
votes
2 answers
Sorting 10 million integers with 1 MB space Solution explanation - Programming Pearls
I was reading "Programming Pearls" and I am really confused in one of the solution explanations.
The question was:
"A file containing at most n positive integers, each less than n, where n = 10^7. Each positive integer could appear at most ten…

test123
- 13,865
- 9
- 28
- 33
2
votes
1 answer
Programming Pearls: Column 9.3 Binary Search - range initialization
In Section 9.3 Job Bentley presents a modified binary search..
a brief snip of the typical implementation and the better approach shown in 9.3
if (arr[mid] < key) low = mid+1
else if (arr[mid] > key) high = mid-1
else return…

Manohar
- 3,865
- 11
- 41
- 56
1
vote
2 answers
Matrix transposition on a magnetic tape
Programming pearls Problem 7 is about transposing a 4000 x 4000 matrix stored on a magnetic tape. My solution was to simply use a temporary variable and swap the contents of a[i][j] and a[j][i].
The solution given by the author confused me a little…

Lelouch Lamperouge
- 8,171
- 8
- 49
- 60
1
vote
1 answer
bsort example from programming pearls
In Programming Pearls there is an algorithm that sorts varying length arrays but sorts in time proportional to the sum of their length. For example, if we have a record array x[0...n-1], and each record has an integer length and a pointer to array…
user466534
1
vote
1 answer
Python Data Structure memory footprint behaving weird
I was trying out the one of the programming pearls:
Given a file containing at most ten million 7-digit integers with no duplicates. What is an efficient way to print these numbers in ascending order using just 1.5Mb RAM and reading the data just…

Fr_nkenstien
- 1,923
- 7
- 33
- 66
1
vote
1 answer
Algorithm for minimum sum subvector
The problem found in programming pearls column 8 is as follows:
Given the real vector x[n], compute the maximum sum found in any contiguous subvector.
The final solution provided is of O(n) complexity which is as follows:
std::vector x;
int…
Matthieu N.
1
vote
2 answers
Finding k elements of length-n list that sum to less than t in O(nlogk) time
This is from Programming Pearls ed. 2, Column 2, Problem 8:
Given a set of n real numbers, a real number t, and an integer k, how quickly can you determine whether there exists a k-element subset of the set that sums to at most t?
One easy solution…

tresbot
- 1,570
- 2
- 15
- 19
1
vote
2 answers
Why does this example use null padding in string comparisons? “Programming Pearls”: Strings of Pearls
In "Programming Pearls": Strings of Pearls, section 15.3 (Generating Text), the author introduces how to generate random text from an input document. In the source code, there are some things that I don't understand.
for (i = 0; i < k; i++)
…

Fihop
- 3,127
- 9
- 42
- 65
1
vote
3 answers
Expectation of the maximum consecutive subsequence sum of a random sequence
Here is a problem from Programming Pearls 2nd edition (Chapter 8.7):
Considering a real number sequence, whose elements are drawn uniformly from the range [-1, 1], what is the expected maximum consecutive subsequence sum? (If all the elements are…

RoBa
- 418
- 3
- 6
1
vote
2 answers
Reading a csv file
I have the comma separated flat file whose contents are below.
Name,Empno,Address,Contact,Company
A,1,”BTM, Bangalore”,1234,”HCL”
B,2,”Tatanagar”,1243,”WIPRO”
C,3,”Patna”,1254,”CTS”
D,4,”Doranda,Ranchi”,1253,”TCS”
I need to get only Name and…

Anshu Kunal
- 196
- 1
- 13
0
votes
0 answers
Problem from book Programming Pearls, Find duplicate number from 4,300,000,000 integers in *Linear time*?
Let me excerpt this problem and solution from Jon Bentley's book Programming Pearls:
Given a file containing 4,300,000,000 integers, how can you find one that appears at least twice?
Solution:
Binary search find an element that occurs at least…

hello.wjx
- 239
- 3
- 13
0
votes
2 answers
Remove characters with pattern from a tab-delimited file
I have saveral files with pattern such…

Shail
- 17
- 4