function deCode_Me($strContent, $cKey){
$hRet = 0;
$sRet = "";
foreach( str_split($strContent) as $nKey ){
$sRet .= chr(ord($nKey) ^ ord($cKey[$hRet++ % strlen($cKey)]));
}
return $sRet;
}
How can I make the encoder for the above resolver.
the exact opposite?
function encode_Me($strContent, $cKey)
{
$key = $cKey;
$text = $strContent;
$outText = '';
for ($i = 0; $i < strlen($text); $i++)
{
for ($j = 0; $j < strlen($key); $j++, $i++)
{
$outText.= $text
{
$i} ^ $key
{
$j};
}
}
return $outText;
}
worked ! :)
Related
I have a form in my website. I am reviewing the form values on ui panel. Function is doing if the the word length is greater than 12 it puts a space to next to it. But when I print the value I am getting error if value is utf8.
$text= 'üğqwoweğofkeiasş övafevpğeüqrg qğekqrğofteölzfs';
function parser($str, $parse) {
$strlength = strlen($str);
$counter = 0;
$query = '';
if($strlength > $parse) {
for($i = 0; $i < $strlength; $i++) {
if($str[$i] != ' ') {
$counter++;
}
if($counter == $parse) {
$query.=$str[$i];
$query.=' ';
$counter = 0;
}
if($counter != $parse) {
$query.=$str[$i];
}
if($counter != $parse & $str[$i] == ' ') {
$counter = 0;
}
}
return $query;
}
else {
return $str;
}
}
echo parser($text,12);
Output is:
'üğqwoweğo ofkeiasş övafevpğe� üqrg qğekqrğoft teölzfs'
and it's not happening all the times just sometimes; I can't understand why is that.
Code:
function parser($string, $max_length = 12)
{
$chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
$i = 0;
foreach ($chars as $index => $char)
{
if ($char === ' ') { $i = 0; }
else { $i++; }
if ($i >= $max_length)
{
$chars[$index] = $char . ' ';
$i = 0;
}
}
return implode('', $chars);
}
$result = parser('üğqwoweğofkeiasş övafevpğeüqrg qğekqrğofteölzfs');
result: üğqwoweğofke iasş övafevpğeüqr g qğekqrğofteö lzfsuser
http://sandbox.onlinephpfunctions.com/code/971f4a6c9dfe80ec6277dd89653ea160af3c68e4
I can't find solution .. Please, help to return changed array
Code
<?php
function bell_sort($arr, $head, $tail, $queue) {
if($head != $tail){
$min = $arr[$head];
$min_index = $head;
for($i = $head; $i <= $tail; ++$i){
if($arr[$i] < $min){
$min = $arr[$i];
$min_index = $i;
}
}
if($queue){
$tmp = $arr[$head];
$arr[$head] = $arr[$min_index];
$arr[$min_index] = $tmp;
$head++;
}else{
$tmp = $arr[$tail];
$arr[$tail] = $arr[$min_index];
$arr[$min_index] = $tmp;
$tail--;
}
bell_sort($arr, $head, $tail, !$queue);
}else{
return $arr;
}
}
$n = 10;
for($i = 0; $i < $n; ++$i){
$arr[$i] = rand(0, 100);
}
$head = 0;
$tail = count($arr) - 1;
$queue = 1;
$new_arr = bell_sort($arr, $head, $tail, $queue);
var_dump($new_arr);
You're missing a return statement in one of the recursive paths in the bell_sort function.
if($queue){
$tmp = $arr[$head];
$arr[$head] = $arr[$min_index];
$arr[$min_index] = $tmp;
$head++;
}else{
$tmp = $arr[$tail];
$arr[$tail] = $arr[$min_index];
$arr[$min_index] = $tmp;
$tail--;
}
bell_sort($arr, $head, $tail, !$queue);
Change the line above line to
return bell_sort($arr, $head, $tail, !$queue);
and it will work
Below is my Code to Reverse a String..
The code runs well but I need to wrap this code inside Paramaterized function
in which user pass a string inside function and get return output.
<?php
$string = trim("This");
$len =strlen($string);
$stringExp = str_split($string);
for ($i = $len-1; $i >=0;$i--)
{
echo $stringExp[$i];
}
?>
for Ex -
I want above string reversal code logic like below function...
<?php
$str = "rahul";
echo reverse($str);
function reverse($str)
{
for ($i = 0, $j = strlen($str) - 1; $i < $j; $i++, $j--) {
$tmp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $tmp;
}
return $str;
}
?>
Simply try this
$str = "rahul";
echo reverse($str);
function reverse($str)
{
$tmp = '';
for($i = (strlen($str)-1);$i >= 0; $i--) {
$tmp .= $str[$i];
}
return $tmp;
}
There is strev() function which does it but if you need write your own here is the code
$str = "abcde";
function reverse ($str)
{
$output = '';
for ($i = strlen($str)-1; $i >= 0 ; --$i) {
$output .= $str[$i];
}
return $output;
}
echo reverse($str);
Im trying to solve one challenge where you have to check all string substrings are they anagrams. The condition is basically For S=abba, anagramic pairs are: {S[1,1],S[4,4]}, {S[1,2],S[3,4]}, {S[2,2],S[3,3]} and {S[1,3],S[2,4]}
Problem is that I have string with 100 chars and execution time should be below 9 secs. My time is around 50 secs... Below is my code, I will appreciate any advice - if you give me only directions or pseudo code it is even better.
$time1 = microtime(true);
$string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs';
$arr = [];
$len = strlen($string);
for ($i = 0; $i < strlen($string); $i++) {
if ($i === 0) {
for ($j = 1; $j <= $len - 1; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
} else {
for ($j = 1; $j <= $len - $i; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
}
}
$br = 0;
$arrLength = count($arr);
foreach ($arr as $key => $val) {
if ($key === count($arr) - 1) {
break;
}
for ($k = $key + 1; $k < $arrLength; $k++) {
if (is_anagram($val, $arr[$k]) === true) {
$br++;
}
}
}
echo $br."</br>";
function is_anagram($a, $b)
{
$result = (count_chars($a, 1) == count_chars($b, 1));
return $result;
}
$time2 = microtime(true);
echo "Script execution time: ".($time2-$time1);
Edit:
Hi again, today I had some time so I tried to optimize but couldnt crack this... This is my new code but I think it got worse. Any advanced suggestions ?
<?php
$string = 'abdcasdabvdvafsgfdsvafdsafewsrgsdcasfsdfgxccafdsgccafsdgsdcascdsfsdfsdgfadasdgsdfawdascsdsasdasgsdfs';
$arr = [];
$len = strlen($string);
for ($i = 0; $i < strlen($string); $i++) {
if ($i === 0) {
for ($j = 1; $j <= $len - 1; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
} else {
for ($j = 1; $j <= $len - $i; $j++) {
$push = substr($string, $i, $j);
array_push($arr, $push);
}
}
}
$br = 0;
$arrlen = count ($arr);
foreach ($arr as $key => $val) {
if (($key === $arrlen - 1)) {
break;
}
for ($k = $key + 1; $k < $arrlen; $k++) {
$result = stringsCompare($val,$arr[$k]);
if ($result === true)
{
$br++;
}
}
echo $br."\n";
}
function stringsCompare($a,$b)
{
$lenOne = strlen($a);
$lenTwo = strlen ($b);
if ($lenOne !== $lenTwo)
{
return false;
}
else {
$fail = 0;
if ($lenOne === 1) {
if ($a === $b) {
return true;
}
else
{
return false;
}
}
else
{
for ($x = 0; $x < $lenOne; $x++)
{
$position = strpos($b,$a[$x]);
if($position === false)
{
$fail = 1;
break;
}
else
{
$b[$position] = 0;
$fail = 0;
}
}
if ($fail === 1)
{
return false;
}
else
{
return true;
}
}
}
}
?>
You should think of another rule that all anagrams of a certain string can meet. For example, something about the number of occurrences of each character.
I want to create an array from an input string. Before this code, I've tried explode, but the array remains length 1. Each string that I've tried is still one in array[0]. Here's my code so far:
public function word()
{
$kata = array($this->kal->getHasil());
if (!empty($kata)) {
$n = count($kata)
for ($i = 0; $i < $n; $i++) {
$imin = $i;
for ($j = $i; $j < $n; $j++) {
if ($kata[$j] < $kata[$imin]) {
$imin = $j;
}
}
$temp = $kata[$i];
$kata[$i] = $kata[$imin];
$kata[$imin] = $temp;
}
for ($i = 0; $i < $n; $i++) {
echo "$kata[$i] ";
}
}
}
public function tokenize()
{
$temp = $this->kal->getHasil();
$token = explode(" ", $temp);
return $token;
}
$hasil = $pp->tokenize();
for ($i = 0; $i < sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
How would I fix this?
You can use a globar var, see the code:
public function word(){ $kata = array($this->kal->getHasil());
global $output;
if(!empty($kata)){
$ar= count($kata)
$output += $ar;
}
public function tokenize() {
$temp = $this->kal->getHasil();
$token = explode(" ",$temp);
return $token;
}
$output = 0;
$hasil = $pp->tokenize();
for($i=0; $i<sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
echo $output;