40 points for a small piece of PHP code, everyone, please enter the solution

Source: Internet
Author: User
40 points for a small piece of PHP code, everyone please enter
Demand:
Randomly take a number from the two number of a, B, and make up a string of 10 characters.

Requirements:
1, the adjacent two letters can not be the same (except 0), if randomly taken to the same 2 consecutive letters, you need to convert the following letter to 0, until another different letter, such as the abbb after the result of the cycle, need to convert to ab00;
2. The overlapping letters cannot be processed outside the loop, and this operation needs to be done when the string is generated.

Examples of final and correct results: 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]);
}

}

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.