100+Python挑战性编程练习系列 -- day 3
Question 10
编写一个程序,接受一系列空格分隔的单词作为输入,并在删除所有重复的单词并按字母数字排序后打印这些单词。
假设向程序提供以下输入:
hello world and practice makes perfect and hello world again
然后,输出应为:
again and hello makes perfect practice world
方法1:
word = input().split()
for i in word:
if word.count(i) > 1: #count function returns total repeatation of an element that is send as argument
word.remove(i) # removes exactly one element per call
word.sort()
print(" ".join(word))
方法2:
word = sorted(list(set(input().split()))) # input string splits -> converting into set() to store unique
# element -> converting into list to be able to apply sort
print(" ".join(word))
Question 11
写一个程序,它接受一个由逗号分隔的4位二进制数序列作为输入,然后检查它们是否能被5整除。可被5整除的数字将以逗号分隔的顺序打印。
示例:
0100,0011,1010,1001
那么输出应该是:
1010
方法1:
def check(x): # converts binary to integer & returns zero if divisible by 5
total,pw = 0,1
reversed(x)
for i in x:
total+=pw * (ord(i) - 48) # ord() function returns ASCII value
pw*=2
return total % 5
data = input().split(",") # inputs taken here and splited in ',' position
lst = []
for i in data:
if check(i) == 0: # if zero found it means divisible by zero and added to the list
lst.append(i)
print(",".join(lst))
方法2:
def check(x): # check function returns true if divisible by 5
return int(x,2)%5 == 0 # int(x,b) takes x as string and b as base from which
# it will be converted to decimal
data = input().split(',')
data = list(filter(check,data)) # in filter(func,object) function, elements are picked from 'data' if found True by 'check' function
print(",".join(data))
方法3:
data = input().split(',')
data = [num for num in data if int(num, 2) % 5 == 0]
print(','.join(data))
Question 12
写一个程序,它将找到所有这样的数字之间的1000和3000(包括),使每个数字的数字是一个偶数。所获得的数字应以逗号分隔的顺序打印在一行上。
方法1:
lst = []
for i in range(1000,3001):
flag = 1
for j in str(i): # every integer number i is converted into string
if ord(j)%2 != 0: # ord returns ASCII value and j is every digit of i
flag = 0 # flag becomes zero if any odd digit found
if flag == 1:
lst.append(str(i)) # i is stored in list as string
print(",".join(lst))
方法2:
def check(element):
return all(ord(i)%2 == 0 for i in element) # all returns True if all digits i is even in element
lst = [str(i) for i in range(1000,3001)] # creates list of all given numbers with string data type
lst = list(filter(check,lst)) # filter removes element from list if check condition fails
print(",".join(lst))
方法3:(一行)
print(','.join([str(num) for num in range(1000, 3001) if all(map(lambda num: int(num) % 2 == 0, str(num)))]))
Question 13
写一个程序,接受一个句子并计算字母和数字的数量。
假设向程序提供以下输入:
hello world! 123
然后,输出应为:
LETTERS 10
DIGITS 3
方法1:
word = input()
letter,digit = 0,0
for i in word:
if ('a'<=i and i<='z') or ('A'<=i and i<='Z'):
letter+=1
if '0'<=i and i<='9':
digit+=1
print("LETTERS {0}\nDIGITS {1}".format(letter,digit))
方法2:
word = input()
letter, digit = 0,0
for i in word:
if i.isalpha(): # returns True if alphabet
letter += 1
elif i.isnumeric(): # returns True if numeric
digit += 1
print(f"LETTERS {letter}\n{digits}") # two different types of formating method is shown in both solution
方法3:
#using reduce for to count
from functools import reduce
def count_letters_digits(counters,char_to_check):
counters[0] += char_to_check.isalpha()
counters[1] += char_to_check.isnumeric()
return counters
print('LETTERS {0}\nDIGITS {1}'.format(*reduce(count_letters_digits,input(),[0,0])))
所有上述问题大多是字符串相关的问题。解决方案的主要部分包括字符串相关函数和理解方法,以更短的形式写下代码。