当前位置:网站首页>写代码也有本手俗手之分,而我们要善于发现妙手!
写代码也有本手俗手之分,而我们要善于发现妙手!
2022-06-12 15:37:00 【Hann Yang】

一年一度的高考结束了,准大一的学子们今天起你们不用去深究何是“本手”何是“俗手”,放松起来去疯狂地玩吧! 特爱学编程的孩子们在高中三年里也根本挤不出时间学,这个假期先轻松放空一段时间后,来CSDN学编程吧!写代码也有本手、俗手之分的,规范中规中矩的代码就似本手;乱七八糟bug满天飞的即是俗手;而代码高手写的叫一个灵动飘逸,就似围棋高手在手谈中“妙手”不断、精妙绝伦也。我先声明我不是一个编程高手,只是段外的业余初学者,但也不乏寻找“神之一手”的勇气和努力!这里是CSDN博客频道,一个业余 pyer 在这里开始发现妙手之旅——
1. 一行代码实现素数判断,有以下二种方法:
def isprime(n):
return not any(filter(lambda i:not n%i and i*i<=n,range(2,n))) if n>1 else False
isPrime = lambda n:not any(not n%i for i in range(2,n) if i*i<=n) if n>1 else False2. 列表索引代替多重 if-elif-elif-else 结构:
【实例】折扣与数量的关系:
if n==1:
discount = 1.0
elif n==2:
discount = 0.95
elif n==3:
discount = 0.9
elif n==4:
discount = 0.8
else:
discount = 0.65索引代替后:
n = int(input('购买数量:'))
price = 499.0
rates = [1.0,0.95,0.9,0.8,0.65]
discount = lambda n:rates[-1 if divmod(n,len(rates))[0] else divmod(n,len(rates))[1]-1]
total = n*price*discount(n)
print(f'折扣率:{discount(n)}\n实际消费金额:{total:.2f}')
print('折扣率测试:')
for i in range(1,10):
print(i,discount(i))3. 单独引用某库某个函数: __import('库名')__.函数名()
【实例1】排列:
print(*(__import__('itertools').permutations(range(1,5),3)))
(1, 2, 3) (1, 2, 4) (1, 3, 2) (1, 3, 4) (1, 4, 2) (1, 4, 3) (2, 1, 3) (2, 1, 4)
(2, 3, 1) (2, 3, 4) (2, 4, 1) (2, 4, 3) (3, 1, 2) (3, 1, 4) (3, 2, 1) (3, 2, 4)
(3, 4, 1) (3, 4, 2) (4, 1, 2) (4, 1, 3) (4, 2, 1) (4, 2, 3) (4, 3, 1) (4, 3, 2)【实例2】组合:
list(__import__('itertools').combinations(range(1,5),3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]【实例3】三角函数:
__import__('math').sin(__import__('math').pi/6)
0.499999999999999944. functools.reduce()的运用:某区间内某些数的积
【实例1】阶乘公式:
fact = lambda n:__import__('functools').reduce(lambda x,y:x*y,range(1,n+1))【实例2】10~n间,7的倍数的累积:
n = int(input())
print(__import__('functools').reduce(lambda x,y:x*y,filter(lambda x:x%7==0,range(10,n+1))))【实例3】20以内所有素数之积:
isPrime = lambda n:not any(not n%i for i in range(2,n) if i*i<=n) if n>1 else False
print(__import__('functools').reduce(lambda x,y:x*y,filter(isPrime,range(20))))【实例4】正整数的各位数字之积:
num = ''
while not num.isnumeric():
num=input('请输入一个正整数:')
else:
print('各位上的数字之积:',__import__('functools').reduce(lambda x,y:int(x)*int(y),num))5. 判断对象obj是否可迭代: 可以使用next(obj)的
>>> from collections.abc import Iterator
>>> isinstance(map(len,[[],[1],[2,3]]), Iterator)
True
>>> isinstance(filter(len,[[],[1],[2,3]]), Iterator)
True
>>> isinstance(__import__('itertools').combinations(range(1,5),3), Iterator)
True
>>> isinstance((i for i in range(100)), Iterator)
True
>>> isinstance([i for i in range(100)], Iterator)
False
>>> isinstance({i for i in range(100)}, Iterator)
False
>>> isinstance({i:i*i for i in range(100)}, Iterator)
False6. 元组或列表中出现次数最少的所有元素:
nums = eval(input()) # 题目要求输入用半角逗号分隔的,只要用eval()函数即可
dic = {n:nums.count(n) for n in nums} # 以 数字:次数 为键值对组成字典
count = min(dic.values()) # 最少出现次数
print(*(k for k,v in dic.items() if v==count)) # 键值最小的所有键7. 实例:有四个数能组成多少个互不相同且无重复数字的三位数,各是多少?
res = set()
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if i!=j!=k!=i:
res.add(i*100+j*10+k)
print(len(res))
print(res)或者:
res = set()
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if len(set([i,j,k]))==3:
res.add(i*100+j*10+k)
print(len(res))
print(res)或者:
from itertools import permutations as perm
lst = [i*100+j*10+k for i,j,k in perm([1,2,3,4],3)]
print(len(lst))
print(lst)8. 实例:多项式求和 Sn=1-2!/3+3!/5-....+(-1)^(n+1)*n!/(2n-1)
m = int(input("输入正整数:"))
f,s,t = -1.0,0,1
for n in range(1,m+1):
t *= n
f *= -1
s += f*t/(2*n-1)
print(f'总和为{s:.3f}')本集完,也即《Python 在问答频道中刷题积累到的小技巧》连载之(四)
边栏推荐
- nohup 命令使用
- Introduction to Eureka
- Two ways of array simulating queue
- Fiddler packet capturing (mobile app)
- [jvm learning] local method stack and heap
- Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
- Distributed concurrent repeated submission
- Five models of software testing
- Codeworks round 797 (Div. 3, cf1690)
- Jupyter notebook new environment shortcut
猜你喜欢

Use of boost:: bind() in ROS

POSTMAN-REST Client插件的应用

org. xml. sax. SAXParseException; lineNumber: 63; columnNumber: 10; The definition of "mapper" in the related type must be matched with "(CAC

How to use grafana to easily realize OVL data visualization

为什么阿里巴巴不建议MySQL使用Text类型?

Understanding of dart typedef

Microservice fault tolerance

任务 输出密雪冰城主题曲 0612

Acwing summer daily question (sexy prime number on June 10)

FPGA (III) trigger and latch
随机推荐
Tcp/ip three handshakes and four waves (interview questions)
How to use grafana to easily realize OVL data visualization
Solutions to some problems of scuacm22 retreat competition before summer training
idea 拉取分支代码
Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
MySQL开发注意事项(阿里巴巴开发手册)
Acwing暑期每日一题(6月10日性感素数)
FPGA (III) trigger and latch
Import and export steps of SQL Server 2008
IGMP message (tcp/ip details volume 1/ Volume 2)
tinyint和int区别
增加mysql的最大连接数
5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
Five models of software testing
Escape analysis of golang compiler
Use of multithreading
Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
Find the number of cells (connectivity map, wide search, deep search)
ARM 64指令小记
Introduction to resttemplate