Last active
June 10, 2020 12:20
-
-
Save ob-ivan/8b6222c4926742d15e053b559ff63a76 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/php | |
<?php | |
interface IFilter { | |
public function bAccept($sSequence); | |
} | |
class CFilterRegExp implements IFilter { | |
private $sRegExp; | |
public function __construct($sRegExp) | |
{ | |
$this->sRegExp = $sRegExp; | |
} | |
public function bAccept($sSequence) { | |
return preg_match($this->sRegExp, $sSequence) > 0; | |
} | |
} | |
class CRead { | |
private $m_iSequenceLength; | |
private $m_iOutputCountMax; | |
private $m_oFilter; | |
private $m_asiSequenceCount = []; | |
public function __construct($iSequenceLength, $iOutputCount, IFilter $oFilter) | |
{ | |
$this->m_iSequenceLength = $iSequenceLength; | |
$this->m_iOutputCountMax = $iOutputCount; | |
$this->m_oFilter = $oFilter; | |
} | |
public function vReadFile($rFile) { | |
while (($sLine = fread($rFile, 1024)) !== FALSE) { | |
for ($iPosition = 0, $iLineLength = mb_strlen($sLine); $iPosition < $iLineLength; ++$iPosition) { | |
$sSequence = mb_substr($sLine, $iPosition, $this->m_iSequenceLength); | |
if ($this->m_oFilter->bAccept($sSequence) !== TRUE) { | |
continue; | |
} | |
if (!isset($this->m_asiSequenceCount[$sSequence])) { | |
$this->m_asiSequenceCount[$sSequence] = 0; | |
} | |
$this->m_asiSequenceCount[$sSequence]++; | |
} | |
if (feof($rFile)) { | |
break; | |
} | |
} | |
} | |
public function tsGetReport() { | |
arsort($this->m_asiSequenceCount, SORT_NUMERIC); | |
$iOutputCount = 0; | |
foreach ($this->m_asiSequenceCount as $sSequence => $iCount) { | |
yield $sSequence . ' ' . $iCount; | |
++$iOutputCount; | |
if ($this->m_iOutputCountMax <= $iOutputCount) { | |
break; | |
} | |
} | |
} | |
} | |
$oRead = new CRead(3, 10, new CFilterRegExp('~^\w+$~')); | |
$oRead->vReadFile(STDIN); | |
foreach ($oRead->tsGetReport() as $sReport) { | |
echo $sReport . PHP_EOL; | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment