Saturday, February 27, 2016

Today we present a solution to a coding question:
Given an array S of n integers, find three integers in S such that sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Array S = {-1, 2, 1, -4 } and target = 1. The sum that is closest to the target is 2. (-1 +2 + 1) = 2
Int getClosestSum(List<int> sorted, int target)
{
sorted.sort();
Int min = INT_MAX;
Int val = 0;
Int len = sorted.count;
For (int i=0; i<len; i++)
{
Int start = I + 1, end = len-1;
While (start<end)
{
Int sum = sorted[i] + sorted[start] + sorted[end];
If (sum == target)
{
min = 0;
val = sum;
break;
}
If (sum  < target)
{
If (target-sum< min)
{
min = target –sum;
val = sum;
}
start++;
}
Else
{
If (sum – target < min)
{
min = sum – target;
val = sum;
}
end--;
}
}
If (val == target) break;
While (i < len -1 && sorted[i] == sorted[i+1]) i++;
}
Return val;
}
We could also use a different technique for arbitrary length selections such as number of integers chosen = 4, 5, 6, ... N-1 etc.
Here we could generate all combinations of length = 4, 5, 6 ,... N-1 from the given set of integers.
They need not be sorted in this case.
Then for each combination we find the sum and pick the one that closes to the target.
The combine() method described earlier in the posts does just this - given an array of n elements, generate combinations of all lengths.

Today we continue to discuss jigsaw.Efficient, low effort mashup isolation by James Mickens and Matthew Finifter. Mashups are web applications that contain code from different principals.
Jigsaw lets mashups be isolated with its framework  and lets them selectively expose private state. We were discussing the  end to end performance study of Jigsaw. 
Today we discuss related work. There are many preexisting frameworks that try to secure mashup applications. Jigsaw differs from those with a simple syntax. Moreover it provides hierarchical security and enables a pass by reference synchronous calls.Dojo Secure uses a restricted portion of the Javascript language and forces legacy applications to be rewritten. Jigsaw on the other hand doesn't. Fbjs also requires rewriting by prepending a prefix to all identifiers. Caja places scripts in virtualize execution environments and defines a tame (obj). A tame method performs many of the security checks that Jigsaw also does.
Jigsaw differs from Caja in the following ways:
Caja implements checks at sharing time not at declaration  time.
Developer must remember to call tame(obj)
Boundary crossings are not clearly discernible.
Caja is focused on making it easy for integrating page to load untrusted scripts. Jigsaw supports guest to guest access just as much.

No comments:

Post a Comment