small prototype
This commit is contained in:
commit
bc99731e62
12 changed files with 471764 additions and 0 deletions
15
.editorconfig
Normal file
15
.editorconfig
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.php]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.{yml,yaml,json}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/vendor/
|
||||
/.php-cs-fixer.cache
|
||||
28
.php-cs-fixer.dist.php
Normal file
28
.php-cs-fixer.dist.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PhpCsFixer\Config;
|
||||
use PhpCsFixer\Finder;
|
||||
|
||||
return (new Config())
|
||||
->setRiskyAllowed(false)
|
||||
->setRules([
|
||||
'@auto' => true
|
||||
])
|
||||
// 💡 by default, Fixer looks for `*.php` files excluding `./vendor/` - here, you can groom this config
|
||||
->setFinder(
|
||||
(new Finder())
|
||||
// 💡 root folder to check
|
||||
->in(__DIR__)
|
||||
// 💡 additional files, eg bin entry file
|
||||
// ->append([__DIR__.'/bin-entry-file'])
|
||||
// 💡 folders to exclude, if any
|
||||
// ->exclude([/* ... */])
|
||||
// 💡 path patterns to exclude, if any
|
||||
// ->notPath([/* ... */])
|
||||
// 💡 extra configs
|
||||
// ->ignoreDotFiles(false) // true by default in v3, false in v4 or future mode
|
||||
// ->ignoreVCS(true) // true by default
|
||||
)
|
||||
;
|
||||
466550
assets/words.txt
Normal file
466550
assets/words.txt
Normal file
File diff suppressed because it is too large
Load diff
9
autoload.php
Normal file
9
autoload.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
const ASSETS_DIR = './assets/';
|
||||
const BIN_DIR = './bin/';
|
||||
const SRC_DIR = './src/';
|
||||
|
||||
|
||||
44
bin/anagram-finder.php
Normal file
44
bin/anagram-finder.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
|
||||
const DICTIONARY = __DIR__ . '/../assets/words.txt';
|
||||
|
||||
echo "ANAGRAM FINDER\n";
|
||||
|
||||
$words = LazyCollection::make(function () {
|
||||
$fh = fopen(DICTIONARY, 'rb');
|
||||
try {
|
||||
while (($line = fgets($fh)) !== false) {
|
||||
yield rtrim($line, "\r\n");
|
||||
}
|
||||
} finally {
|
||||
fclose($fh);
|
||||
}
|
||||
});
|
||||
|
||||
function output($message)
|
||||
{
|
||||
$message = $message . PHP_EOL;
|
||||
print($message);
|
||||
flush();
|
||||
// ob_flush();
|
||||
}
|
||||
|
||||
|
||||
$anagramBase = readline("Enter the anagram: ");
|
||||
echo "\n";
|
||||
echo "Results for your anagram: " . $anagramBase . "\n";
|
||||
|
||||
$anagramChars = new Collection(explode($anagramBase, ""));
|
||||
echo $words
|
||||
->filter(fn (string $word) => ctype_alpha($word))
|
||||
->filter(fn (string $word) => strtolower($word) === $word)
|
||||
->map(fn (string $word) => new Collection(explode($word, "")))
|
||||
->filter(fn (Collection $wordChars) => $wordChars->count() === $anagramChars->intersect($wordChars)->count())
|
||||
->count();
|
||||
// ->map(fn (Collection $wordChars) => $wordChars->join(""))
|
||||
// ->each(fn (string $word) => output($word));
|
||||
16
bin/tests.php
Normal file
16
bin/tests.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
use Myrmidex\AnagramFinder\Matcher;
|
||||
|
||||
// are letters in word?
|
||||
$anagram = "eamstexil";
|
||||
$word = "steam";
|
||||
|
||||
assert(Matcher::check(anagram: $anagram, word: $word) === true);
|
||||
|
||||
$anagram = "eamstexil";
|
||||
$word = "xxx";
|
||||
|
||||
assert(Matcher::check(anagram: $anagram, word: $word) === false);
|
||||
25
composer.json
Normal file
25
composer.json
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "myrmidex/anagram-finder",
|
||||
"description": "An anagram finder in PHP",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"illuminate/collections": "^13.26"
|
||||
},
|
||||
"license": "AGPL-3.0-only",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Myrmidex\\AnagramFinder\\": "src/",
|
||||
"Myrmidex\\AnagramFinder\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "myrmidex",
|
||||
"email": "myrmidex@myrmidex.net"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^13",
|
||||
"friendsofphp/php-cs-fixer": "^3.95"
|
||||
}
|
||||
}
|
||||
5026
composer.lock
generated
Normal file
5026
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
19
shell.nix
Normal file
19
shell.nix
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
# PHP and tools
|
||||
php84
|
||||
php84Packages.composer
|
||||
|
||||
# Container tools
|
||||
podman
|
||||
podman-compose
|
||||
|
||||
# Utilities
|
||||
git
|
||||
curl
|
||||
gnumake
|
||||
];
|
||||
|
||||
}
|
||||
17
src/Matcher.php
Normal file
17
src/Matcher.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Myrmidex\AnagramFinder;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Matcher
|
||||
{
|
||||
public static function check(string $anagram, string $word): bool
|
||||
{
|
||||
$available = (new Collection(str_split($anagram)))->countBy();
|
||||
|
||||
return (new Collection(str_split($word)))
|
||||
->countBy()
|
||||
->every(fn (int $needed, string $letter) => $available->get($letter, 0) >= $needed);
|
||||
}
|
||||
}
|
||||
13
tests/Unit/MatcherTest.php
Normal file
13
tests/Unit/MatcherTest.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Myrmidex\AnagramFinder\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MatcherTest extends TestCase
|
||||
{
|
||||
public function testCheck()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue