python学习——练习题(1)

时间:2020-12-22 05:14:13
"""
题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
"""
import itertools def answer1():
"""自己思考完成,一开始以为两个循环就可以搞定了,结果还是要用三个循环;打印时只要效果在就好了,不用专门用int去组合成三位数字了"""
print("答案一", end=":")
x = ("1", "2", "3", "4")
r = range(0, 4)
n = 0
for i in r:
for j in r:
if i != j:
for k in r:
if k != i and k != j:
s = "%s%s%s" % (x[i], x[j], x[k])
n += 1
print(s, end=',')
print("共%d个" % n) answer1() def answer2():
"""参考答案,本以为自己用字符串组合,够简洁了,但参考答案直接输出个样子就得了"""
print("答案二", end=":")
r = range(1, 5)
n = 0
for i in r:
for j in r:
for k in r:
if i != j and i != k and j != k:
n += 1
print("%d%d%d" % (i, j, k), end=',')
print("共%d个" % n) answer2() def answer3():
"""利用列表来计算总数"""
print("答案三", end=":")
d = []
r = range(1, 5)
for a in r:
for b in r:
for c in r:
if a != b and a != c and b != c:
d.append("%d%d%d" % (a, b, c))
print(d, end=",")
print("共%d个" % len(d)) answer3() def answer4():
"""在列表里面使用for 和 if 操作可迭代数据"""
print("答案四", end=":")
arr = [1, 2, 3, 4]
newList = ["%d%d%d" % (i, j, k)for i in arr for j in arr for k in arr if i != j and i != k and j != k]
print(newList, end=",")
print("共%d个" % len(newList)) answer4() def answer5():
"""使用从最小值到最大值轮训查找, 注意在求十位上的数值时,先求模后整除"""
print("答案五", end=":")
r = range(123, 433)
n = 0
for i in r:
a = i % 10
b = (i % 100) // 10
c = i // 100
if a != b and a != c and b != c and 0 < a < 5 and 0 < b < 5 and 0 < c < 5:
n += 1
print(i, end=",")
print("共%d个" % n) answer5() def answer6():
"""使用集合的自动去重功能来生成三位数的组合"""
print("答案六", end=":")
r = range(1, 5)
n = 0
for i in r:
for j in r:
for k in r:
if len(set((i, j, k))) == 3:
n += 1
print("%d%d%d" % (i, j, k), end=",")
print("共%d个" % n) answer6() def answer7():
"""
利用python自带的排列方法,可以获取到所有的结果
combinations方法重点在组合,permutations方法重在排列。
还有就是,combinations和permutations返回的是对象地址,
原因是在python3里面,返回值已经不再是list,而是iterators(迭代器),
所以想要使用,只用将iterator 转换成list 即可, 还有其他一些函数返回的也是一个对象,需要list转换,比如 list(map())等
"""
print("答案七", end=":")
s = "1234"
resultList = ["".join(i) for i in list(itertools.permutations(s, 3))]
print(resultList, end=",")
print("共%d个" % len(resultList)) answer7() def answer8():
"""利用列表的删除机制来实现"""
print("答案八", end=":")
listNum = [1, 2, 3, 4]
n = 0
for i in listNum:
listNum1 = listNum.copy()
listNum1.remove(i)
for j in listNum1:
listNum2 = listNum1.copy()
listNum2.remove(j)
for k in listNum2:
n += 1
print("%d%d%d" % (i, j, k), end=",")
print("共%d个" % n) answer8() def answer9():
"""
利用位运算来实现,具体原理大概是这样的:
1,2,3,4 只有四个数字, 我们只要用两个二进制位(2bit)就可以表示:00,01,10,11;
而生成的三位数,我们可以用六个二进制位(6bit)来表示,如123可以表示为:00 01 10。
根据题目要求我们可以知道能生成的最大值和最小值是123和432 ,所以我们可以取区间range(123,433),可见answer5
用二进制替换就是range(00 01 10,11 10 10),转为十进制就是range(6,58).
将i右移4位再和3进行与运算,可以获取百位的数值,例如234,可以表示01 10 11(转成十进制为27,在6-58之间),
右移4位得到01,再&3(二进制即11)可将01左边清零,得到想要的01,获取十位上的数值,就只要右移2位即可,&3都是为了左边清零,
异或运算其实就是不等于运算,最后加一是将00,01,10,11 转为对应的1,2,3,4
"""
print("答案九", end=":")
r = range(6, 58)
n = 0
for i in r:
a = i >> 4 & 3
b = i >> 2 & 3
c = i & 3
if a ^ b and b ^ c and c ^ a:
n += 1
print("%d%d%d" % (a+1, b+1, c+1), end=",")
print("共%d个" % n) answer9()