An algorithm that automatically analyzes mobile phone numbers (such as AAAAABAB)-php Tutorial

Source: Internet
Author: User
An algorithm for automatically analyzing mobile phone numbers (such as AAAAABAB) is classified into aaaaa aaaa aaa aabbcc aabb aaabbb aabbb aaabababababab AAAAB ascending ABCD descending DCBA

I import a group of mobile phone numbers to the database through text (one line of mobile phone numbers). I need to write an algorithm to obtain the type of mobile phone numbers, such as mobile phone numbers (15836998812) this is AABB (because 9988 is AABB, as long as two of them are connected together, for example, 1133 is also AABB), 15836888812, which is AAAA, divided
AAAAA type (for example, 15836999992, judgment basis: 99999)
AAAA type (for example, 158365111152, judgment basis: 1111)
AAA type (for example, 15836222712, judgment basis: 222)
AABBCC type (for example, 15877889912, judgment basis: 778899)
AABB type (for example, 15866779912, judgment basis: 6677)
AAABBB (for example, 15811122212, judgment basis: 111222)
AABBB type (for example, 15855666212, judgment basis: 55666)
AAABB type (for example, 15811122712, judgment basis: 11122)
ABABAB type (for example, 15823232312, judgment basis: 22323)
ABAB type (for example, 15836368963, judgment basis: 3636)
AAAAB (for example, 15811112569, judgment basis: 11112)
Ascending ABCD (for example, 158123478963, judgment basis: 1234)
Descending DCBA (for example, 15843215698, judgment basis: 4321)


Reply to discussion (solution)

Recursive computing

Recursive computing
Let's talk about the detailed algorithm.

First give a test code
Do not process ascending or descending order

$ Ar = array ('123', '123', '123', '123', '123', '123', '123', '123', '123 ', '20140901', '20160901', '20160901', '20160901',); $ mode = array ('aaaaa' => '(\ d) \ 1 {4} ', 'AAA' =>' (\ d) \ 1 {3} ', 'AAA' =>' (\ d) \ 1 {2} ', 'abbcc' => '(\ d) \ 1 (\ d) \ 2 (\ d) \ 3 ', 'aab' => '(\ d) \ 1 (\ d) \ 2', 'aaabbb' => '(\ d) \ 1 {2} (\ d) \ 2 {2} ', 'abbb' =>' (\ d) \ 1 (\ d) \ 2 {2} ', 'aaab' =>' (\ d) \ 1 {2} (\ d) \ 2 ', 'ababab' =>' (\ d) \ 1 \ 2 \ 1 \ 2 ', 'abab' => '(\ d) \ 1 \ 2', 'aaaab' =>' (\ d) \ 1 {3} \ d', // 'ascending ABCD' => '(\ d) \ 1 {4 }', // 'descending dcba' => '(\ d) \ 1 {4}',); foreach ($ ar as $ tel) {foreach ($ mode as $ m => $ p) {if ($ tel! = Preg_replace_callback ("/$ p/", 'back', $ tel) {$ r [$ tel] [] = $ m ;}} function back ($ r) {if (count ($ r) = 2 & strlen ($ r [0])> 2) return ''; if (count ($ r) = 3 & $ r [1] = $ r [2]-1) return ''; if (count ($ r) = 4 & $ r [1] = $ r [2]-1 & $ r [1] = $ r [3]-2) return ''; return $ r [0];} print_r ($ r );
Array(    [15836999992] => Array        (            [0] => AAAAA            [1] => AAAA            [2] => AAA            [3] => AAAAB        )    [158365111152] => Array        (            [0] => AAAA            [1] => AAA            [2] => AAAAB        )    [15836222712] => Array        (            [0] => AAA        )    [15877889912] => Array        (            [0] => AABBCC            [1] => AABB        )    [15866779912] => Array        (            [0] => AABB        )    [15811122212] => Array        (            [0] => AAA            [1] => AABB            [2] => AAABBB            [3] => AABBB            [4] => AAABB        )    [15855666212] => Array        (            [0] => AAA            [1] => AABB            [2] => AABBB        )    [15811122712] => Array        (            [0] => AAA            [1] => AABB            [2] => AAABB        )    [15823232312] => Array        (            [0] => ABABAB            [1] => ABAB        )    [15811112569] => Array        (            [0] => AAAA            [1] => AAA            [2] => AAAAB        ))

First give a test code
Do not process ascending or descending order

PHP code

$ Ar = array (
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'2017 ......
Can you provide the idea of descending order?

First give a test code
Do not process ascending or descending order

PHP code

$ Ar = array (
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'2017 ......

'Aaaaa' => '(\ d) \ 1 {4}' what does this regular expression mean?

'Aaaaa' => '(\ d) \ 1 {4}' what does this regular expression mean?
(\ D) indicates matching a number.
\ 1 {4} indicates that the number matched previously must be repeated four times.

So he can match
11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 00000

The ascending or descending order cannot be expressed using a regular expression and needs to be determined separately.

$ Ar = array ('20140901', '20160301',); foreach ($ ar as $ tel) {$ u = 0; $ d = 0; $ t = str_split ($ tel); for ($ I = 1; $ I
 
  
= 3) {echo "ascending order $ tel \ n"; break;} if ($ d> = 3) {echo "descending order $ tel \ n "; break ;}}}
 
Ascending 158123478963
Descending 15843215698

First give a test code
Do not process ascending or descending order

PHP code

$ Ar = array (
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'2017 ......
I still don't understand the regular expression. let's talk about the principle.

First give a test code
Do not process ascending or descending order

PHP code

$ Ar = array (
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'2017 ......
Especially the back function.

First give a test code
Do not process ascending or descending order

PHP code

$ Ar = array (
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'123 ',
'2017 ......


This algorithm is a bit problematic. if it is 15877998917, it should be AABB, but it cannot be analyzed.

Good on the 7th floor, learning

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.