Commit 3f6ee173 by “安晓东”

第五次作业

parent f3127e80
import re
class Regular:
def __init__(self, str_f):
self.str_f = str_f
# 匹配所有的数字
def get_numbers(self):
return re.findall('\d', self.str_f)
# 匹配所有的邮箱
def get_emails(self):
ret = '[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z_]{1,13}\.[com,cn,net]{1,3}$'
return re.findall(ret, self.str_f)
# 方法可以匹配出所有的ip
def get_ips(self):
rep = re.findall('((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))', self.str_f)
return rep
# 方法能够匹配出所有的url,url可以以http开头、https开头、ftp开头
def get_urls(self):
ret = '[http,https,ftp]{1,5}://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
return re.findall(ret, self.str_f)
if __name__ == '__main__':
s = Regular('https://www.fdsafds$%%.com')
s1 = Regular('my mumber is 18268194892 and my email address is 289965734@qq.com,and my ip address is 192.168.1.1 and 255.666.555.222 and my url is https://www.weekfan.com and my phone home_number is 0734-6838533 and 020-85071966')
print(s1.get_ips())
# print(s.get_numbers())
print(s.get_urls())
class SortUtils:
def sort_s(self, lis, way):
if way == 'bubble':
self._bubble(lis)
if way == 'quick':
self._quick(lis, 0, len(lis)-1)
def _bubble(self, lis):
for i in range(len(lis) - 1):
for j in range(len(lis) - 1 - i):
if lis[j] > lis[j + 1]:
lis[j], lis[j + 1] = lis[j + 1], lis[j]
print(lis)
return lis
def _quick(self, list_str, start_index, end_index):
if start_index < end_index: # 角标左侧小于右侧开始排序
i, basick, j = start_index, list_str[start_index], end_index
while i <j:# 首先保证左侧的index比右侧的小
while i < j and basick <= list_str[j]: # 基准比有侧的下表为(j)值小时,角标做左移
j -= 1
while i < j and basick >= list_str[i]:# 基准比左侧的下表为(i)值大时,角标右移
i += 1
list_str[i], list_str[j]=list_str[j],list_str[i]
print("i=" + str(i) + "&&&" + "j=" + str(j) + "$$$" + "sort_list=" + str(list_str))
list_str[start_index],list_str[i]=list_str[i],list_str[start_index]# 当i=j时,将基准值与下标为i或者j的值交换位置
self._quick(list_str,start_index , i-1)# 取i=j的下标左侧的值在执行
self._quick(list_str,i+1,end_index)
if __name__ == '__main__':
s = SortUtils()
s.sort_s([10, 6, 9, 18, 20, 5, 7, 15, 14, 18, 19], 'quick')
# s.sort_s([10, 6, 9, 18, 20, 5, 7, 15, 14, 18, 19], 'bubble')
import re
a = '1324.231.432.1234,192.168.1.6,10.25.11.255这些信息中,哪些是ip呢?'
ret = re.findall('((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))',a)
print(ret)
print(re.findall("(?:(?:\d\.|\d{2}\.|1\d{2}\.|2[0-4]\d\.|25[0-5]\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))",a))
a = "I speak Goat Latin"
s = a.split(' ')
print(s)
def toGoatLatin(S):
list_s = S.split(' ')
result = ''
index = 1
for i in list_s:
if i[0] in ['a','e','i','o','u','A','E','I','O','U']:
i =i+'ma'+'a'*index
else:
i=i[1:]+i[0]+'ma'+'a'*index
index +=1
result+=i+' '
return result
print(toGoatLatin("I speak Goat Latin"))
#
#
# def toGoatLatin(S):
#
# vowel = ['a', 'e', 'i', 'o', 'u']
# temp = S.split()
# for i in range(len(temp)):
# if temp[i][0].lower() in vowel:
# temp[i] = temp[i] + 'ma' + 'a' * (i + 1)
# else:
# temp[i] = temp[i][1:] + temp[i][0] + 'ma' + 'a' * (i + 1)
# return " ".join(temp).strip()
# print(toGoatLatin("I speak Goat Latin"))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment