Recursive, two-dimensional array clockwise rotation 90°, regular expression
1. Recursive algorithm is a process of directly or indirectly calling its own algorithm.
Characteristics:
- Recursion is invoking itself in a procedure or function.
- Explicit recursive end condition, i.e. recursive exit
- Concise, but not advocating
- Recursive number of multiple, easy to cause stack overflow
Requirements:
- decrease in recursive size per call
- Prepare for the last time
- smaller size must be answered directly instead of recursive calls
Example: recursive implementation of the dichotomy
1 defSearchmydata (MYDATE,A1):2mid = Int (len (mydate)/2)3 ifMid >= 1:4 ifMydate[mid]>A1:5 Print("Target data is on the left of%s"%Mydate[mid])6 Searchmydata (MYDATE[:MID],A1)7 elifmydate[mid]<A1:8 Print("Target data is to the right of%s"%Mydate[mid])9 Searchmydata (MYDATE[MID:],A1)Ten Else: One Print("We Get it! ") A Else: - Print("We can ' t find it! ") - if __name__=='__main__': thedata = List (range (1,600,3)) - Print(data) -Searchmydata (data,397)
Results:
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 9 1, 94, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124, 127, 130, 133, 136, 139, 142, 145, 148, 151, 154, 157, 160, 163, 1 66, 169, 172, 175, 178, 181, 184, 187, 190, 193, 196, 199, 202, 205, 208, 211, 214, 217, 220, 223, 226, 229, 232, 235, 238 , 241, 244, 247, 250, 253, 256, 259, 262, 265, 268, 271, 274, 277, 280, 283, 286, 289, 292, 295, 298, 301, 304, 307, 310, 313, 316, 319, 322, 325, 328, 331, 334, 337, 340, 343, 346, 349, 352, 355, 358, 361, 364, 367, 370, 373, 376, 379, 382, 38 5, 388, 391, 394, 397, 400, 403, 406, 409, 412, 415, 418, 421, 424, 427, 430, 433, 436, 439, 442, 445, 448, 451, 454, 457, 460, 463, 466, 469, 472, 475, 478, 481, 484, 487, 490, 493, 496, 499, 502, 505, 508, 511, 514, 517, 520, 523, 526, 529, 5 535, 538, 541, 544, 547, 550, 553, 556, 559, 562, 565, 568, 571, 574, 577, 580, 583, 586, 589, 592, 595, 598]
Target data on 301 Right
Target data on 451 left
Target data on 376 right
Target data on 412 left
Target data on 394 right
Target data on 403 left
We Get It!
2, two-dimensional array of° clockwise rotation
1A = [[col] forAo.inchRange (4)] forRowinchRange (4)]2 forIinchA:3 Print(i)4 Print('-'*20)5 forRow_2inchRange (4):6 forCol_2inchRange (row_2,4):7TMP =A[row_2][col_2]8A[row_2][col_2] =A[col_2][row_2]9A[col_2][row_2] =tmpTen One forIinchA: A Print(i)
Results:
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
--------------------
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
Note: A[r][c] represents the element in column c of section 2 of the two-dimensional list , and The array representation is similar
3. Regular Expressions
1 ImportRe2ret = Re.match ("ABC","abcdef")3 Print(ret)4 Print(Ret.group ())5ret = Re.match ("[0-9]","Abc6fak")#must be a string6 #Match is matched from the beginning, and if the start is not a number, then the match is not7ret = Re.match ("[0-9]{0,10}","Abc6fak")#Match 0~10 Times8ret = Re.match ("[0-9]{10}","Abc6fak")#matched 10 times9 ifret:Ten Print(Ret.group ()) Oneret = Re.findall ("[0-9]{1,10}","213abc6fak")#Match all numbers Aret = Re.findall ("[0-9]{1,10}","213abc6fak")#Match all characters -ret = Re.findall (".*","1234aggh")#match all, and finally have ' -ret = Re.findall (".","1234aggh")#match a single character theret = Re.findall (".+","1234aggh")#Match All -ret = Re.sub ("\d+","|","1234aggh", count=2)#Replace the top two - ifret: - Print(ret)
Python Learning note recursion, two-dimensional array clockwise rotation 90°, regular expression