Merge branch 'pr/189'
commit
2901aef653
|
@ -0,0 +1,78 @@
|
|||
package DDG::Goodie::ReverseResistorColours;
|
||||
#ABSTRACT: Do the reverse of what ResistorColors.pm does.
|
||||
|
||||
use DDG::Goodie;
|
||||
use utf8;
|
||||
|
||||
triggers query_raw => qr/^resistors?|resistors?$/i;
|
||||
|
||||
zci is_cached => 1;
|
||||
zci answer_type => 'ohms';
|
||||
|
||||
primary_example_queries 'red yellow white gold resistor';
|
||||
secondary_example_queries 'resistor red yellow white';
|
||||
description 'find resistance based on colour bands';
|
||||
name 'ReverseResistorColours';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/lib/DDG/Goodie/ReverseResistorColours.pm';
|
||||
category 'reference';
|
||||
topics 'science';
|
||||
|
||||
my %coloursToDigits = (
|
||||
'black' => { value => 0, tolerance => '' },
|
||||
'brown' => { value => 1, tolerance => 1, },
|
||||
'red' => { value => 2, tolerance => 2 },
|
||||
'orange' => { value => 3, tolerance => '' },
|
||||
'yellow' => { value => 4, tolerance => 5 },
|
||||
'green' => { value => 5, tolerance => .5 },
|
||||
'blue' => { value => 6, tolerance => .25 },
|
||||
'violet' => { value => 7, tolerance => .1 },
|
||||
'grey' => { value => 8, tolerance => .05 },
|
||||
'white' => { value => 9, tolerance => '' },
|
||||
'gold' => { value => '', tolerance => 5 },
|
||||
'silver' => { value => '', tolerance => 10 },
|
||||
);
|
||||
|
||||
handle query_raw => sub {
|
||||
s/gray/grey/g; #learn how to spell 'grey' right
|
||||
s/purple/violet/g; #because we can say both
|
||||
s/ ?resistors? ?//;
|
||||
my $count = 0;
|
||||
my $resistance = 0;
|
||||
my @colours = (split(' ', $_));
|
||||
my $marginE = 0;
|
||||
my $append = '';
|
||||
if ((scalar(@colours) != 3) && (scalar(@colours) != 4)) { return };
|
||||
foreach my $colour (@colours) {
|
||||
if (!exists $coloursToDigits{$colour}) { return };
|
||||
}
|
||||
$resistance += ($coloursToDigits{$colours[0]}{value} * 10);
|
||||
$resistance += $coloursToDigits{$colours[1]}{value};
|
||||
for (my $i = 0; $i < $coloursToDigits{$colours[2]}{value}; $i++) {
|
||||
$resistance = $resistance * 10;
|
||||
}
|
||||
my $mult = $coloursToDigits{$colours[2]}{value};
|
||||
if (exists $colours[3]) {
|
||||
$marginE = $coloursToDigits{$colours[3]}{tolerance};
|
||||
} else {
|
||||
$marginE = 20;
|
||||
}
|
||||
if ($resistance > 1000 && $resistance < 999999) {
|
||||
$resistance = $resistance / 1000;
|
||||
$append = 'k';
|
||||
}
|
||||
if ($resistance > 1000000 && $resistance < 999999999) {
|
||||
$resistance = $resistance / 1000000;
|
||||
$append = 'M';
|
||||
}
|
||||
if ($resistance > 1000000000) {
|
||||
$resistance = $resistance / 1000000000;
|
||||
$append = "G";
|
||||
}
|
||||
#U+2126 is the ohm symbol, U+00B1 is the plus-minus sign.
|
||||
my $answer = "A $_ resistor has a resistance of $resistance $append\x{2126} \x{00B1} $marginE\%.";
|
||||
my $source = '<a href="https://en.wikipedia.org/wiki/Electronic_color_code">More at Wikipedia</a>';
|
||||
|
||||
return $answer, html => "$answer $source";
|
||||
};
|
||||
|
||||
1;
|
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Test::More;
|
||||
use DDG::Test::Goodie;
|
||||
use utf8;
|
||||
|
||||
zci is_cached => 1;
|
||||
zci answer_type => 'ohms';
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(
|
||||
DDG::Goodie::ReverseResistorColours
|
||||
)],
|
||||
|
||||
'black green red resistor'
|
||||
=> test_zci('A black green red resistor has a resistance of 500 Ω ± 20%.',
|
||||
html => 'A black green red resistor has a resistance of 500 Ω ± 20%. '
|
||||
. '<a href="https://en.wikipedia.org/wiki/Electronic_color_code">More at Wikipedia</a>'),
|
||||
'red orange yellow gold resistor'
|
||||
=> test_zci('A red orange yellow gold resistor has a resistance of 230 kΩ ± 5%.',
|
||||
html => 'A red orange yellow gold resistor has a resistance of 230 kΩ ± 5%. '
|
||||
. '<a href="https://en.wikipedia.org/wiki/Electronic_color_code">More at Wikipedia</a>'),
|
||||
'resistor yellow blue purple'
|
||||
=> test_zci('A yellow blue violet resistor has a resistance of 460 MΩ ± 20%.',
|
||||
html => 'A yellow blue violet resistor has a resistance of 460 MΩ ± 20%. '
|
||||
. '<a href="https://en.wikipedia.org/wiki/Electronic_color_code">More at Wikipedia</a>'),
|
||||
|
||||
'resistor yellow green' => undef,
|
||||
'resistor red orange blue red green' => undef,
|
||||
'resistor red banana orangutan' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
Loading…
Reference in New Issue