Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
5
5_homework_yangpeng
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杨鹏
5_homework_yangpeng
Commits
39e7d06a
Commit
39e7d06a
authored
Jan 14, 2019
by
yangpengflag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit homework
parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
0 deletions
+149
-0
reg_utils.py
+102
-0
to_goat_latin.py
+47
-0
No files found.
reg_utils.py
0 → 100644
View file @
39e7d06a
import
re
class
Regular
:
def
__init__
(
self
,
strs
):
self
.
strs
=
strs
# 方法能够匹配出所有的数字
def
get_numbers
(
self
):
reg
=
"
\
d"
reg_reslt
=
re
.
findall
(
reg
,
self
.
strs
)
print
(
reg_reslt
)
return
reg_reslt
# 方法能够匹配出所有的邮箱
def
get_emails
(
self
):
reg
=
"
\
d{9}@[a-z]*
\
.com"
reg_reslt
=
re
.
findall
(
reg
,
self
.
strs
)
print
(
reg_reslt
)
return
reg_reslt
# 方法可以匹配出所有的ip
def
get_ips
(
self
):
reg
=
r'(?<![\.\d])(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?![\.\d])'
reg_reslt
=
re
.
findall
(
reg
,
self
.
strs
)
print
(
reg_reslt
)
return
reg_reslt
# 方法能够匹配出所有的电话号码,电话包含座机和移动电话,座机要考虑区号3位或4位,号码要考虑7位或8位
def
get_phone_numbers
(
self
):
reg
=
"13[0-9]
\
d{8}|14[5,7]
\
d{8}|15[0-3,5-9]
\
d{8}|17[0-3,5-8]
\
d{8}|18[0-9]
\
d{8}|166
\
d{8}|19[8-9]
\
d{8}"
reg1
=
"0
\
d{2,3}
\
-
\
d{7,8}"
reg2
=
"(?:13[0-9]
\
d{8}|14[5,7]
\
d{8}|15[0-3,5-9]
\
d{8}|17[0-3,5-8]
\
d{8}|18[0-9]
\
d{8}|166
\
d{8}|19[8-9]
\
d{8})|(?:0
\
d{2,3}
\
-
\
d{7,8})"
reg_reslt
=
re
.
findall
(
reg2
,
self
.
strs
)
print
(
reg_reslt
)
return
reg_reslt
# 方法能够匹配出所有的url,url可以以http开头、https开头、ftp开头
def
get_urls
(
self
):
reg
=
"(?:http|https|ftp)
\
://[w]{3}
\
.[a-z0-9]*
\
.com"
reg_reslt
=
re
.
findall
(
reg
,
self
.
strs
)
print
(
reg_reslt
)
return
reg_reslt
class
SortUtils
:
# def __init__(self,list_strs,order_method):
# self.list_strs = list_strs
# self.order_method = order_method
def
sort
(
self
,
list_strs
,
order_method
):
if
order_method
==
"bubble"
:
SortUtils
.
__bubble_sort
(
list_strs
)
if
order_method
==
"quick"
:
SortUtils
.
__quick_sort
(
list_strs
,
0
,
len
(
list_strs
)
-
1
)
def
__bubble_sort
(
self
,
list_strs
):
for
i
in
range
(
len
(
list_strs
)
-
1
):
for
j
in
range
(
len
(
list_strs
)
-
1
-
i
):
if
list_strs
[
j
]
>
list_strs
[
j
+
1
]:
list_strs
[
j
],
list_strs
[
j
+
1
]
=
list_strs
[
j
+
1
],
list_strs
[
j
]
print
(
list_strs
)
print
(
list_strs
)
return
list_strs
def
__quick_sort
(
self
,
liststrs
,
start_index
,
end_index
):
print
(
liststrs
)
if
start_index
<
end_index
:
# 如果角标左侧小于右侧则开始排序,否则退出
basic
,
i
,
j
=
liststrs
[
start_index
],
start_index
,
end_index
while
i
<
j
:
# 保证左侧的index一定比右侧的小
while
i
<
j
and
basic
<=
liststrs
[
j
]:
# 基准值比j(右侧)小,那么该值不做任何运算
j
-=
1
# 角标左移
while
i
<
j
and
basic
>=
liststrs
[
i
]:
# 基准值比i(左侧)大,那么该值不做任何运算
i
+=
1
# 角标右移
liststrs
[
i
],
liststrs
[
j
]
=
liststrs
[
j
],
liststrs
[
i
]
print
(
"i="
+
str
(
i
)
+
"&&&"
+
"j="
+
str
(
j
)
+
"$$$"
+
"sort_list="
+
str
(
liststrs
))
liststrs
[
i
],
liststrs
[
start_index
]
=
liststrs
[
start_index
],
liststrs
[
i
]
SortUtils
.
__quick_sort
(
liststrs
,
start_index
,
i
-
1
)
SortUtils
.
__quick_sort
(
liststrs
,
i
+
1
,
end_index
)
if
__name__
==
"__main__"
:
s_number
=
"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"
regular
=
Regular
(
s_number
)
regular
.
get_numbers
()
regular
.
get_emails
()
regular
.
get_ips
()
regular
.
get_urls
()
regular
.
get_phone_numbers
()
l
=
[
10
,
6
,
9
,
18
,
20
,
5
,
7
,
15
,
14
,
18
,
19
]
sorttest
=
SortUtils
()
sorttest
.
sort
(
l
,
"bubble"
)
sorttest
.
sort
(
l
,
"quick"
)
to_goat_latin.py
0 → 100644
View file @
39e7d06a
# 给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
#
# 我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
#
# 山羊拉丁文的规则如下:
#
# 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
# 例如,单词"apple"变为"applema"。
#
# 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
# 例如,单词"goat"变为"oatgma"。
#
# 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
# 例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
# 返回将 S 转换为山羊拉丁文后的句子。
# 示例 1:
#
# 输入: "I speak Goat Latin"
# 输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
# 示例 2:
#
# 输入: "The quick brown fox jumped over the lazy dog"
# 输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
def
to_goat_latin
(
s
):
vowel
=
[
'a'
,
'e'
,
'i'
,
'o'
,
'u'
]
words
=
s
.
split
()
print
(
words
)
for
i
in
range
(
len
(
words
)):
if
words
[
i
][
0
]
.
lower
()
in
vowel
:
# 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
words
[
i
]
=
words
[
i
]
+
"ma"
words
[
i
]
=
words
[
i
]
+
"a"
*
(
i
+
1
)
# 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始
else
:
# 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"
words
[
i
]
=
words
[
i
][
1
:
len
(
words
[
i
])]
+
words
[
i
][
0
]
+
"ma"
words
[
i
]
=
words
[
i
]
+
"a"
*
(
i
+
1
)
# 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始
print
(
" "
.
join
(
words
))
s
=
"I speak Goat Latin"
to_goat_latin
(
s
)
ss
=
"The quick brown fox jumped over the lazy dog"
to_goat_latin
(
ss
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment