This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem Statement: | |
* Say you have an array for which the ith element is the price of a given stock on day i. | |
* If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. | |
* Note that you cannot sell a stock before you buy one. | |
* Example 1: | |
* Input: [7,1,5,3,6,4] | |
* Output: 5 | |
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6). Profit = 6–1 = 5. Not 7–1 = 6, as selling price needs to be larger than buying price. | |
* Example 2: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observable { | |
subscribe(observer: Observer): void; | |
unsubscribe(observer: Observer): void; | |
notify(): void; | |
} | |
interface Observer { | |
update(subject: Subject); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const score = new Score(); | |
const display = new DisplayScore(); | |
const cli = new CliScore(); | |
score.subscribe(display); | |
score.subscribe(cli); | |
score.updateScore(4); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisplayScore implements Observer { | |
update(subject: Score) { | |
console.log('Display score: ' + subject.getScore()); | |
} | |
} | |
class CliScore implements Observer { | |
update(subject: Score) { | |
console.log('CLI score: ' + subject.getScore()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Score extends Subject { | |
private score: number; | |
public updateScore(score: number) | |
{ | |
this.score = score; | |
console.log('got new score.....'); | |
this.notify(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Subject implements Observable { | |
private observers: Observer[] = []; | |
subscribe(observer: Observer) { | |
this.observers.push(observer); | |
} | |
unsubscribe(observer: Observer) { | |
this.observers = this.observers.filter((o) => { | |
return o !== observer | |
}) | |
console.log('observer detached...' + observer.constructor.name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observer { | |
update(subject: Subject); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observable { | |
subscribe(observer: Observer): void; | |
unsubscribe(observer: Observer): void; | |
notify(): void; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Str::macro('addressSplitter', function (string $address) { | |
$leadingHouseNumber = '/^([\d-]+[a-z]{0,1}[\d-]?[\d,]?)\ ([a-z\ ]+)$/mi'; | |
$endingWithHouseNumber = '/^([a-z\ ]+)([\d?]+)([\D+?]+[\d+?]?.+)?|([\D+?]+[\d+?]?+)$/mi'; | |
if (preg_match_all($leadingHouseNumber, $address, $matches, PREG_SET_ORDER, 0) | |
&& isset($matches[0][1], $matches[0][2]) | |
) { | |
$result['house_number'] = (int) $matches[0][1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* example code gist, | |
*/ | |
Model_A::whereHas('Model_B', | |
function (\Illuminate\Database\Eloquent\Builder $builder) { | |
$builder->upcoming()->where('cancel_reason', Model_B::REASON_NOT_AVAILABLE); | |
})->active(); |
NewerOlder