Python:每日一题 91(答题领鱼币)
昨天的题目难度有些大,今天再来个简单的。我们知道英文字符A~Z从外观上看,有左右对称的如AM等,也有上下对称的如BK等,也有旋转180度重合的,也称作旋转对称如SN等,也有符合前面三种的称为全对称,现在给出一个字符串,统计出各种对称和非对称的字符数量。注对于全对称的字符就不再计入前三种对称中了。字符串中有重复的字符则都计算在内,如有两个W,则记为左右对称字符2个。
>>> str1 = 'WEFNKDSPHOFGMAFW'
>>> StrCount(str1)
字符串中共有左右对称字符4个,上下对称字符3个,旋转对称字符2个,全对称字符2个,其他字符5个。
我的程序:
**** Hidden Message *****
def StrCount(s):
Mylist=['HIOX','AMTUVWY','BCDEK','NSZ']
Mycount=
for i in s:
if i in Mylist:Mycount+=1
elif i in Mylist:Mycount+=1
elif i in Mylist:Mycount+=1
elif i in Mylist:Mycount+=1
else:Mycount+=1
print ('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个。'%(Mycount,Mycount,Mycount,Mycount,Mycount))
with many thanks from itertools import groupby
group = lambda s: 'RL' if s in 'AMTUVWY' else ('UD' if s in 'BCDEK' else ('RA' if s in 'NSZ' else ('ALL' if s in 'HIOX' else 'Others')))
str1 = 'WEFNKDSPHOFGMAFW'
for m,n in groupby(sorted(list(str1), key=group), key=group):
if m == 'RL': print('左右对称%d个' % len(list(n)), end=', ')
if m == 'UD': print('上下对称%d个' % len(list(n)), end='. ')
if m == 'RA': print('旋转对称%d个' % len(list(n)), end=', ')
if m == 'ALL': print('全对称%d个' % len(list(n)), end=', ')
全对称2个, 旋转对称2个, 左右对称4个, 上下对称3个.
def StrCount(a):
zy=0
sx=0
xz=0
qd=0
qt=0
for i in a:
if i in 'AMTUVWY':
zy += 1
elif i in 'BKCDE':
sx += 1
elif i in 'SNZ':
xz += 1
elif i in 'OHIX':
qd += 1
elif i in 'FGJLPQR':
qt += 1
print ('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个'%(zy,sx,xz,qd,qt))
a = 'WEFNKDSPHOFGMAFW'
StrCount(a)
def strcount(st):
str_dict = {'左右对称':'AMTUVWY','上下对称':'BCDEK','旋转对称':'NSZ','全对称':'HIOX','其他字符':'FGJLPQR'}
count_dict = {'左右对称':0, '上下对称':0,'旋转对称':0,'全对称':0,'其他字符':0}
for each in st:
for key in str_dict.keys():
if each in str_dict:
count_dict += 1
break
result = '字符串中共有'
for key in count_dict.keys():
result += '%s%d个,'%(key,count_dict)
return result[:-1] + '。' def StrCount(str1):
i=0
j=0
k=0
m=0
n=0
dict1={'lr':'AMTUVWY','ud':'BCDEK','rotate':'NSZ','all':'HIOX','other':'FGJLPQR'}
for each_str in str1:
for each_item in dict1:
for each_str1 in dict1:
if each_str == each_str1:
if each_item=='lr':
i+=1
elif each_item=='ud':
j+=1
elif each_item=='rotate':
k+=1
elif each_item=='all':
m+=1
elif each_item=='other':
n+=1
print('字符串中共有左右对称字符%d,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其它字符%d个'%(i,j,k,m,n))
通过字典的方式遍历。。 def StrCount(str1):
count1,count2,count3,count4,count5 = 0,0,0,0,0
for i in str1:
if i in ('HIOX'):#全对称
count1+=1
elif i in ('BCDEK'):#上下对称
count2+=1
elif i in ('AMTUVWY'):#左右对称
count3+=1
elif i in ('SZN'):#旋转对称
count4+=1
else:
count5+=1
print('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个' % (count3,count2,count4,count1,count5))
str1=input('请输入一个字符串:')
StrCount(str1) 。。。 看看 想问怎么把26个字母分组,是人工分吗?
def StrCount(s):
return "字符串中共有左右对称字符%s个,上下对称字符%s个,旋转对称字符%s个,全对称字符%s个,其他字符%s个。"%tuple(len() for j in ('AMTUVWY','BCDEK','NSZ','HIOX','FGJLPQR')) def StrCount():
ALL='HIOX'
LR='AMTUVWY'
UPDOWN='BCDEK'
Rotate='NSZ'
al=0
lr=0
ud=0
rotate=0
els=0
str1='WEFNKDSPHOFGMAFW'
for e in str1:
if e in ALL:
al+=1
if e in LR:
lr+=1
if e in UPDOWN:
ud+=1
if e in Rotate:
rotate+=1
else:
els+=1
print ('字符串中共有左\
右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个。'
%(lr,ud,rotate,al,els))
if __name__=="__main__":
StrCount()
===================== RESTART: D:\Python36-32\caogao.py =====================
字符串中共有左右对称字符4个,上下对称字符3个,旋转对称字符2个,全对称字符2个,其他字符14个。 def strCount(string):
'统计字符串中左右对称0,上下对称1,旋转对称2,全对称字符3,及其他字符4'
alphabet = {
'A': 0,
'B': 1,
'C': 1,
'D': 1,
'E': 1,
'F': 4,
'G': 4,
'H': 3,
'I': 3,
'J': 4,
'K': 1,
'L': 4,
'M': 0,
'N': 2,
'O': 3,
'P': 4,
'Q': 4,
'R': 4,
'S': 2,
'T': 0,
'U': 0,
'V': 0,
'W': 0,
'X': 3,
'Y': 0,
'Z': 2
}
leftright = 0
topdown = 0
spin = 0
complete = 0
other = 0
for s in string:
if alphabet == 0:
leftright += 1
elif alphabet == 1:
topdown += 1
elif alphabet == 2:
spin += 1
elif alphabet == 3:
complete += 1
else:
other += 1
print('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个。' % (leftright, topdown, spin, complete, other))
strCount('WEFNKDSPHOFGMAFW') def StrCount(str1):
zuoyou='AMTUVWXY'
shangxia='BCDEK'
xuanzhuan='NSZ'
quanduichen = 'HIOX'
count_zuoyou = 0
count_shangxia = 0
count_xuanzhuan = 0
count_quanduichen = 0
count_qita = 0
for each in str1:
if each in zuoyou:
count_zuoyou += 1
elif each in shangxia:
count_shangxia += 1
elif each in xuanzhuan:
count_xuanzhuan += 1
elif each in quanduichen:
count_quanduichen += 1
else:
count_qita += 1
print("字符串中共有左右对称字符",count_zuoyou,"个,上下对称字符",count_shangxia,"个,旋转对称字符",count_xuanzhuan,"个,全对称字符",count_quanduichen,"个,其他字符",count_qita,"个")
工程狗 发表于 2017-9-6 12:33
def StrCount(str1):
zuoyou='AMTUVWXY'
shangxia='BCDEK'
弱弱的问一句,你们那个可以复制代码的是怎么弄上去的?? def StrCount(tt):
list1=['A','M','T','U','V','W','Y']
list2=['B','C','D','E','K']
list3=['N','S','Z']
list4=['H','I','O','X']
list5=['F','G','J','L','P','Q','R']
a = dict().fromkeys(range(5), 0)
for i in tt:
for x in range(1,6):
if i in list(str(x)):
a+=1
print('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个'%(a,a,a,a,a))
NN=input('请输入:')
StrCount(NN)
===================
输出错误。我有两个问题,1:怎么 在for x in range(1,6):
if i in list(str(x)):处让其判定是否在list(x)中的写法是什么(我这个有问题)
2: print('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个'%(a,a,a,a,a)) 处:
我想把%(a,a,a,a,a for i in range(5))怎么写?
土沙拉 发表于 2017-9-7 10:43
def StrCount(tt):
list1=['A','M','T','U','V','W','Y']
list2=['B','C','D','E','K']
第二个问题我解决了 tuple(a for i in range(5)就可以满足
第一个还是不行{:5_100:},求指教 str1 = 'WEFNKDSPHOFGMAFW'
def StrCount(Str):
count = *5
for i in list(Str):
if i in list("AMTUVWY"):
count+=1
elif i in list("BCDEK"):
count+=1
elif i in list("NSZ"):
count+=1
elif i in list("HIOX"):
count+=1
else:
count+=1
print("字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个。" % (count,count,count,count,count))
StrCount(str1)
结果:
============
字符串中共有左右对称字符4个,上下对称字符3个,旋转对称字符2个,全对称字符2个,其他字符5个。
>>> def strcount(strn):
zy,sx,xz,qdc,others = 0,0,0,0,0
for each in strn:
if each in 'AMTUVWY':
zy += 1
elif each in 'BCDEK':
sx += 1
elif each in 'NSZ':
xz += 1
elif each in 'HIOX':
qdc += 1
else:
others += 1
print('字符串中共有左右对称字符%d个,上下对称字符%d个,旋转对称字符%d个,全对称字符%d个,其他字符%d个。'%(zy,sx,xz,qdc,others))
土沙拉 发表于 2017-9-7 10:43
def StrCount(tt):
list1=['A','M','T','U','V','W','Y']
list2=['B','C','D','E','K']
可以用local(),这是一个字典,包括了所有定义了的变量。
if i in locals()['list' + str(x)]:
页:
[1]
2