-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIB_2.php
More file actions
225 lines (186 loc) · 5.53 KB
/
IB_2.php
File metadata and controls
225 lines (186 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* A code sample which converts odd integers to word strings and returns the sum of the common factors of even numbers.
*
* The input is assumed to be a random list of integers. For the sum of the common factors of even numbers
* only positive values are taken into consideration
*/
error_reporting(E_ALL);
function exception_handler($exception) {
echo get_class($exception). ' : ' . $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
//start
$iB = new IntelligentBee(array(-2,6,12, 7, 101, 67));
$iB->processOddNumbers();
$sum = $iB->processEvenNumbers();
echo PHP_EOL.'Sum of common factors: ' . $sum . PHP_EOL;
//end
class IntelligentBee{
const OneHundred = 100;
const OneThousand = 1000;
const OneMillion = 1000000;
const OneBillion = 1000000000;
protected $minCommonFactors = [];
protected $oddNumbers = [];
protected $evenNumbers = [];
protected $startInt;
protected $endInt;
protected static $dictionary = array(
0=>'zero',
1=>'one',
2=>'two',
3=>'three',
4=>'four',
5=>'five',
6=>'six',
7=>'seven',
8=>'eight',
9=>'nine',
10=>'ten',
11=>'eleven',
12=>'twelve',
13=>'thirteen',
14=>'fourteen',
15=>'fifthteen',
16=>'sixteen',
17=>'seventeen',
18=>'eighteen',
19=>'nineteen',
20=>'twenty',
30=>'thirty',
40=>'forty',
50=>'fifty',
60=>'sixty',
70=>'seventy',
80=>'eighty',
90=>'ninety',
self::OneHundred=>'hundred',
self::OneThousand=>'thousand',
self::OneMillion=>'million',
self::OneBillion=>'billion'//might get into memory issues
);
public function __construct(array $arrayOfIntegers){
$startInt = min($arrayOfIntegers);
$endInt = max($arrayOfIntegers);
if(abs($startInt) > self::OneBillion || abs($startInt) > self::OneBillion){
throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt));
}
if(abs($endInt) > self::OneBillion || abs($endInt) > self::OneBillion){
throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt));
}
$this->startInt = $startInt;
$this->endInt = $endInt;
//split it into 2 arrays, one for odd numbers and one for even numbers to make it easier to perform each task
$this->splitNumbers($arrayOfIntegers);
}
public function processOddNumbers()
{
foreach($this->oddNumbers as $oddNumber){
echo $this->convertIntToWord($oddNumber).PHP_EOL;
}
}
public function processEvenNumbers()
{
$commonFactors = $this->calcFactors($this->getClosestEvenToZero($this->startInt, $this->endInt, $this->evenNumbers));
foreach($this->evenNumbers as $evenNumber){
$commonFactors = $this->checkIfHasCommonFactors($evenNumber, $commonFactors);
}
return array_sum($commonFactors);
}
protected function splitNumbers(array $arrayOfIntegers){
foreach($arrayOfIntegers as $int){
if($int & 1){
//odd number
$this->oddNumbers[] = $int;
}else{
//even number
$this->evenNumbers[] = $int;
}
}
sort($this->evenNumbers);//sort the even numbers as it helps calculate the closest number to 0
}
protected function getClosestEvenToZero($startInt, $endInt, $evenNumbers){
$dist = abs($evenNumbers[0]);
$idx = 0;
$totalNo = count($evenNumbers);
for($i=0;$i<$totalNo;$i++){
$newDist = abs($evenNumbers[$i]);
if($newDist < $dist){
$idx = $i;
$dist = $newDist;
}
if($dist == 0){//only if evenNumbers is sorted
return $evenNumbers[$idx];
}
}
return $evenNumbers[$idx];
}
protected function checkIfHasCommonFactors($int, array $minCommonFactors){
$int = abs($int);
$cF = $this->calcFactors($int);
return array_intersect($cF, $minCommonFactors);
}
private function calcFactors($int)
{
$int = abs($int);
$cF = [];
$max = $int/2;
for($i=1;$i<=$max;$i++){
if(0 === $int%$i){
$cF[] = $i;
}
}
$cF[] = $int;
return $cF;
}
protected function convertIntToWord($int)
{
$word = '';
if($int < 0){
$word .= 'minus ' . $this->convertIntToWord(abs($int));
}
if(0 <= $int && $int < 20){
return self::$dictionary[$int];
}
if(20 <= $int && $int < self::OneHundred){
return $this->processTens($int);
}
if (self::OneHundred <= $int && $int < self::OneThousand)
{
return $this->processHundreds($int);
}
if(self::OneThousand <= $int && $int < self::OneMillion){
return $this->processBigNumber($int, self::OneThousand);
}
if(self::OneMillion <= $int && $int < self::OneBillion){
return $this->processBigNumber($int, self::OneMillion);
}else{
return $this->processBigNumber($int, self::OneBillion);
}
}
protected function processTens($int)
{
$tens = intval($int/10)*10;
$units = $int%10;
$conv = self::$dictionary[$tens];
$conv .= $units > 0 ? '-'.self::$dictionary[$units] : '';
return $conv;
}
protected function processHundreds($int)
{
$hundreds = intval($int/100);
$remainder = $int%100;
$conv = self::$dictionary[$hundreds] . ' ' . self::$dictionary[self::OneHundred];
$conv .= $remainder > 0 ? " and " . $this->convertIntToWord($remainder) : '';
return $conv;
}
protected function processBigNumber($int, $baseUnit)
{
$nrBaseUnits = intval($int/$baseUnit);
$remainder = $int%$baseUnit;
$conv = $this->convertIntToWord($nrBaseUnits) . ' ' . self::$dictionary[$baseUnit];
$conv .= $remainder <= 0 ? "" : ($remainder < 100 ? " and " : ", ") . $this->convertIntToWord($remainder);
return $conv;
}
}