Issue
def merge_the_tools(string, k):
strings_list = []
for i in range(0,len(string),k): # incrementing the loop by k
sub = (string[i:i+k])
substring='' # empty substrings
for x in sub:
if x not in substring: # removing the duplicates
substring = substring + x
strings_list.append(substring) # adding it into empty list
print(*strings_list, sep='\n') # printing the list
//This code takes a string breaks into substring based on variable 'k' , then check and remove the duplicates from substrings in an ordered way and print the new substrings.
input : merge_the_tools('AABCAAADA', 3) output :AB CA AD
Solution
If I understood your question correctly, you're asking whether for each iteration in your outer for-loop the substring-variable will be an empty sting before assigning variables to it. This is the case as you assign the empty string to the variable substring every time you start the loop (i.e after part of string is assigned to sub).
I wil try to go through yout function step by step using string = 'AABCAAADA', k = 3 (I copied the code as many times as necessary for the example and added comments to the code):
merge_the_tools('AABCAAADA', 3) #function call will trigger execution of function
strings_list = []
for i in range(0,len('AABCAAADA'), 3): #outer loop
# iteration 1: i = 0:
sub = (string[0:3]) # = 'AAB'
substring=''
for x in sub: #inner loop
# iteration 1.1: x = 'A'
if 'A' not in substring: #substring is empty
substring = '' + 'A' # substring = 'A' then go to next x in inner loop
# iteration 1.2: x = 'A'
if 'A' not in substring: #substring = 'A' -> condition not fullfilled, go to next x in inner loop
# iteration 1.3: x = 'B'
if 'B' not in substring: #substring = 'A'
substring = 'A' + 'B' # substring = 'AB' -> last x in inner loop -> continue outer loop
strings_list.append('AB') # strings_list = ['AB'], end of outer loop -> continue with next i of outer loop
# iteration 2: i = 3:
sub = (string[3:6]) # = 'CAA'
substring='' #here the empty string is reassigned to substring!
for x in sub: #inner loop
# iteration 2.1: x = 'C'
if 'C' not in substring: #substring is empty
substring = '' + 'C' # substring = 'C' then go to next x in inner loop
# iteration 2.2: x = 'A'
if 'A' not in substring: #substring = 'C'
substring = 'C' + 'A' # substring = 'CA' -> go to next x in inner loop
# iteration 2.3: x = 'A'
if 'A' not in substring: #substring = 'CA' -> condition not fullfilled, last x in inner loop -> continue outer loop
strings_list.append('CA') # strings_list = ['AB', 'CA'], end of outer loop -> continue with next i of outer loop
# iteration 3: i = 6:
sub = (string[6:9]) # = 'ADA'
substring='' #here the empty string is reassigned to substring!
for x in sub: #inner loop
# iteration 3.1: x = 'A'
if 'A' not in substring: #substring is empty
substring = '' + 'A' # substring = 'A' then go to next x in inner loop
# iteration 3.2: x = 'D'
if 'D' not in substring: #substring = 'A'
substring = 'A' + 'D' # substring = 'AD' -> go to next x in inner loop
# iteration 3.3: x = 'A'
if 'A' not in substring: #substring = 'AD' -> condition not fullfilled, last x in inner loop -> continue outer loop
strings_list.append('AD') # strings_list = ['AB', 'CA', 'AD'], end of outer loop -> last i of outer loop -> exit loop
print(*strings_list, sep='\n') #output :AB CA AD
Hope that makes things a bit more clear. Also if you are unfamiliar with the concept of loops, there are a lot of good tutorials out there (just google it).
Answered By - Hidden_NB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.