[HAF] Challange for a PHP Developer

Status
Not open for further replies.

lucky.sparks

Elite Member
Executive VIP
Jr. VIP
Joined
Oct 6, 2020
Messages
2,626
Reaction score
9,931
I need to improve a function of mine and the task is interesting enough to be posted under HAF as a challenge to see who can do it.

The task is to make a replacement of a random char in the string with its replacement only once and replacement selection must be randomized.


$input = 'bhw is the most popular marketing forum out there.'; $map = [ 'i' => '1', 'o' => '0', 'b' => '3', ' ' => '+' ]; $skip = ['bhw'];


So approx steps should be:
- we take a random key/value pair from $map
- we find if there is a match to key in the string, if not, take another key/value pair
- we find a random word where we will make the replacement but make sure its not listed in $skip
- we do the replacement of a single char and return the string

Results that must be produced every time calling the function on the string:
//bhw 1s the most popular marketing forum out there.
//bhw is the most popular market1ng forum out there.
//bhw is the m0st popular marketing forum out there.
//bhw is the most popular marketing forum 0ut there.
//bhw is the most+popular marketing forum out there.


See how the replacement is randomized and done only once and skipping bhw keyword?

I don't want the solution to be long & ugly. :) If you can't make it impressive and beautiful, please do not apply.

The solution should be an easy drop-in function.

Budget - $30 and my thanks. If it's too low, state your own budget.

Happy to pay with paypal, payoneer or whatever.

I do not use skype and please - only Jr. VIP members reach out to me, I will ignore people without the ability to sell on forums.
 
Doubling the bounty, so now it's $60. :D
 
Are you trying to make a god dem spintax parser in a really complicated way?
 
Are you trying to make a god dem spintax parser in a really complicated way?
Even with using spintax(heavily, multiple levels, every word spun) - at the current rate of sending - I get caught into antispam filters from time to time so there are some tricks I use to avoid that.
 
Even with using spintax(heavily, multiple levels, every word spun) - at the current rate of sending - I get caught into antispam filters from time to time so there are some tricks I use to avoid that.
but tham the message will look really spammy to those who receive it
 
homoglyphs may be a solution here.
gordon-ramsey-ssh.gif


Sorry, had to write my own ugly solution for this task in python
It's beautiful! And it seems to work exactly as per requirements, nice work! Now Im sad that my project base is written in PHP :D
 
Two functions. Copy pasted one to sort out the problem with strpos returning the first match. Kept all the comments.
Split up everything into a char array. Identify where the skip words are and watch out not to touch the chars on those indexes.
Could do a little better on the randomness with selecting the winning replacement, but it's 3:30 AM
<?php

$input = 'bhw is the most popular marketing forum out there. bhw!!';

$map = [
'i' => '1',
'o' => '0',
'b' => '3',
' ' => '+'
];

$skip = ['bhw', 'the'];

function luckyReplacement ($input, $map, $skip) {


// we split up the string to chars
$StringToChars = str_split($input);

foreach($skip as $skip_string) {
// woreach skip string we find where whe chars start

$skip_string_char_lenght = strlen($skip_string);
$skip_chars_positions = strpos_recursive($input, $skip_string);

foreach($skip_chars_positions as $skip_chars_positions) {
// now that we know where the start positions are, lets write them down
//var_dump($skip_chars_positions);
foreach (range($skip_chars_positions, $skip_chars_positions + $skip_string_char_lenght - 1) as $position) {
$positionsNotToFuckUp[$position] = "can't tuch this, na na na naaa na naaaaaa";
}
}

}
// so now we are rollin, we got $StringToChars which is our char array, and $positionsNotToFuckUp whic we dont tuch
// lets make a new array with only positions we can tuch
//var_dump($StringToChars);
//var_dump($positionsNotToFuckUp);

$positionsThatWeCanTouch =array_diff_key($StringToChars,$positionsNotToFuckUp);
// great, now that we know what we can touch, lets try to use the map array to find if we can replace somethig

//var_dump($StringToChars);
//var_dump($positionsThatWeCanTouch);


// listlle shuffle magic and we select one lucky winner
uksort($map, function() { return rand() > getrandmax() / 2; });
foreach ($map as $key => $value) {

$positionsToReplace = array_keys($positionsThatWeCanTouch, $key);
//print_r($positionsToReplace);
if (!empty($positionsToReplace)) {
$locationOfTheWinnerChar = $positionsToReplace[rand(0, count($positionsToReplace) - 1)];
//echo "replace $key with $value where: $locationOfTheWinnerChar";
$StringToChars[$locationOfTheWinnerChar] = $value;

break;
}

}

// and let built it back up from chars to string;
$inputWithReplacehChar = implode("", $StringToChars);

return $inputWithReplacehChar;

}

function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) {
$offset = strpos($haystack, $needle, $offset);
if($offset === false) {
return $results;
} else {
$results[] = $offset;
return strpos_recursive($haystack, $needle, ($offset + 1), $results);
}
}



echo luckyReplacement ($input, $map, $skip);


?>
 
@Chris Devon DM me your payment details, I accept your solution.

I had to remove uksort() because of the deprecation notice in PHP 8 but other than that - it works as expected. :)
 
Status
Not open for further replies.
Back
Top