final class Str { public static function format(string $str): string { // Could be refactor with strlen or str_contains before return preg_replace("/[^a-z0-9 ]+/", "", trim(strtolower($str))); } } final class Arr { public static function toArray(string $str): array { return str_split($str); } } final class Anagram { public function __construct( private string $str1, private string $str2, ) {} public function __invoke(): bool { $formatted1 = Str::format($this->str1); $formatted2 = Str::format($this->str2); $split1 = Arr::toArray($formatted1); $split2 = Arr::toArray($formatted2); sort($split1); sort($split2); // Could be refactor with array_diff or count return $split1 === $split2; } } function isAnagram( string $str1, string $str2 ): bool { return (new Anagram($str1, $str2))(); }
function isAnagram($str1, $str2) {return false- final class Str
- {
- public static function format(string $str): string
- {
- // Could be refactor with strlen or str_contains before
- return preg_replace("/[^a-z0-9 ]+/", "", trim(strtolower($str)));
- }
- }
- final class Arr
- {
- public static function toArray(string $str): array
- {
- return str_split($str);
- }
- }
- final class Anagram
- {
- public function __construct(
- private string $str1,
- private string $str2,
- ) {}
- public function __invoke(): bool
- {
- $formatted1 = Str::format($this->str1);
- $formatted2 = Str::format($this->str2);
- $split1 = Arr::toArray($formatted1);
- $split2 = Arr::toArray($formatted2);
- sort($split1);
- sort($split2);
- // Could be refactor with array_diff or count
- return $split1 === $split2;
- }
- }
- function isAnagram(
- string $str1,
- string $str2
- ): bool {
- return (new Anagram($str1, $str2))();
- }
// Could implement a contract final class FrequencyWordProcessor { private const SENTENCE_PATTERN = '/[\s!?,]/'; private const MIN_WORD_LENGTH = 3; public function __construct( private string $sentence, private int $maxItemsOutput, ) {} public function __invoke(): array { $strWithoutExtraCharacters = strtolower( $this->sentence ); $words = explode(" ", $strWithoutExtraCharacters); $filteredWords = array_filter( $words, fn (string $word) => self::MIN_WORD_LENGTH < strlen($word) ); $occurenceWords = []; foreach ($filteredWords as $word) { $replacedWord = preg_replace(self::SENTENCE_PATTERN, '', $word); if (!isset($occurenceWords[$replacedWord])) { $occurenceWords[$replacedWord] = 1; continue; } $occurenceWords[$replacedWord]++; } arsort($occurenceWords); return array_slice($occurenceWords, 0, $this->maxItemsOutput); } } function getTopNFrequentWords(string $sentence, int $maxItemsOutput): array { return (new FrequencyWordProcessor($sentence, $maxItemsOutput))(); }
function getTopNFrequentWords($text, $n) {return 0;- // Could implement a contract
- final class FrequencyWordProcessor
- {
- private const SENTENCE_PATTERN = '/[\s!?,]/';
- private const MIN_WORD_LENGTH = 3;
- public function __construct(
- private string $sentence,
- private int $maxItemsOutput,
- ) {}
- public function __invoke(): array
- {
- $strWithoutExtraCharacters = strtolower(
- $this->sentence
- );
- $words = explode(" ", $strWithoutExtraCharacters);
- $filteredWords = array_filter(
- $words,
- fn (string $word) => self::MIN_WORD_LENGTH < strlen($word)
- );
- $occurenceWords = [];
- foreach ($filteredWords as $word) {
- $replacedWord = preg_replace(self::SENTENCE_PATTERN, '', $word);
- if (!isset($occurenceWords[$replacedWord])) {
- $occurenceWords[$replacedWord] = 1;
- continue;
- }
- $occurenceWords[$replacedWord]++;
- }
- arsort($occurenceWords);
- return array_slice($occurenceWords, 0, $this->maxItemsOutput);
- }
- }
- function getTopNFrequentWords(string $sentence, int $maxItemsOutput): array {
- return (new FrequencyWordProcessor($sentence, $maxItemsOutput))();
- }
// Could implement a contract final class Closest { private int $closest = 0; public function __construct( private array $nbrs ) {} public function __invoke(): int { if (empty($this->nbrs) or in_array(0, $this->nbrs)) { return 0; } foreach ($this->nbrs as $nbr) { if (!is_int($nbr)) { continue; } if ( 0 === $this->closest || abs($nbr) < abs($this->closest) // e.g. 2 < 3 || (abs($nbr) === abs($this->closest) && $nbr > $this->closest) // e.g. 2 === 2 && 2 > -2 ) { $this->closest = $nbr; } } return $this->closest; } } function closestToZero(array $nbrs): int { return (new Closest($nbrs))(); }
function closestToZero(array $ints) {// ici vous pouvez mettre votre codereturn 0;- // Could implement a contract
- final class Closest
- {
- private int $closest = 0;
- public function __construct(
- private array $nbrs
- ) {}
- public function __invoke(): int
- {
- if (empty($this->nbrs) or in_array(0, $this->nbrs)) {
- return 0;
- }
- foreach ($this->nbrs as $nbr) {
- if (!is_int($nbr)) {
- continue;
- }
- if (
- 0 === $this->closest
- || abs($nbr) < abs($this->closest) // e.g. 2 < 3
- || (abs($nbr) === abs($this->closest) && $nbr > $this->closest) // e.g. 2 === 2 && 2 > -2
- ) {
- $this->closest = $nbr;
- }
- }
- return $this->closest;
- }
- }
?>- function closestToZero(array $nbrs): int {
- return (new Closest($nbrs))();
- }