如何解决Python:查找包含通配符的字符串中是否存在子字符串
我想创建一个接受两个输入 fullstring
和 substring
的函数。如果完整字符串中存在子字符串,则函数应返回 True
。否则,它将返回 False
。如果子字符串包含通配符 (*),则通配符可以表示任何单个字符。
例如:
arg1: fullstring = "hitherehello"
arg2: substring = "the*e
output: True
我尝试过的: 下面是一个将子字符串与父字符串匹配的函数,但我似乎无法弄清楚如何集成通配符。
def count_substring(string,sub_string):
len_ss = len(sub_string)
for i in range(len(string) - len_ss + 1):
if string[i:i+len_ss] == sub_string:
return True
return False
约束:
我不能使用正则表达式或内置的 Python 函数,例如 find
。
解决方法
您可以查找字符串中的每个“通配符”部分,返回位置并随后检查是否有后续:
def count_substring(string,sub_string,lastIndex):
len_ss = len(sub_string)
for i in range(len(string) - len_ss + 1):
if string[i:i + len_ss] == sub_string and lastIndex < i + len_ss:
return i + len_ss
return -1
def count_wrapper(string,index):
positions = []
for wild_sub_string in sub_string.split('*'):
index = count_substring(string,wild_sub_string,index)
positions.append(index)
# check pairwise
return all([x + 2 == y for x,y in zip(*[iter(positions)] * 2)])
print(count_wrapper("there","the*e",0))
print(count_wrapper("thera",0))
print(count_wrapper("theresa","the*e*a",0))
出:
True
False
True
,
import java.util.*;
public class ArrayStatistics {
public static void main(String[] args) {
double total = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of your array >> ");
int size = input.nextInt();
double[] myArray = new double[size];
System.out.print("Enter the integer values >> ");
for (int i=0; i<size; i++) {
myArray[i] = input.nextInt();
}
System.out.println("\nIntegers:");
for (int i=0; i<size; i++) {
System.out.println(myArray[i]);
}
double mean = calculateMean(myArray);
System.out.println("\nMean: " + mean);
double mode = calculateMode(myArray);
System.out.println("Mode: " + mode);
double median = calculateMedian(myArray);
System.out.println("Median: " + median);
double SD = calculateSD(myArray);
System.out.format("Standard Deviation: %.6f",SD);
}
public static double calculateMean(double myArray[]) {
int sum = 0;
for(int i = 0; i<myArray.length; i++) {
sum = (int) (sum + myArray[i]);
}
double mean = ((double) sum) / (double)myArray.length;
return mean;
}
public static double calculateMode(double myArray[]) {
int modeCount = 0;
int mode = 0;
int currCount = 0;
for(double candidateMode : myArray) {
currCount = 0;
for(double element : myArray) {
if(candidateMode == element) {
currCount++;
}
}
if(currCount > modeCount) {
modeCount = currCount;
mode = (int) candidateMode;
}
}
return mode;
}
public static double calculateMedian(double myArray[]) {
Arrays.sort(myArray);
int val = myArray.length/2;
double median = ((myArray[val]+myArray[val-1])/2.0);
return median;
}
public static double calculateSD(double myArray[]) {
double sum = 0.0;
double standardDeviation = 0.0;
int length = myArray.length;
for(double num : myArray) {
sum += num;
}
double mean = sum/length;
for(double num : myArray) {
standardDeviation += Math.pow(num - mean,2);
}
return Math.sqrt(standardDeviation/length);
}
打印:
def search(fullstring,substring):
def check(s1,s2):
for a,b in zip(s1,s2):
if a != b and b != "*":
return False
return True
for i in range(len(fullstring) - len(substring) + 1):
if check(fullstring[i : i + len(substring)],substring):
return True
return False
print(search("hitherehello","the*e"))
更多测试:
True
,
这似乎对我有用:
def check_all_substrings(index,string,substrings):
for ss in substrings:
if string[index:index+len(ss)] != ss:
return False
# If we matched,move the index along by the length of the substring + 1,so we skip a character
index = index + len(ss) + 1
return True
def match(string,substring):
i = 0
substrings = substring.split('*')
while (i < (len(string) - len(substring))):
if check_all_substrings(i,substrings):
return True
i += 1
return False
# match substring
assert match("hitherehello","there")
#match substring with 1 wild card
assert match("hitherehello","the*e")
# match
assert match("hithat","h*t*a")
# wild cards should match exactly 1 character
assert not match("hitherrrrehello","the*e")
# do not match invalid substrings
assert not match("hithat","the*e")
assert not match("hithat","there")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。