darkmetal 0 Newbie Poster
def countStrs(StrToSearch, pattern):
	count = 0
	starting_point = 0
	for element in pattern:
		while element in StrToSearch [starting_point:] :
			count += 1
			starting_point = StrToSearch.find(element,starting_point) + 1
		print "the string","\'"+element+"\'", "occurs", count, "times."

		
def main():
	file = open("pidigits.txt", "rU")
	lineStr = ''
	numbers =['0','1','2','3','4','5','6','7','8','9']
	pattern =['1234','56789']
	number_processor = 0
	for line in file: #read file line by line
		line = line.strip()
		if '3.' in line:  #in the line checks for the 3. 
			line = line.replace('3.','') #check the line if their is 3. replaces it with new string.
		number_processor += len(line) #number_process = 0 addeds to the len of the line
		lineStr += str(line)
	file.close()  # closes the file
		#print number_processor
	print ''
	#~ print "Number of pi digits to process:", number_processor
	#~ print ''
	#~ findLongRepeats(lineStr)
	#~ print ''
	countStrs(lineStr,pattern)
	#~ print
	#~ increasing(lineStr)
main()

Can you help me understand exactly what is happening in the countStrs(lineStr,pattern) function. Explain the what the while loop is doing and how it calculates the count. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.