Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
1-homework-yanjun
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
bonnieyan
1-homework-yanjun
Commits
1d24455b
Commit
1d24455b
authored
Jan 15, 2019
by
bonnieyan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
第五次作业
parent
89b228b6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
123 additions
and
0 deletions
+123
-0
5-homework-yanjun/goat_latin.py
+48
-0
5-homework-yanjun/regular.py
+37
-0
5-homework-yanjun/sort_utils.py
+38
-0
No files found.
5-homework-yanjun/goat_latin.py
0 → 100644
View file @
1d24455b
'''
给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
山羊拉丁文的规则如下:
如果单词pple"变为"applema"。
如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词"goat"变为"oatgma"。
以元音开头(a, e, i, o, u),在单词后添加"ma"。
例如,单词"a
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'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
goat_latin
(
s
):
yuan_yin
=
[
'a'
,
'e'
,
'i'
,
'0'
,
'u'
]
for
i
in
range
(
len
(
s
)):
if
s
[
i
][
0
]
.
lower
()
in
yuan_yin
:
s
[
i
]
+=
"ma"
else
:
s
[
i
]
=
s
[
i
][
1
:]
+
s
[
i
][
0
]
s
[
i
]
+=
"ma"
s
[
i
]
+=
"a"
*
(
1
+
i
)
#print(s[i])
return
" "
.
join
(
s
)
s
=
"I speak Goat Latin"
.
split
(
' '
)
s
=
"The quick brown fox jumped over the lazy dog"
.
split
(
' '
)
#print(s)
print
(
goat_latin
(
s
))
5-homework-yanjun/regular.py
0 → 100644
View file @
1d24455b
# 类初始化时接受一个字符串的参数,下面的方法将不会接受参数。
import
re
class
Regular
:
# 方法能够匹配出所有的数字
def
get_numbers
(
self
,
s
):
print
(
re
.
findall
(
"
\
d{4,7}"
,
s
))
# 方法能够匹配出所有的邮箱
def
get_emails
(
self
,
s1
):
print
(
re
.
findall
(
"
\
d*@[a-z0-9]*.com"
,
s1
))
# 方法可以匹配出所有的ip
def
get_ips
(
self
,
s2
):
print
(
re
.
findall
(
"
\
d{1,3}
\
.
\
d{1,3}
\
.
\
d{1,3}
\
.
\
d{1,3}"
,
s2
))
# 方法能够匹配出所有的电话号码,电话包含座机和移动电话,座机要考虑区号3位或4位,号码要考虑7位或8位
def
get_phone_numbers
(
self
,
s3
):
print
(
re
.
findall
(
"0
\
d{2,3}-
\
d{7,8}|13[0-9]
\
d{8}|17[0-9]
\
d{8}"
,
s3
))
# 方法能够匹配出所有的url,url可以以http开头、https开头、ftp开头
def
get_urls
(
self
,
s4
):
print
(
re
.
findall
(
"http://w{3}.[a-z]*.com|https://w{3}.[a-z]*.com|ftp://w{3}.[a-z]*.com"
,
s4
))
s
=
"给的一串数字1231,是不是哦1778881"
s1
=
"我的邮箱是:525663314@qq.com,她的邮箱是:1738022131@126.com"
s2
=
"ip地址是:1132.1112.312.1,123.124.121.122,10.112.123.122"
s3
=
"我的电话号码是:010-12341213,0832-3821251,17380409735,12345678901"
s4
=
"以下url为:http://www.baidu.com,https://www.tmall.com,ftp://www.taobao.com"
p
=
Regular
()
p
.
get_numbers
(
s
)
p
.
get_emails
(
s1
)
p
.
get_ips
(
s2
)
p
.
get_phone_numbers
(
s3
)
p
.
get_urls
(
s4
)
\ No newline at end of file
5-homework-yanjun/sort_utils.py
0 → 100644
View file @
1d24455b
class
SortUtils
:
def
sort
(
self
,
f
,
s
):
if
f
==
"bubble"
:
for
i
in
range
(
len
(
s
)
-
1
):
for
j
in
range
(
len
(
s
)
-
i
-
1
):
if
s
[
j
]
>
s
[
j
+
1
]:
s
[
j
],
s
[
j
+
1
]
=
s
[
j
+
1
],
s
[
j
]
print
(
s
)
print
(
s
)
elif
f
==
"quick"
:
start_index
=
0
end_index
=
len
(
s
)
-
1
if
start_index
<
end_index
:
# 如果角标左侧小于右侧则开始排序,否则退出
basic
,
i
,
j
=
s
[
start_index
],
start_index
,
end_index
while
i
<
j
:
# 保证左侧的index一定比右侧的小
while
i
<
j
and
basic
<=
s
[
j
]:
# 基准值比j(右侧)小,那么该值不做任何运算
j
-=
1
# 角标左移
while
i
<
j
and
basic
>=
s
[
i
]:
# 基准值比i(左侧)大,那么该值不做任何运算
i
+=
1
# 角标右移
s
[
i
],
s
[
j
]
=
s
[
j
],
s
[
i
]
print
(
"i="
+
str
(
i
)
+
"&&&"
+
"j="
+
str
(
j
)
+
"$$$"
+
"sort_list="
+
str
(
s
))
s
[
i
],
s
[
start_index
]
=
s
[
start_index
],
s
[
i
]
print
(
str
(
s
))
#sort(s, start_index, i-1)
# SortUtils.sort(self, f, s)
# sort(s, i + 1, end_index)
p
=
SortUtils
()
#p.sort("bubble", [5, 15, 2, 5, 3, 18, 6])
p
.
sort
(
"quick"
,
[
5
,
15
,
2
,
5
,
3
,
18
,
6
])
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