Commit eeec5392 by 牛家玺

批改作业

parent c8185c31
#!/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)
......@@ -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)
......@@ -43,3 +43,5 @@ while True:
else:
print("我没看懂你的问题")
break
# 建议使用正则表达式匹配
\ 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