1.sdut - use the function to output the value of Fibonacci sequence and the number of Fibonacci values within the specified range (10 points)
Design function implementation: calculate the value of the nth Fibonacci sequence, and calculate the number of all Fibonacci numbers between the closed interval [a,b] (0 < a < B ≤ 100000) composed of two positive integers a and B.
Fibonacci sequence, any number is the sum of the first two terms (the first two terms are defined as 1), fib(0)=fib(1)=1.
Function interface definition:
fib(n) #Function 1 fibs(a, b) #Function 2
fib(n): returns the Fibonacci number of the nth item;
fibs(a, b): returns a list of all Fibonacci numbers in [a, b].
n. a and b are positive integers.
Example of referee test procedure:
/* Please fill in the answer here */ n,a,b=input().split() n=int(n) a=int(a) b=int(b) fb=fib(n) print("fib({0}) = {1}".format(n,fb)) fiblist=fibs(a, b) print(len(fiblist))
Input example:
A set of inputs is given here. For example:
6 20 100
No blank lines at the end
Output example:
The corresponding output is given here. For example:
fib(6) = 13
4
No blank lines at the end
def fib(n): f1 = 1 f2 = 1 for i in range(n): f1, f2 = f2, f1 + f2 return f1 def fibs(a, b): l = list() i = 0 while True: if a <= fib(i) <= b: l.append(fib(i)) if fib(i) > b: break i += 1 return l
2. Calculate the area according to the length of three sides of the triangle, and throw an exception if the triangle cannot be formed legally (higher education society, exercise 12-4 of fundamentals and applications of Python Programming) (10 points)
The following program reads the three side length (decimal) of the triangle from the keyboard, and then uses Helen's formula to calculate the area of the triangle. If the input length of three sides can form a legal triangle, output the calculated triangle area, otherwise output "cannot form a legal triangle".
Please complete the following program. Please note that the error output in the program depends on Python's exception handling mechanism.
Function interface definition:
Please complete the code to complete the corresponding functions.
The missing code here is not a function.
Example of referee test procedure:
from math import sqrt def Area(a,b,c): p=(a+b+c)/2 s=sqrt(p*(p-a)*(p-b)*(p-c)) return s ans=0 a = float(input()) b = float(input()) c = float(input()) #Complete the code here #try .... #... #... except ValueError: print("Cannot form a legal triangle") else: print("%.2f" % ans)
Input example:
3
3.1
10.8
No blank lines at the end
Output example:
Cannot form a legal triangle
No blank lines at the end
try: if a+b>c and a+c>b and b+c>a: ans=Area(a,b,c) else: raise ValueError
3.sdut- look up the dictionary (10 points)
What if you don't know the words? Look it up in the dictionary!
It is known that there are n words in the dictionary. There are m unknown words. Ask whether these m words appear in the dictionary. If they are in the dictionary, output their meaning. If they are not in the dictionary, output: Not found!
Input format:
Contains multiple sets of test cases.
Input n and m (n > = 0 & & n < = 100000 & & M > = 0 & & M < = 100000) in the first line are n words existing in the dictionary and M words to query
Immediately followed by n lines, representing the words existing in the dictionary and their interpretation (meaning);
Then m lines, m words to query.
If n = 0 & & M = 0, the program ends
Output format:
For the word to be queried, if it exists in the dictionary, its explanation (meaning) will be output. If it does not exist, Not found will be output!
Input example:
3 2
red: having the colour of blood or fire
green:having the colour of grass or the leaves of most plants and trees
blue:having the colour of a clear sky or the sea/ocean on a clear day
blue
abcded
0 0
No blank lines at the end
Output example:
having the colour of a clear sky or the sea/ocean on a clear day
Not found!
No blank lines at the end
while True: n, m = map(int, input().split(" ")) if n == 0 and m == 0: break d = dict() for i in range(n): keys, values = input().split(":") d[keys] = values for j in range(m): word = input() if word in d.keys(): print(d[word]) else: print('Not found!')
4. Add elements to the set (20 points)
To add a single element to a collection, you can use the. add() method operation, which will add to the collection. Apply your knowledge to help your good friend Xiao Ming. Xiao Ming likes collecting stamps. He has a large number of stamps from different countries. He decided to count the number of stamps from different countries in his stamp album. He asked you for help. You take out N stamps one by one and find out how many countries there are without repetition.
Input format:
The first line of the first line input an integer N in the first line, the first line, the first line, the first line, the first line input an integer N, representing the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps, the number of all stamps 82 82 82 82 8236 82 8236 82 82 82 82 8236 82 82 82 82 82 82 82 82 82 82 82 82 8236 82 82 82 82 82 82 82 82 82 8236 \82 82 \82 82 82 82 \82 82 82 \828236;
In the next N lines, enter the name of which country a stamp comes from
Output format:
Output the number of countries that are not duplicated in one line.
Input example:
7
UK
China
USA
France
New Zealand
UK
France
No blank lines at the end
Output example:
5
n = eval(input()) s = set() for i in range(n): s.add(input()) print(len(s))
5. Collective application - list (20 points)
A collection is an unordered data type with no duplicate elements. When it is output or converted to a sequence type, its element positions appear randomly.
Li Bai is a teacher in a community university. One day, he asked his student Xiao Ming to calculate the average height of plants in the greenhouse. The \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ average height = number of different heights and / or number of different heights
Input format:
Enter the height of N plants separated by spaces
Output format:
Output the average height of plants in one line, with 3 decimal places reserved
Input example:
161 182 161 154 176 170 167 171 170 174
No blank lines at the end
Output example:
169.375
n = set(map(int, input().split(" "))) print('{:.3f}'.format(sum(n) / len(n)))
6. Dictionary query 1 - query provincial capital (30 points)
The premier's poems include: two lakes, two cantons, two rivers and mountains, five rivers, clouds, GUI, Fujian, four West, two Ning, Qing, Gansu and Shaanxi, and inner Taipei to heaven. The dictionary data of China's provinces, municipalities directly under the central government, autonomous regions and special administrative regions are as follows:
capitals = {'Hunan':'changsha ',' Hubei ':'wuhan', 'Guangdong':'Guangzhou ',' Guangxi ':'nanning', 'Hebei':'shijiazhuang ',' Henan ':'zhengzhou', 'Shandong':'jinan ',' Shanxi ':'taiyuan', 'Jiangsu':'nanjing ',' Zhejiang ':'hangzhou', 'Jiangxi':'nanchang ',' Heilongjiang ':'harbin', 'Xinjiang':'urum ' Qi, Yunnan, Kunming, Guizhou, Guiyang, Fujian, Fuzhou, Jilin, Changchun, Anhui, Hefei, Sichuan, Chengdu, Tibet, Lhasa, Ningxia, Yinchuan, Liaoning, Shenyang, Qinghai, Xining, Hainan, Haikou, Gansu, Lanzhou, Shaanxi, Xi'an‘ Inner Mongolia:'hohhot ',' Taiwan ':'taipei', 'Beijing':'Beijing ',' Shanghai ':'shanghai', 'Tianjin':'tianjin ',' Chongqing ':'chongqing', 'Hong Kong':'hong Kong ',' Macao ':'macao'}
Design a program to receive the names of provinces, municipalities directly under the central government, autonomous regions and special administrative regions input by the user, output the corresponding provincial capital name, and output "input error" when input error.
Input format:
On the first line, enter an integer n
The next line is the n line, and the next line is the next input for the n line. Each line inputs the name of a province, municipality, autonomous region, or special administrative region, or the name of a province, municipality, or autonomous region or special administrative region, or the name 82 \82 82 82 \82 \\\ \\\\\\\\\\\\\\\\
Output format:
Output the corresponding provincial capital name
Input example 1:
2
Xinjiang
Beijing
Output example 1:
Urumqi
Beijing
Input example 1:
1
U.S.A
Output example 1:
Input error
capitals = {'Hunan': 'Changsha', 'Hubei': 'Wuhan', 'Guangdong': 'Guangzhou', 'Guangxi': 'Nanning', 'Hebei': 'Shijiazhuang', 'Henan': 'Zhengzhou', 'Shandong': 'Jinan', 'Shanxi': 'Taiyuan', 'Jiangsu': 'Nanjing', 'Zhejiang': 'Hangzhou', 'Jiangxi': 'Nanchang', 'Heilongjiang': 'Harbin', 'Xinjiang': 'Urumqi', 'Yunnan': 'Kunming', 'Guizhou': 'Guiyang', 'Fujian': 'Fuzhou', 'Jilin': 'Changchun', 'Anhui': 'Hefei', 'Sichuan': 'Chengdu', 'Tibet': 'Lhasa', 'Ningxia': 'Yinchuan', 'Liaoning': 'Shenyang', 'Qinghai': 'Xining', 'Hainan': 'Haikou', 'Gansu': 'Lanzhou', 'Shaanxi': 'Xi'an', 'Inner Mongolia': 'Hohhot', 'Taiwan': 'Taipei', 'Beijing': 'Beijing', 'Shanghai': 'Shanghai', 'Tianjin': 'Tianjin', 'Chongqing': 'Chongqing', 'Hong Kong': 'Hong Kong', 'Macao': 'Macao'} n = eval(input()) for i in range(n): s = input() if s in capitals.keys(): print(capitals[s]) else: print("Input error")
7. Dictionary application - user login (30 points)
There are dictionaries as follows: dic = {'admin':'123456 ',' administrator ':'12345678', 'root':'password '} enables the user to enter the user name and password. When the user name matches the password and the key value pair in the dictionary, it displays "login success", otherwise it displays "login failure". When the login fails, it is allowed to enter it three times.
Input format:
Enter the user name and password on both lines
Output format:
"Login succeeded" or "login failed"
Input example:
admin
12345678
admin
123456
No blank lines at the end
Output example:
Login failed
Login succeeded
dic = {'admin': '123456', 'administrator': '12345678', 'root': 'password'} for i in range(3): user = input() p = input() if user in dic.keys() and p == dic[user]: print("Login succeeded") break else: print("Login failed")
8. List operation (10 points)
Enter a positive integer n between 1 and 100, and randomly generate a positive integer m not greater than n with n as the random number seed. Generate a list LS containing elements 1, 2, 3... N, delete the elements with the integer multiple of m in the list ls, and output the original list and the list after deleting the multiple of m in two lines.
Input format:
Enter a positive integer n between 1 and 100
Output format:
Two rows, one list per row
Input example:
A set of inputs is given here. For example:
20
No blank lines at the end
Output example:
The corresponding output is given here. For example:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19]
No blank lines at the end
import random n = eval(input()) ls = list() for i in range(1, n + 1): ls.append(i) print(ls) random.seed(n) m = random.randint(1, n) i = 1 while True: if m * i > n: break else: if m * i in ls: ls.remove(m * i) i += 1 print(ls)
9. Peculiar four digits (10 points)
A four digit number. The numbers are different from each other. The sum of all numbers is 6, and this number is a multiple of 11. How many four digits meet this requirement? What are each?
Input format:
The title is not entered
Output format:
The first line outputs the number of qualified numbers
The second line outputs all four digits that meet the conditions in the form of a list, and the list elements are arranged in order from small to large
Input example:
A set of inputs is given here. For example:
No blank lines at the end
Output example:
The corresponding output is given here. For example:
6
[1023, 1320, 2013, 2310, 3102, 3201]
ls = list() for i in range(1000, 10000): if sum(list(map(int, str(i)))) == 6 and i % 11 == 0 and len(str(i)) == len(set(str(i))): ls.append(i) print(len(ls)) print(ls)
10. Print Friendly number (3 points)
kekao defines a number called friendly number.
The definition of friendly number is as follows: "for a positive integer a, if the sum of each number of a is even, then a is a friendly number".
Given the interval l, r, find all friendly numbers in the interval.
Input format:
Input two positive integers l,r input data to ensure 1 < l < R < 10 ^ 6
Output format:
Output all friendly numbers in the interval [l, r]. The friendly numbers are separated by spaces. If there is no friendly number, output 0
Input example:
A set of inputs is given here. For example:
3 5
No blank lines at the end
Output example:
The corresponding output is given here. For example:
4
No blank lines at the end
def fun(x): sum = 0 while x > 0: sum += x % 10 x = x // 10 return sum l, r = map(int, input().split(" ")) flag = 0 s = '' for i in range(l, r + 1): if fun(i) % 2 == 0: s += str(i)+' ' flag = 1 if flag == 0: print(flag) else: print(s.rstrip())