Ask for a short PHP code in 40 minutes. please enter the requirement: Take a random number from the numbers a and B to form a string of 10 characters. Requirements: 1. two adjacent letters cannot be the same (except for 0). if two consecutive letters of the same name are randomly obtained, the following letter must be converted to 0, until another different letter appears, such as the result of "abbb" after the loop, you need to calculate a short PHP code in 40 minutes.
Requirements:
Take a random number from numbers a and B to form a string of 10 characters.
Requirements:
1. two adjacent letters cannot be the same (except 0). if two consecutive letters of the same name are randomly obtained, the following letter must be converted to 0, until another different letter appears, such as the result of "abbb" after the loop, it needs to be converted to "ab00;
2. overlapping letters cannot be processed outside the loop. this operation must be completed when the string is generated.
Example of the final correct result: 0a000b00ab or abab0000ba
Thank you!
------ Solution --------------------
PHP code
$ Last = ''; $ r =''; for ($ I = 0; $ I <10; $ I ++) {$ t = rand (0, 1)> 0.5? 'A': 'B'; $ r. = $ t = $ last? '0': $ t; $ last = $ t;} echo $ r;
------ Solution --------------------
Function chkStr ($ a, $ B)
{
$ Str = '';
$ C = '';
$ D = '';
$ Arr = array ($ a, $ B );
While (strlen ($ str) <10)
{
$ D = rand (0, 1 );
$ Str. = ($ c = $ arr [$ d])? ("0"): ($ arr [$ d]);
$ C = ($ c = $ arr [$ d])? ("0"): ($ arr [$ d]);
}
Return $ str;
}
Echo chkStr (a, B );
?>