My family, books, photos, technology, language and some math משפחתי, ספרים, תמונות, טכנולוגיה, שפה, וקצת מתמטיקה
Wednesday, January 16, 2008
Thursday, January 3, 2008
Detecting Credit Card Numbers
Detecting Credit Card Numbers in Network Traffic
I was investigating this about a year ago and got to similar conclusions. The cost of doing CCN detection while processing requests and responses via an IDS/IPS should be seamless if the system scales with the number of regular expressions (rather than iterating over them as one as to do with PCRE like libraries), as transducer based technology allows.
Calculating luhn and applying other means to reduce false positives has a negative impact on performance (although this can be reduced to some extent).
Tuesday, January 1, 2008
Bryan Peterson -- photographer
I want to recommend to you some wonderful books by a gifted photographer that contributed a lot to the results of my (good) photography (the bad photography is all my own fault). The photographer's name is Bryan Peterson. Check out his website at http://www.bryanfpeterson.com/
The books are:
If you don't make much of the technical information in the books you'll surely admire the photos. Nonetheless, the information next to the photos in the books is very inspiring and instructive.
The books are:
- His technical book about exposure is Understanding Exposure
- His composition book is Learning to See Creatively
- His book about digital photography is Understanding Digital Photography
- His book about taking pictures of people is Beyond Portraiture
- I just noticed that he has a new book coming Understanding Shutter Speed
If you don't make much of the technical information in the books you'll surely admire the photos. Nonetheless, the information next to the photos in the books is very inspiring and instructive.
Monday, December 31, 2007
Some Perl Unicode Idioms
Q: I want to represent a Unicode code-point as a Perl character
A:
Q: I want to get the list of bytes in UTF-8 encoding for a Unicode code-point
A:
Q: I want to see the list of bytes in UTF-8 encoding for a Unicode code-point represented in hex notation
A: For the Hebrew letter Alef (code point 0x05d0)
Here's a function that returns a blessed hash with a few useful operations:
A:
"\x{05d0}" will represent the Hebrew letter א as a Perl characterpack("U0U*",0x05d0) will do the sameQ: I want to get the list of bytes in UTF-8 encoding for a Unicode code-point
A:
my @bytes = unpack "C*", Encode::encode("utf8",pack("U0U",0x05d0));Q: I want to see the list of bytes in UTF-8 encoding for a Unicode code-point represented in hex notation
A: For the Hebrew letter Alef (code point 0x05d0)
my $octets = Encode::encode("utf8","\x{05d0}");
my @bytes = unpack "C*", $octets;
my @chars = map { sprintf "%#x", $_ } @bytes;
Here's a function that returns a blessed hash with a few useful operations:
sub new_char {
my ($code_point,$encoding) = @_;
my $perl_char = pack "U0U", $code_point;
my $octets_str = Encode::encode($encoding,$perl_char);
my @bytes = unpack "C*", $octets_str;
my $hex_str = [map {sprintf "%#x", $_} @bytes];
my $length = @bytes;
bless {
code_point => $code_point,
encoding => $encoding,
perl_char => $perl_char,
octets_str => $octets_str,
bytes => [@bytes],
hex_str => $hex_str,
length => $length,
}, 'Char';
}
Sunday, December 30, 2007
A interview question: the next permutation
Write the code for the following question:
OK. This sounds like a task that requires writing an iterator for the next permutation.
For a number (string) with n distinct digits (symbols) there are n! permutation.
n! is recursively defined for every non-negative integer to be:
Here are some examples for 1, 2, 3, and 4 digit numbers:
Observations:
You get an array of digits that represent a number. The digits are unique, such that for every index i in the array there is not index j such that the digit in i is the same as the digit in j (unless j=i). You need to write a function that will give the next number (the smallest number that has this property with its digits and the same digits, that is bigger than the given number). If the given number is the largest, then your function should indicate that there is no next number.
Examples:
* if you get 123 you should return 132
* if you get 4312 you should return 4321
* if you get 12543 you should return 13245
* if you get 21 you cannot return the next number because there is no next number.
OK. This sounds like a task that requires writing an iterator for the next permutation.
For a number (string) with n distinct digits (symbols) there are n! permutation.
n! is recursively defined for every non-negative integer to be:
So, we have a way to see if we wrote down all the permutations of such a number with n digits, by making sure that we wrote n! different numbers with the same digits.
0! = 1
n! = n*(n-1)!
Here are some examples for 1, 2, 3, and 4 digit numbers:
| n=1 | n=12 | n=123 | n=1234 |
| 1 | 12 | 123 | 1234 |
| 21 | 132 | 1243 | |
| 213 | 1324 | ||
| 231 | 1342 | ||
| 312 | 1423 | ||
| 321 | 1432 | ||
| 2134 | |||
| 2143 | |||
| 2314 | |||
| 2341 | |||
| 2413 | |||
| 2431 | |||
| 3124 | |||
| 3142 | |||
| 3214 | |||
| 3241 | |||
| 3412 | |||
| 3421 | |||
| 4123 | |||
| 4132 | |||
| 4213 | |||
| 4231 | |||
| 4312 | |||
| 4321 |
Observations:
- when all digits are sorted (from left to right) in a descending order then we have reached the largest number that has the listed property.
- in order to get the next permutation (the next number) we scan the digits from right to left and seek for the first pair of digits where the left digit in the pair is smaller than the right digit in the pair, replace the left digit of the pair with the smallest digit to its right that is larger than it, and then sort the digits to its right (after the swap) from smallest to largets.
#!/usr/bin/perl
use strict;
use warnings;
print join('', @{get_next([@ARGV])}),"\n";
sub get_next {
my ($permutation) = @_;
for (my $i=@$permutation-1; $i>0; --$i) {
if ($permutation->[$i]>$permutation->[$i-1]) {
swap($permutation,$i-1,swap_with($permutation,$i-1));
@$permutation[$i .. @$permutation-1] =
sort @$permutation[$i .. @$permutation-1];
return $permutation;
}
}
return [];
}
sub swap_with {
my ($array_ref,$index) = @_;
my $candidate=$index+1;
for (my $i=$index; $i<@$array_ref; ++$i) {
$candidate=$i
if ($array_ref->[$i]<$array_ref->[$candidate] and
$array_ref->[$i]>$array_ref->[$index]);
}
return $candidate;
}
sub swap {
my ($array_ref,$this_index,$that_index) = @_;
($array_ref->[$this_index],$array_ref->[$that_index]) =
($array_ref->[$that_index],$array_ref->[$this_index]);
}
Saturday, December 29, 2007
Sunday, December 23, 2007
Subscribe to:
Posts (Atom)