Python编程函数题【第十一周】
一、稀疏矩阵的表示
m,n = input().split(',')
m = eval(m)
n = eval(n)
flag = 0
lst1 = []
lst2 = []
for i in range(m):
lst = input().split()
if len(lst) != n:
flag = 1
break
lst1.append(list(map(eval,lst)))
for j in range(len(lst)):
if float(lst[j]) != 0.0:
lst2.append((i+1,j+1,eval(lst[j])))
if flag == 1:
print(f'矩阵第{i+1}行输入错误!')
else:
y = len(lst2)
lst2.append((m,n,y))
print('Triplet List:',tuple(lst2),sep='\n')
print('Transpose matrix:')
for i in range(n):
for j in range(m):
print('{0:>6}'.format(lst1[j][i]),end='')
print()
二、字符串的重新排列
sli = list(input())
ili = list(map(int,input().split(",")))
res = ["" for i in range(len(sli))]
for i in range(len(sli)):
res[ili[i]] = sli[i]
print("".join(res))
三、下三角矩阵判断
#include <iostream>
using namespace std;
int a[1010][1010];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
int flag=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]!=0&&i<j)
{
flag=0;
}
}
}
if(flag==1)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
return 0;
}
四、列表元素排序【part two】
l = eval(input())
def paixv(i):
sum = 0
for j in i:
if (isSuShu(j)):
sum += j
return sum
def isSuShu(num):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
return False
else:
return True
else:
return False
l.sort(key=paixv)
print(l)
五、反转单词中的字母
s = list(input())
s.reverse()
for i in s:
if i==" ":
print("#",end="")
if i>='a' and i<='z':
print(i.upper(),end="")
if i>='A' and i<='Z':
print(i.lower(),end="")
六、分段函数
n = eval(input())
if n>=0 and n<2:
y=2.5-n
elif n>=2 and n<4:
y=2-1.5*(n-3)*(n-3)
elif n>=4:
y=n/2-1.5
print("{:.3f}".format(y))
|