Ask for a short PHP code in 40 minutes. please refer to the solution.

Source: Internet
Author: User
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 );

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.