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
f922668d
Commit
f922668d
authored
Jan 12, 2019
by
bonnieyan
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
7ab1d2b3
7e520d96
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
103 additions
and
18 deletions
+103
-18
.gitignore
+1
-0
2-homework-yanjun/botv0.1.py
+3
-0
2-homework-yanjun/peakIndexInMountainArray.py
+31
-6
3-homework-yanjun/mosimm.py
+40
-9
gemstone.py
+28
-3
No files found.
.gitignore
0 → 100644
View file @
f922668d
# Created by .ignore support plugin (hsz.mobi)
2-homework-yanjun/botv0.1.py
View file @
f922668d
...
...
@@ -46,3 +46,6 @@ while True:
elif
re
.
findall
(
"再见|bye|拜拜"
,
s
):
print
(
"再见"
)
break
2-homework-yanjun/peakIndexInMountainArray.py
View file @
f922668d
#山脉数组
# 山脉数组
# 遍历了2次 时间复杂度是O(n^2)
def
peakIndexInMountainArray
(
A
):
index
=
0
for
i
in
range
(
1
,
len
(
A
)):
if
A
[
i
-
1
]
<
A
[
i
]
>
A
[
i
+
1
]:
if
A
[
i
-
1
]
<
A
[
i
]
>
A
[
i
+
1
]:
index
=
i
# break 这里加上break 跳出语句减少循环次数
print
(
index
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
peakIndexInMountainArray
([
0
,
1
,
0
])
peakIndexInMountainArray
([
0
,
2
,
1
,
0
])
\ No newline at end of file
peakIndexInMountainArray
([
0
,
2
,
1
,
0
])
# 通过二分查找实现 O(log2n)
def
peakIndexInMountainArray
(
mountain
):
height
=
len
(
mountain
)
low
=
0
while
low
<=
height
:
mid
=
(
height
+
low
)
//
2
if
array
[
mid
]
>
array
[
mid
-
1
]
and
array
[
mid
]
>
array
[
mid
+
1
]:
print
(
mid
)
break
elif
array
[
mid
]
>
array
[
mid
+
1
]:
height
=
mid
-
1
else
:
low
=
mid
+
1
array
=
[
0
,
1
,
0
]
peakIndexInMountainArray
(
array
)
array
=
[
0
,
2
,
1
,
0
]
peakIndexInMountainArray
(
array
)
array
=
[
0
,
1
,
2
,
3
,
7
,
6
,
5
,
4
,
2
,
1
,
0
]
peakIndexInMountainArray
(
array
)
array
=
[
0
,
1
,
2
,
7
,
6
,
5
,
4
,
0
]
peakIndexInMountainArray
(
array
)
3-homework-yanjun/mosimm.py
View file @
f922668d
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
]
mima
=
{
"a"
:
".-"
,
"b"
:
"-..."
,
"c"
:
"-.-."
,
"d"
:
"-.."
,
"e"
:
"."
,
"f"
:
"..-."
,
"g"
:
"--."
,
"h"
:
"...."
,
"i"
:
".."
,
"j"
:
".---"
,
"k"
:
"-.-"
,
"l"
:
".-.."
,
"m"
:
"--"
,
"n"
:
"-."
,
"o"
:
"---"
,
"p"
:
".--."
,
"q"
:
"--.-"
,
"r"
:
".-."
,
"s"
:
"..."
,
"t"
:
"-"
,
"u"
:
"..-"
,
"v"
:
"...-"
,
"w"
:
".--"
,
"x"
:
"-..-"
,
"y"
:
"-.--"
,
"z"
:
"--.."
}
mima
=
{
"a"
:
".-"
,
"b"
:
"-..."
,
"c"
:
"-.-."
,
"d"
:
"-.."
,
"e"
:
"."
,
"f"
:
"..-."
,
"g"
:
"--."
,
"h"
:
"...."
,
"i"
:
".."
,
"j"
:
".---"
,
"k"
:
"-.-"
,
"l"
:
".-.."
,
"m"
:
"--"
,
"n"
:
"-."
,
"o"
:
"---"
,
"p"
:
".--."
,
"q"
:
"--.-"
,
"r"
:
".-."
,
"s"
:
"..."
,
"t"
:
"-"
,
"u"
:
"..-"
,
"v"
:
"...-"
,
"w"
:
".--"
,
"x"
:
"-..-"
,
"y"
:
"-.--"
,
"z"
:
"--.."
}
word
=
""
list_code
=
[]
count
=
0
#外层循环获取字符gin
#
外层循环获取字符gin
for
i
in
words
:
#内层循环获取字母对应的摩斯密码
#
内层循环获取字母对应的摩斯密码
for
j
in
i
:
word
+=
mima
[
j
]
#将对应字符gin的摩斯密码组合放入s
#
将对应字符gin的摩斯密码组合放入s
s
=
word
if
s
not
in
list_code
:
count
+=
1
#将不一样的摩斯密码组合放入list_code
#
将不一样的摩斯密码组合放入list_code
list_code
.
append
(
s
)
#清空组合的摩斯密码以便外层循环存放新的摩斯密码
#
清空组合的摩斯密码以便外层循环存放新的摩斯密码
word
=
""
print
(
count
)
print
(
list_code
)
\ No newline at end of file
print
(
list_code
)
morse
=
[
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
]
# 根据ascii 小写97 a 开始创建字典
morse_dict
=
{
chr
(
i
+
97
):
morse
[
i
]
for
i
in
range
(
0
,
len
(
morse
))}
def
uniqueMorseRepresentations
(
words
):
# 使用set 用来去重
unique
=
set
()
for
word
in
words
:
unique
.
add
(
toMorseCode
(
word
))
return
len
(
unique
)
def
toMorseCode
(
word
):
morse_code
=
""
for
char
in
word
.
lower
():
morse_code
+=
morse_dict
.
get
(
char
)
return
morse_code
check_words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
]
print
(
uniqueMorseRepresentations
(
check_words
))
gemstone.py
View file @
f922668d
# 复杂度是较高
def
numJewelsInStones
(
J
,
S
):
J_length
=
len
(
J
)
S_length
=
len
(
S
)
...
...
@@ -6,7 +7,32 @@ def numJewelsInStones(J, S):
count
=
0
for
s
in
S
:
if
s
in
J
:
count
=
count
+
1
count
=
count
+
1
return
count
print
(
numJewelsInStones
(
"ba"
,
"aAAbbbb"
))
\ No newline at end of file
print
(
numJewelsInStones
(
"ba"
,
"aAAbbbb"
))
# 复杂度O(n)
def
numJewelsInStones
(
J
,
S
):
stores
=
{}
for
c
in
S
:
if
c
in
stores
:
stores
[
c
]
=
stores
[
c
]
+
1
else
:
stores
[
c
]
=
1
count
=
0
for
c
in
J
:
if
c
in
stores
:
count
+=
stores
[
c
]
print
(
count
)
J
=
"aA"
S
=
"aAAbbbb"
numJewelsInStones
(
J
,
S
)
J
=
"z"
S
=
"ZZ"
numJewelsInStones
(
J
,
S
)
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