Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
homework
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
ligang
homework
Commits
eeec5392
Commit
eeec5392
authored
Jan 11, 2019
by
牛家玺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
批改作业
parent
c8185c31
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
18 deletions
+79
-18
01-homework-ligang.py
+45
-16
02-homework-ligang/mountain.py
+31
-2
02-homework-ligang/question_answer.py
+3
-0
No files found.
01-homework-ligang.py
View file @
eeec5392
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def
check
(
s1
,
s2
):
print
(
sum
(
map
(
lambda
ch
:
s1
.
count
(
ch
),
s2
)))
# -*- coding:utf-8 -*-
def
check
(
s1
,
s2
):
## 算法题最好自己来实现 使用基本的数据结构 和操作
## si.count 方法每次要遍历一次字符数组如果 s1的长度为n s2的长度为m 那么计算的时间复杂度是 O(m)*O(n) =O(n^2)
## 试着实现下复杂度为O(n)的算法
print
(
sum
(
map
(
lambda
ch
:
s1
.
count
(
ch
),
s2
)))
def
isUnique
(
str
):
l
=
list
(
str
)
n
=
len
(
l
)
for
i
in
range
(
n
):
if
str
.
count
(
l
[
i
])
!=
1
:
if
str
.
count
(
l
[
i
])
!=
1
:
print
(
"J value is repeat,please reinput J values"
)
exit
(
0
)
def
alphaCount
(
str
,
type
=
1
):
def
alphaCount
(
str
,
type
=
1
):
w
=
0
for
i
in
str
:
if
i
.
isalpha
():
w
+=
1
w
+=
1
if
w
>
50
:
if
type
==
1
:
if
type
==
1
:
a
=
'J'
else
:
a
=
'S'
a
=
'S'
print
(
"
%
s alpha gt 50"
%
a
)
exit
(
0
)
def
numJwelsInStones
(
j
,
s
):
if
j
.
strip
()
==
''
or
s
.
strip
()
==
''
:
def
numJwelsInStones
(
j
,
s
):
if
j
.
strip
()
==
''
or
s
.
strip
()
==
''
:
print
(
"J or S values is not empty"
)
else
:
jcount
=
alphaCount
(
j
,
1
)
scount
=
alphaCount
(
s
,
2
)
jcount
=
alphaCount
(
j
,
1
)
scount
=
alphaCount
(
s
,
2
)
isUni
=
isUnique
(
j
)
chek
=
check
(
j
,
s
)
chek
=
check
(
j
,
s
)
j
=
input
(
"please input J value"
)
j
=
input
(
"please input J value"
)
S
=
input
(
"please input S value "
)
numJwelsInStones
(
j
,
S
)
numJwelsInStones
(
j
,
S
)
# 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
)
02-homework-ligang/mountain.py
View file @
eeec5392
...
...
@@ -2,6 +2,8 @@
# -*-coding:utf-8 -*-
import
re
# 问题同宝石 时间负责度较高
def
peakIndexInMountainArray
(
s
):
sp
=
s
.
split
(
','
)
numbers
=
list
(
map
(
int
,
sp
))
...
...
@@ -9,9 +11,36 @@ def peakIndexInMountainArray(s):
print
(
'长度有误 ,重新输入 '
)
exit
(
0
)
print
(
numbers
.
index
(
max
(
numbers
)))
# 格式 为:1,2,3
st
=
input
(
"请输入数字,以半角逗号分隔 ,数字不能少于3个或大于10000个 "
)
if
not
re
.
search
(
'^[0-9,]+$'
,
st
):
print
(
'match error,repeat reinput '
)
if
not
re
.
search
(
'^[0-9,]+$'
,
st
):
print
(
'match error,repeat reinput '
)
exit
(
0
)
peakIndexInMountainArray
(
st
)
# 二分查找 时间复杂度logn的版本
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
)
02-homework-ligang/question_answer.py
View file @
eeec5392
...
...
@@ -43,3 +43,5 @@ while True:
else
:
print
(
"我没看懂你的问题"
)
break
# 建议使用正则表达式匹配
\ 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