Chapter 4 -11 determine prime numbers (20 minutes)
defisprime(n):
res ="Yes"if n <=2:
res =Falseelse:for i inrange(2,n):if n % i ==0:
res ="No"breakreturn res
n =int(input())for i inrange(n):
number =int(input())print(isprime(number))
Chapter 4-12 seek to meet the conditions of the Fibonacci (30 points)
deffib(n):
x1 =1
x2 =1
result =1if n <=2:passelse:for i inrange(n-2):
result = x1 + x2
x1 = x2
x2 = result
return result
n =int(input())
start =6while fib(start)< n:
start +=1print(fib(start))
Chapter 4-13 seek error is smaller than the input value e approximation (20 minutes)
import math
defget_e(n):
summ =1for i inrange(1,n+1):
summ +=1/math.factorial(i)return summ
e1 =float(input())
start =1whileTrue:if get_e(start+1)-get_e(start)< e1:print("{:.6f}".format(get_e(start+1)))breakelse:
start +=1
Chapter 4-14 characters statistics (15 points)
letter =0
blank =0
digit =0
other =0
count =0
list2 =[]whileTrue:
list1 =list(input())
count +=1for i in list1:
list2.append(i)iflen(list2)+ count >10:
count -=1breakfor i in list2:if i.isalpha():
letter +=1elif i ==" "or i =="\n":
blank +=1elif i.isdigit():
digit +=1else:
other +=1print("letter = {0:}, blank = {1:}, digit = {2:}, other = {3:}".format(letter,blank + count,digit,other))
a,b,c =input().split()
a =int(a)
b =int(b)
c =int(c)if a + b > c and a + c > b and b + c > a:print("yes")else:print("no")
Chapter 4-17 narcissistic number (20 points) (20 points)
n =int(input())for i inrange(pow(10,n-1),pow(10,n)):
summ =0
tmp =str(i)
tmp_list =[]for j in tmp:
tmp_list.append(int(j))for k in tmp_list:
summ +=pow(k,n)if summ == i:print('{:}'.format(i))
Chapter 4-18 monkey king election (20 points)
n =int(input())
monkey =[]if n ==1:print("1")else:for i inrange(1,n+1):
monkey.append(i)whilelen(monkey)>=3:
monkey.pop(2)
monkey.append(monkey[0])
monkey.append(monkey[1])
monkey = monkey[2:]print(monkey[1])
Matrix operation Chapter 4-19 (20 minutes)
n =int(input())
list1 =[]for i inrange(n):
tmp =[]
tmp_list =input().split()for i in tmp_list:
tmp.append(int(i))
list1.append(tmp)
summ =0
useless =0for i inrange(n):
summ +=sum(list1[i])
useless += list1[i][-1]for i inrange(1,n-1):
useless += list1[i][-i-1]print(summ - useless-sum(list1[-1])+list1[-1][-1])
Chapter elements in the rows of the Matrix and 4-20 (15 minutes)
m,n =input().split()
m =int(m)
n =int(n)
list1 =[]for i inrange(m):
list2 =input().split()
tmp =[]for i in list2:
tmp.append(int(i))
list1.append(tmp)for j in list1:print(sum(j))