Berbagi Artikel-Artikel Unik Dan Bermutu

Blog Archive

Copyright © 2015 Artikel TopNews | . Powered by Blogger.

Labels

ads3

tag

Top Artikel

ads2

ads

iklan

Membuat anagram dengan PHP

udah tau anagram kan ? klo blum tw baca aja nih di sini
skarang ke script nya


<?php

class anagrams {

private $anagram_string;

/**
*
* @set the string
*
* @access public
*
* @param string $string
*
*/
public function setAnagram($string){
$this->anagram_string = $this->sanitizeString($string);
}

/**
*
* @remove any nasties
*
* @access private
*
* @return string
*
*/
private function sanitizeString($string){
return str_replace(' ', '', strtolower($string));
}

/**
*
* @filter our by first letter
*
* @access public
*
* @param string $char
*
* @return array
*
*/
public function filterFirstLetter($char){
$return_array = array();
$array = $this->createAnagrams();
foreach($array as $word)
{
if(substr($word, 0, 1) == $char)
{
array_push($return_array, $word);
}
}
return $return_array;
}

/**
*
* @create the anangrams
*
* @access public
*
* @return array
*
*/
function createAnagrams(){

/*** split the string into chars ***/
$input = str_split($this->anagram_string);

/*** output array(s) ***/
$output=array( array() );

/*** length of input ***/
$inputLength = sizeof($input);

for($scan=0; $scan<$inputLength; $scan++)
{
$pickLatest = array();
$outputLength = sizeof($output);

for($out=0; $out<$outputLength; $out++)
{
/*** add an input char to each entry ***/
array_push($output[$out], $input[$scan]);
$pickLatest[$out] = $output[$out];
}
$pickLatestLength = sizeof($pickLatest);
for($a=0; $a<$pickLatestLength; $a++)
{
for($x=sizeof($pickLatest[$a])-1; $x>0; $x--)
{
$swap = $pickLatest[$a][$x-1];
$pickLatest[$a][$x-1] = $pickLatest[$a][$x];
$pickLatest[$a][$x] = $swap;
if(!in_array($pickLatest[$a], $output))
{
$output[] = $pickLatest[$a];
}
}
}
}

return $this->sortAnagrams($output);
}

/**
*
* @sort array of letters into words and
* only return unique values (no duplicates)
*
* @access private
*
* @param array $array
*
* @return array
*
*/
private function sortAnagrams($array){
$word_array = array();
foreach($array as $letters)
{
$word = implode('', $letters);
if(!in_array($word, $word_array))
{
$ret[] = $word;
}
}
return $ret;
}

} /*** end of class ***/

/** example implentation **/

/*** a new anagrams object ***/
$anagrams = new anagrams;
/*** set the letters for the anagram ***/
$anagrams->setAnagram('rieqy');
/*** get only anagrams beginnig with the letter p ***/
$anagram_array = $anagrams->filterFirstLetter('p');
/*** loop over the array of words found ***/
echo 'found '.sizeof($anagram_array)." anagrams \n";
foreach($anagram_array as $anagram )
{
echo $anagram,"\n";
}
echo "\n";

/*** get all the anagrams ***/

/*** set the letters for the anagram ***/
$anagram_array = $anagrams->createAnagrams();
/*** number of anagrams ***/
echo 'found '.sizeof($anagram_array)." anagrams\n";
/*** loop over the array of words found ***/
foreach($anagram_array as $anagram )
{
echo $anagram,"\n";
}
?>
pada bagian
$anagrams->setAnagram('rieqy');
ganti 'rieqy' menjadi kata yang akan diproses menjadi anagram
setelah ane eksekusi, maka output nya kya gini :

rieqy
ireqy
reiqy
eriqy
ierqy
eirqy
riqey
rqiey
qriey
irqey
iqrey
qirey
reqiy
rqeiy
qreiy
erqiy
eqriy
qeriy
ieqry
iqery
qiery
eiqry
eqiry
qeiry
rieyq
riyeq
ryieq
yrieq
ireyq
iryeq
iyreq
yireq
reiyq
reyiq
ryeiq
yreiq
eriyq
eryiq
eyriq
yeriq
ieryq
ieyrq
iyerq
yierq
eiryq
eiyrq
eyirq
yeirq
riqye
riyqe
ryiqe
yriqe
rqiye
rqyie
ryqie
yrqie
qriye
qryie
qyrie
yqrie
irqye
iryqe
iyrqe
yirqe
iqrye
iqyre
iyqre
yiqre
qirye
qiyre
qyire
yqire
reqyi
reyqi
ryeqi
yreqi
rqeyi
rqyei
ryqei
yrqei
qreyi
qryei
qyrei
yqrei
erqyi
eryqi
eyrqi
yerqi
eqryi
eqyri
eyqri
yeqri
qeryi
qeyri
qyeri
yqeri
ieqyr
ieyqr
iyeqr
yieqr
iqeyr
iqyer
iyqer
yiqer
qieyr
qiyer
qyier
yqier
eiqyr
eiyqr
eyiqr
yeiqr
eqiyr
eqyir
eyqir
yeqir
qeiyr
qeyir
qyeir
yqeir
Tag : PHP, Programming
0 Komentar untuk "Membuat anagram dengan PHP"
Back To Top