用于检查几乎相似字符串的Python程序
给定两个字符串,这里的任务是编写一个Python程序来测试它们是否几乎相似。字符串的相似性正在根据每个字符的频率差异标准进行检查,该标准应大于此处由 K 表示的阈值。
Input : test_str1 = ‘aabcdaa’, test_str2 = “abbaccd”, K = 2
Output : True
Explanation : ‘a’ occurs 4 times in str1, and 2 times in str2, 4 – 2 = 2, in range, similarly, all chars in range, hence true.
Input : test_str1 = ‘aabcdaaa’, test_str2 = “abbaccda”, K = 3
Output : True
Explanation : ‘a’ occurs 5 times in str1, and 3 times in str2, 5 – 3 = 2, in range, similarly, all chars in range, hence true.
方法 1:使用ascii_lowecase 、字典理解、循环和abs()
在这里,我们使用字典理解和循环计算两个字符串中所有字符的所有频率。接下来,从字母小写 ascii字符迭代每个字符,并使用 abs() 测试两个字符串中的频率差异,如果计算出的任何差异大于 K,则结果被标记为关闭。
例子
Python3
from string import ascii_lowercase
# function to compute frequencies
def get_freq(test_str):
# starting at 0 count
freqs = {char: 0 for char in ascii_lowercase}
# counting frequencies
for char in test_str:
freqs[char] += 1
return freqs
# initializing strings
test_str1 = 'aabcdaa'
test_str2 = "abbaccd"
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 2
# getting frequencies
freqs_1 = get_freq(test_str1)
freqs_2 = get_freq(test_str2)
# checking for frequencies
res = True
for char in ascii_lowercase:
if abs(freqs_1[char] - freqs_2[char]) > K:
res = False
break
# printing result
print("Are strings similar ? : " + str(res))
Python3
from collections import Counter
# initializing strings
test_str1 = 'aabcdaa'
test_str2 = "abbaccd"
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 2
# extracting frequencies
cnt1 = Counter(test_str1.lower())
cnt2 = Counter(test_str2.lower())
# getting maximum difference
res = True
if max((cnt1 - cnt2).values()) > K or max((cnt2 - cnt1).values()) > K:
res = False
# printing result
print("Are strings similar ? : " + str(res))
输出:
The original string 1 is : aabcdaa
The original string 2 is : abbaccd
Are strings similar ? : True
方法 2:使用Counter()和max()
在这里,我们使用 Counter() 执行获取单个字符频率的任务,并使用 max() 获取最大差异,如果大于 K,则结果被标记掉。
例子:
蟒蛇3
from collections import Counter
# initializing strings
test_str1 = 'aabcdaa'
test_str2 = "abbaccd"
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 2
# extracting frequencies
cnt1 = Counter(test_str1.lower())
cnt2 = Counter(test_str2.lower())
# getting maximum difference
res = True
if max((cnt1 - cnt2).values()) > K or max((cnt2 - cnt1).values()) > K:
res = False
# printing result
print("Are strings similar ? : " + str(res))
输出:
The original string 1 is : aabcdaa
The original string 2 is : abbaccd
Are strings similar ? : True