---16---
Question:
>Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.Suppose the following input is supplied to the program:
1,2,3,4,5,6,7,8,9
>Then, the output should be:
1,9,25,49,81
Solution:
解法一:

解法二:


---17---
Question:
>Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:
D 100
W 200
D means deposit while W means withdrawal.D表示存款,W表示取款。
Suppose the following input is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be: 500
Solution:
解法一:

解法二:


解法三:


---18---
Question:
>A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
>Following are the criteria for checking the password:
At least 1 letter between [a-z]
At least 1 number between [0-9]
At least 1 letter between [A-Z]
At least 1 character from [$#@]
Minimum length of transaction password: 6
Maximum length of transaction password: 12
>Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
>Example :If the following passwords are given as input to the program:
ABd1234@1,a F1#,2w3E*,2We3345
>Then, the output of the program should be: ABd1234@1
Solution:
解法一


解法二

输出结果:

以上两种代码逻辑比较清晰,但代码较长,不够简洁,下面导入re模块,使代码更简单。
解法三


解法四


---19---
Question:
>You are required to write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:
1: Sort based on name
2: Then sort based on age
3: Then sort by score
>The priority is that name > age > score.
>If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Hints:
>In case of input data being supplied to the question, it should be assumed to be a console input.We use itemgetter to enable multiple sort keys.
Solution:


---20---
Question:
>Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
Solution:


19-20题不太理解~
网友评论