Hello guys,i have been stuck for days while trying to find how to solve this python problem. here is how the code looks like:
set1=['78','18','79','56']
set2=['13','40','16','04']
set3=['63','70','78','60']
set4=['10','35','66','13']
set5=['32','41','71','70']
set6=['50','58','02','11']
set7=['13','40','41','05']
set8=['12','52','50','60']
set9=['71','13','66','12']
set10=['50','90','73','41']
set11=['09','18','44','54']
set12=['12','41','32','67']
Big_Set= [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10, set11, set12]
def occur(N):
Done=[]
dic={}
i = -1
for sets in Big_Set[:-1]:
i += 1
for item in sets:
if item not in Done and not dic.has_key(i):
#an exception catching block
#in case the set run out of data (IndexError)
j=i
try:
if item in Big_Set[j+N+1]:
#checks if any item reoccurs after jumping N sets
print("%s reoccurs after %d sets at:" %(item,N))
print("set%d: %s" %((j+1),str(Big_Set[j])))
dic[j]=0
try:
while item in Big_Set[j+N+1]:
#keeps checking if the item exists after N sets
sets=Big_Set[j+N+1]
print ("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
dic[j]=0
except IndexError: #in case we run out of data
pass
if item not in Done: Done.append(item)
except IndexError: #in case we run out of data
pass
return Done
def occur2(N):
Done=[]
dic={}
i = -1
for sets in Big_Set[:-1]:
i += 1
for item in sets:
if item not in Done and not dic.has_key(i):
#an exception catching block
#in case the set run out of data (IndexError)
j=i
try:
if int(item)-20 in Big_Set[j+N+1]:
#checks if the item minus 20 exist after N number of sets
print("%s minus 20 exists after %d sets at:" %(item,N))
print("set%d: %s" %((j+1),str(Big_Set[j])))
dic[j]=0
try:
while int(item)-20 in Big_Set[j+N+1]:
#keeps checking if the item exists after N sets
sets=Big_Set[j+N+1]
print ("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
dic[j]=0
except IndexError: #in case we run out of data
pass
if item not in Done: Done.append(item)
except IndexError: #in case we run out of data
pass
return Done
the first issue is that my first function doesn work the way i want it to. For example when i run it in IDLE thus:
occur(4)
here is what i get:
13 reoccurs after 4 sets at:
set2: ['13', '40', '16', '04']
set7: ['13', '40', '66', '05']
60 reoccurs after 4 sets at:
set3: ['63', '70', '78', '60']
set8: ['12', '50', '77', '60']
66 reoccurs after 4 sets at:
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
41 reoccurs after 4 sets at:
set5: ['32', '41', '71', '70']
set10: ['50', '90', '73', '41']
['13', '60', '66', '41']
the issue is that it does not return the result for an item twice, for example when i run the function occur(4), i expected it to return (set2, set7) and (set4, set9) for item 13 but i got only (set2, set7) as seen in the result above. i also expected it to return (set5, set10) and (set7, set12) for item 41 but i only got (set5, set10).
(2) the second issue is that i wanted to modify the program i.e occur2, so that it can look up for an item as well as (the item minus 20) simultaneously with the both items separated by N sets.for example if i run the second function like this..
occur2(4). here is what i got
set2: ['13', '40', '16', '04']
set7: ['13', '40', '66', '05']
60 reoccurs after 4 sets at:
set3: ['63', '70', '78', '60']
set8: ['12', '50', '77', '60']
66 reoccurs after 4 sets at:
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
41 reoccurs after 4 sets at:
set5: ['32', '41', '71', '70']
set10: ['50', '90', '73', '41']
['13', '60', '66', '41']
i get the same answer as before, but i was expecting the program to return set3 and set8. this is becuase we have item=70 in set3 and if you subtraact 20 from 70, you will get 50. if you count 4 sets (N=4) you have set8 which contains the item 50.
pls can someone put me through on how to do this or if there is a better way to do this.