Search code examples
pseudocodeauto-generate

replacing letters in text (pseudo-code)


I'm making a script to create a username. It should be four letters long; traditionally we've used 3 letters of the last name + 1 of the first name.
If it was already used we manually thought of an alternative.

So if my name is Fred Flinstones we should try FLIF. If this doesnt work; we loop through the name: FLIA, FLIB, FLIC ... FLIZ, FLAA, FLAB, FLAC, ... FLZZ, FAAA, FAAB, ...

The easiest way is to loop through last letters; then make another set of loops through second last letter and loop through last letters; then a set of loops through third last, second last, last; and fourth+third+second+last. This makes a lot of do while loops nested in eachother + unreadable for other humans + a lot of typing.
I could use a counter per letter but that also doesn't seem elegant I could try with one counter and then using mod 26 to see how many letters need replacement (but that seems very complex).

Is there some elegant/efficient ways to do this?


Bonus points for first trying to keep the string as 'logically correct' as possible (f.e. keeping the last letter an F for Fred or skipping letters FLIF; FLNF, FLSF, FLTF, ...) .


Solution

  • Not sure if this is what you mean, but if you structure your username-script in the following way (I used PHP as language), you can extend it by adding options with higher fuzz factors while keeping the code readable:

    echo findName('FLINTSTONE', 'FRED');
    
    function findName($last, $first) {
        for ($fuzzFactor = 0; ; $fuzzFactor++) {
            $candidates = fuzzNames($last, $first, $fuzzFactor);
    
            if (empty($candidates)) {
                // exhausted
                return "sorry, I'm out of options!";
            }
    
            foreach ($candidates as $candidate) {
                if (isUnique($candidate)) {
                    return $candidate;
                }
            }
        }
    }
    
    function fuzzNames($last, $first, $fuzzFactor) {
        switch ($fuzzFactor) {
            case 0:
                // no fuzz, return first choice
                return array(substr($last, 0, 3) . $first[0]);
            case 1:
                // replace the third letter of the last name
                // by the fourth/fifth/.../last letter (FLNF, FLTF, ... , FLEF)
                $candidates = array();
                for ($i = 3; $i < strlen($last); $i++) {
                    $candidates[] = substr($last, 0, 2) . $last[$i] . $first[0];
                }
                return $candidates;
            case 2:
                // replace the second and third letter of the last name
                // by their follow-ups (FINF, FITF, ... , FNEF)
                $candidates = array();
                for ($i = 2; $i < strlen($last) - 1; $i++) {
                    for ($j = $i + 1; $j < strlen($last); $j++) {
                        $candidates[] = $last[0] . $last[$i] . $last[$j] . $first[0];
                    }
                }
                return $candidates;
            default:
                return array();
        }
    }