Merge pull request #1183 from kste/master

Hamming Weight/Bitsum Goodie
master
Zaahir Moolla 2015-10-15 10:15:46 -04:00
commit 9de09c69f4
2 changed files with 126 additions and 0 deletions

57
lib/DDG/Goodie/Bitsum.pm Normal file
View File

@ -0,0 +1,57 @@
package DDG::Goodie::Bitsum;
# ABSTRACT: Computes the Hamming weight of a hex or decimal number.
use DDG::Goodie;
use strict;
use Math::BigInt;
zci answer_type => "bitsum";
zci is_cached => 1;
name "Bitsum";
description "Computes the Hamming Weight / bit-wise sum of a decimal or hex number.";
primary_example_queries "bitsum 1023", "bitsum 0x789abcd";
secondary_example_queries "hammingweight 1023", "hw 1023";
category "programming";
topics "programming", "cryptography";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Bitsum.pm";
attribution github => ["kste", "Stefan Koelbl"],
twitter => "kste_";
# Triggers
triggers start => "bitsum", "hammingweight", "hw",
"bitsum of", "hammingweight of", "hw of",
"bitsum for", "hammingweight for", "hw for";
# Handle statement
handle remainder => sub {
# Return if input is no hex or decimal number
return unless $_ =~ /(^0x[0-9a-f]+$)|(^\d+$)/i;
my $input_number = $_;
my $hex_number;
my $binstring;
# Construct binary representation for both hex and decimal numbers
if( $input_number =~ /^0x/) {
$hex_number = substr($input_number, 2);
$binstring = unpack ('B*', pack ('H*',$hex_number));
} else {
$binstring = Math::BigInt->new($input_number)->as_bin();
}
# Count ones
my $result = $binstring =~ tr/1/1/;
return $result,
structured_answer => {
input => [html_enc($input_number)],
operation => 'Hamming Weight',
result => html_enc($result),
};
};
1;

69
t/Bitsum.t Normal file
View File

@ -0,0 +1,69 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "bitsum";
zci is_cached => 1;
ddg_goodie_test(
[qw( DDG::Goodie::Bitsum )],
'bitsum 127' => test_zci('7',
structured_answer => {
input => ['127'],
operation => 'Hamming Weight',
result => '7'
}),
'bitsum of 127' => test_zci('7',
structured_answer => {
input => ['127'],
operation => 'Hamming Weight',
result => '7'
}),
'bitsum for 0x1234' => test_zci('5',
structured_answer => {
input => ['0x1234'],
operation => 'Hamming Weight',
result => '5'
}),
'hammingweight 1024' => test_zci('1',
structured_answer => {
input => ['1024'],
operation => 'Hamming Weight',
result => '1'
}),
'hw 0xff' => test_zci('8',
structured_answer => {
input => ['0xff'],
operation => 'Hamming Weight',
result => '8'
}),
'hw for 0xaa' => test_zci('4',
structured_answer => {
input => ['0xaa'],
operation => 'Hamming Weight',
result => '4'
}),
# Long number tests
'hw 123456789123456789123456789' => test_zci('50',
structured_answer => {
input => ['123456789123456789123456789'],
operation => 'Hamming Weight',
result => '50'
}),
'bitsum 0x123456789ABCDEF123456789ABCDEF' => test_zci('64',
structured_answer => {
input => ['0x123456789ABCDEF123456789ABCDEF'],
operation => 'Hamming Weight',
result => '64'
}),
# Tests which should fail
'bitsum 213f3a', undef,
'hw 0d23238', undef,
'bitsum 0x' => undef,
'bitsum test' => undef,
);
done_testing;