#316、请用一行代码 实现将1-N 的整数列表以3为单位分组
print([[i for i in range(1,100)][i:i+3] for i in range(0,99,3)])
result=[]
for i in range(1,100,3):
temp=[]
for j in range(i,i+3):
temp.append(j)
result.append(temp)
print(result)
#317、一个字符串"abc123def45G7hi890j1121klMN567890",
#把他拆分成如下列表:[‘abc123’, ‘def45’, ‘G7’, ‘hi890’, ‘j1121’, ‘klMN567890’]
#方法一
import re
s = "abc123def45G7hi890j1121klMN567890"
print(re.findall(r"[a-zA-Z]+\d+",s))
#方法二
result = []
temp = ""
index = 0
for i in range(index,len(s)-1):
if s[i].isdigit() and s[i+1].isalpha():
temp = s[index:i+1]
index = i+1
result.append(temp)
result.append(s[index:])
print(result)
#方法三
result = []
temp = ""
index = 0
for i in range(len(s)-1):
if not(s[i].isdigit() and s[i+1].isalpha()):
temp += s[i]
else:
temp += s[i]
result.append(temp)
temp = ""
j=i
result.append(s[j+1:])
print(result)
#318、宝石与石头,想知道石头中有多少是宝石
#字符串J代表宝石,S代表石头
#J中字母不重复,J和S中都是字母,区分大小写
def find_jewels(j,s):
jewels_count = 0
for i in s:
if i in j:
jewels_count += 1
return jewels_count
print(find_jewels("aA","aAAbbbb"))
#321、独特的电子邮件地址
#如果在电子邮件地址的本地名称部分中的某些字符之间添加句点(’.’),
#则发往那里的邮件将会转发到本地名称中没有点的同一地址。
#例如,"alice.z@leetcode.com” 和 “alicez@leetcode.com” 会转发到同一电子邮件地址。
#如果在本地名称中添加加号(’+’),则会忽略第一个加号后面的所有内容。
#这允许过滤某些电子邮件,
#例如 m.y+name@email.com 将转发到 my@email.com。
emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
result = []
for email in emails:
temp = email[:email.find("+")]
temp = temp.replace(".","")
temp += email[email.find("@"):]
result.append(temp)
print(set(result),len(set(result)))
#322、翻转图像
#l = [[1,1,0],[1,0,1],[0,0,0]]
l=[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
result=[]
for sub in l:
sub.reverse()
temp=[]
for i in sub:
if i==0:
i=1
elif i==1:
i=0
temp.append(i)
result.append(temp)
print(result)
#323、实现函数 ToLowerCase(),该函数接收一个字符串参数 str,
#并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
def ToLowerCase(s):
if not isinstance(s,str):
return None
result=""
for i in s:
if i.islower():
result += i
elif i.upper():
result += i.lower()
else:
result += i
return result
print(ToLowerCase("Hello"))
print(ToLowerCase("here"))
print(ToLowerCase("Hello123"))
print(ToLowerCase("LOVELY"))