程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Two elegant small function blocks of micropython

編輯:Python

Although it's great to be a TIKU , But I still suggest that you write as much as possible , Take a look at the small function blocks , It is very helpful to improve your programming thinking and ability , And it's very refreshing if it's done gracefully !
function 1:
Find the characters between two topping strings
such as chars=‘sdf#1234,[email protected]’
Just want to extract # and , Between 1234, How do you do it? ?
Universal regular expressions , I'm sure I can do it , Maybe other libraries can also , But why not try to write one yourself ?
On the core code :

clist=[]
mark=0
text='sdf#1234,[email protected]'
for ch in text:
if ch=='#':
mark+=1
elif ch==',':
mark-=1
else:
if mark:
clist.append(ch)
result=''.join(clist)

Add a sign as , Start and finish well , Get it done ! Learned
function 2:
Find the area where two coordinate rectangles intersect
It doesn't sound like a complicated data problem ? Maybe cv2 Or a library has this function , But is it really difficult to write one by yourself ?
Code up :

def Returns_Coordinates(left_1,right_1,left_2,right_2): # left_1 right_1 block
coord = []
left_h = 0
left_w = 0
right_h = 0
right_w = 0
height = min(right_1[0],right_2[0])-max(left_1[0],left_2[0])
width = min(right_1[1],right_2[1])-max(left_1[1],left_2[1])
if height > 0 and width > 0 :
if left_2[0] <= left_1[0] and left_2[1] <= left_1[1] :
left_h = 0
left_w = 0
elif left_2[0] >= left_1[0] and left_2[1] <= left_1[1] :
left_h = left_2[0] - left_1[0]
left_w = 0
elif left_2[0] >= left_1[0] and left_2[1] >= left_1[1] :
left_h = left_2[0] - left_1[0]
left_w = left_2[1] - left_1[1]
else :
left_h = 0
left_w = left_2[1] - left_1[1]
right_h = left_h + height
right_w = left_w + width
coord.append(left_h)
coord.append(left_w)
coord.append(right_h)
coord.append(right_w)
return coord

It is not difficult to figure it out , Just a comparison , Then I want to do a , Calculate the total area of the intersection of a rectangle and multiple rectangles , cheer up , Just think it over !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved