i am a beginner in python and still got a whole lot to learn.i have this exercise that seem too difficult to crack.
check it out and see if you can help.

here is a simple data
set1=["01","34","29","16"]
set2=["45","88","19","17"]
set3=["56","21","57","20"]
set4=["29","42","89","08"]
set5=["19","45","56","20"]
set6=["85","56","33","78"]

using the data above, write this program

define lapp(x1,x2)
x1 and x2 are integers or numbers in the data above.the relationship between x1 and x2 is that x1 and x2 are in the same position in different sets, i.e if x1 is in set1,position 0 then x2 is in set2 postion 0.
if you type lets say lapp(x1=45,x2=56), the program returns set2,set3 as first result and set5,set6 as second result(depending on the number of times the data satisfies the function).
also if you input x1=29 and x2=19,the program returns set1,set2 as first result and then set4,set5 as second result.
if x1 and x2 do not satisfy the function , the the program returns not available.for example,if you enter x1=89 and x2=20,
it return s not available since 89 and 20 do not lap in the data.(though 89 is in set4,postion 2 and 20 is in set5 position 3,
the first condition is satisfied because x1=89 in set4 is followed by 20 in set5 but they are not in the same position,i.e 89 is in position 2 while 20 is in position 3.
pls help me out on this, if you dont understand my explanation, pls indicate so that i make it clearer in my next post.thanks

5
Contributors
11
Replies
121
Views
4 Years
Discussion Span
Last Post by bright.silva
Featured Replies
• 1

> not_lapped= True """used to test if there was no lapping""" I don't see how this can work. You should be writing: not_lapped= True # used to test if there was no lapping > """this was the best i could come out with but didnt solve the problem""" You should …

First of all, if you have to return "set3" or similar, then you have to have it available as a data.
My quick and dirty solution is below:

``````from collections import defaultdict
data={"set1":["01","34","29","16"],
"set2":["45","88","19","17"],
"set3":["56","21","57","20"],
"set4":["29","42","89","08"],
"set5":["19","45","56","20"],
"set6":["85","56","33","78"]}

positions=defaultdict(set) # which value, in which positions: which sets
allvalues=set() # which values are present in the data
for setname,values in data.iteritems():
for counter,value in enumerate(values):

def lapp(x1,x2):
if x1 not in allvalues or x2 not in allvalues:
return set()
returnset=set()
for k in positions:
value,counter=k
if value==x1 and (x2,counter) in positions:
returnset.update(positions[(x2,counter)])
if value==x2 and (x1,counter) in positions:
returnset.update(positions[(x1,counter)])
return returnset

print(lapp("88","21"))
print(lapp("01","88"))
print(lapp("77","88"))
``````

Edited by slate: salopp

I understand otherwise than why you have strings data given but your example result is talking numbers '12' is not same at all than 12.

You do must post any you own effort before expecting help, you might find builtin function zip useful.

ok, here is what i did but didnt work as expected.

``````set1=["23","45","88","34"]
set2=["65","43","56","78"]
set3=["01","87","12","77"]
set4=["89","23","45","65"]
set5=["15","81","43","76"]
set6=["65","23","16","51"]

Big_set=[set1, set2, set3, set4, set5, set6]

def lapping(x1,x2):

not_lapped= True """used to test if there was no lapping"""
for sets in Big_Set:
if x1 in sets:
y = sets.index(x1) """the position where x1 is """
try:
if x2 in Big_Set[i+1] and x2==Big_Set[i+1][y]:
print("%s and %s laps in sets: " %(x1,x2))
print("set%d: %s" %(i+1,str(sets)))
print("and")
print("set%d: %s" %(i+2,str(Big_Set[i+1])))
not_lapped= False

except IndexError:
pass
if not_lapped:
print("%s and %s do not match the lapping criteria\n" %(x1,x2))
i+=1
``````

"""this was the best i could come out with but didnt solve the problem"""
pls see if you could help me with your own idea.

Edited by Gribouillis: fixed code box

@slate, i tried your solution but didnt work like i explained.

not_lapped= True """used to test if there was no lapping"""

I don't see how this can work. You should be writing:

``````not_lapped= True # used to test if there was no lapping
``````

"""this was the best i could come out with but didnt solve the problem"""

You should tell us what kind of output you got. "It didn't work" is not too helpful.

o.k, here is the code and the output after running.

``````set1=['12','18','44','56']
set2=['13','40','16','04']
set3=['63','85','78','60']
set4=['17','35','12','20']
set5=['32','41','73','87']
set6=['12','23','02','61']

set7=['13','40','16','05']
set8=['63','85','78','60']
set9=['17','35','12','20']
set10=['32','41','73','87']
set11=['12','18','44','54']
Big_Set= [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10, set11]
# u can use Big_Set.append(setx) to add any more set u like

def lapping(x1,x2):

not_lapped= True #used to test if there was no lapping
for sets in Big_Set:
if x1 in sets:
y = sets.index(x1)#the position where x1 is
try:
if x2 in Big_Set[i+1] and x2==Big_Set[i+1][y]:
print("%s and %s laps in sets: " %(x1,x2))
print("set%d: %s" %(i+1,str(sets)))
print("and")
print("set%d: %s" %(i+2,str(Big_Set[i+1])))
not_lapped= False

except IndexError:
pass
if not_lapped:
print("%s and %s do not match the lapping criteria\n" %(x1,x2))
i+=1
``````

I ran the above program inputing x1=12 and x2=13.Here is the output.

``````12 and 13 laps in sets:
set1: ['12', '18', '44', '56']
and
set2: ['13', '40', '16', '04']
12 and 13 laps in sets:
set1: ['12', '23', '02', '61']
and
set2: ['13', '40', '16', '04']
12 and 13 laps in sets:
set1: ['12', '18', '44', '54']
and
set2: ['13', '40', '16', '04']
>>>
``````

the output above is not what i really wanted.if you look at the data,12 and 13 laps only in set1 and set2 as well as in set6 and set7.these were the only result i was expecting.pls help me look at the code and see what modification can be made to make it work properly.

Also, i discovered that if an item is not in set1,the program always reurns a negative result for example, i input x1=60 and x2=20, i expected it to return set3 and set4 as well as set8 and set9 according to the data but here is the output

60 and 20 do not match the lapping criteria.

i would also appreciate,if any guru in the house can help me with an alternative method of solving this problem.

Edited by pyTony: Do not misuse headers, use code formatting

The errors come from bad bookkeeping of the index i

``````def lapping(x1, x2):
x1, x2 = str(x1), str(x2) # in case integers are passed to lapping()
not_lapped= True #used to test if there was no lapping
i = -1
for sets in Big_Set[:-1]: # don't use the last set
i += 1
if x1 in sets:
y = sets.index(x1) # the first position where x1 is
if x2 == Big_Set[i+1][y]:
print("%s and %s laps in sets: " %(x1, x2))
print("set%d: %s" %(i+1,str(sets)))
print("and")
print("set%d: %s" %(i+2,str(Big_Set[i+1])))
not_lapped= False
if not_lapped:
print("%s and %s do not match the lapping criteria\n" %(x1,x2))
``````

@Gribouillis: thanks for the solution.worked as i wanted.

@Gribouillis and other gurus in the house, i just did this code but seem i ssem to be having difficulties in modifying it to work the way i want it.Its a bit lenghty, pls pardon me because am still a beginner in python.This program prints out where a particular property occurs depending on whether or not the number and position is specified.

``````set1=['34','45','67','56']
set2=['12','71','78','20']
set3=['76','73','36','32']
set4=['34','57','67','88']
set5=['78','45','82','28']
set6=['52','10','67','41']
set7=['58','78','77','80']
set8=['57','45','68','20']
set9=['67','36','29','53']
set10=['78','79','69','21']
set11=['64','90','35','33']
set12=['45','90','52','72']

Big_Set=[set1, set2, set3, set4, set5, set6, set7, set8, set9, set10, set11, set12]

def occur(N):
Done=[]
i=0
for sets in Big_Set:
for item in sets:
if item not in Done:
#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])))
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
except IndexError: #in case we run out of data
pass
Done.append(item)

except IndexError: #in case we run out of data
pass

i+=1
return Done

def occur2(N, item): #when the number is specified
Done=[]
i=0
for sets in Big_Set:
if item in sets:
if item not in Done:
#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 the number specified reoccurs after jumping N sets
print("%s reoccurs after %d sets at:" %(item,N))
print("set%d: %s" %((j+1),str(Big_Set[j])))
try:
while item in Big_Set[j+N+1]:
#keeps checking if the numbr specified exists after N sets
sets=Big_Set[j+N+1]
print("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
except IndexError: #in case we run out of data
pass
Done.append(item)

except IndexError: #in case we run out of data
pass

i+=1
return Done, item #returns a tuple (Done,item)

def occur3(N, item, pos): #when both the number and position is specified
Done=[]
i=0
j=i
for sets in Big_Set:
if item in sets and (item == sets[pos]):
if item not in Done:
#an exception catching block
#in case the set run out of data (IndexError)
j=i

try:
if item in Big_Set[j+N+1] and item == Big_Set[j+N+1][pos]:
#checks if the number specified exists after N sets and at the specified position
print("%s reoccurs after %d sets at position %d in:"%(item,N,pos))
print("set%d: %s" %((j+1),str(Big_Set[j])))

try:
while item in Big_Set[j+N+1]and item == Big_Set[j+N+1][pos]:
#keeps checking if the item exists after N sets at the specified position
sets=Big_Set[j+N+1]
print("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
except IndexError: #in case we run out of data
pass
Done.append(item)

except IndexError: #in case we run out of data
pass

i+=1
return Done, item, pos #returns a tuple (Done,item,pos)

def run_occurs(N,i,p):#this program runs the functions above
N=N
item=i
position=p
if not item and type(position)!=type(3):#when number and position is unspecified
print("N is: %d" %(N))
y=occur(N)
if not y:
print("No item matches the search criteria")

elif type(position)!=type(3) and item:#when only position is unspecified
print("N is: %d" %(N))
print("item is %s: " %(item))
y=occur2(N,item)
if not y[0]:
print("%s does not match that search criteria" %(y[1]))

elif type(position)==type(3) and item:
#when number and position are specified
print("N is: %d" %(N))
print("item is: %s" %(item))
print("Position is %d: " %(position))
y=occur3(N,item,position)
if not y[0]:
print ("%s does not match the search criteria at position %d" %(y[1],y[2]))

if __name__== '__main__':
item=None
position= None
N=None

while not N: #this loops until a number is specified
print("Please insert the number of sets to jump: ")
try:
N= input()
except:
print("Invalid input. Input must be a number\n")

print("Do u want to specify the number to search for? (Y/N)")
print("Please type Y for yes or N for no")
search_number= raw_input().upper()
while search_number != 'Y' and search_number != 'N':
print("Invalid input")
print("Please type Y for yes or N for no")
search_number= raw_input().upper()
if search_number == 'Y':
while not item:
item= raw_input("type in the number: ")
try:
int(item)
except:
print("Invalid input. Input must be a number\n")
item= None

print("Do u want to specify the the position to search for? (Y/N)")
print("Please type Y for yes or N for no")
search_position= raw_input().upper()
while search_position != 'Y' and search_position != 'N':
print("Invalid input")
print("Please type Y for yes or N for no")
search_position= raw_input().upper()
if search_position == 'Y':
while type(position)!= type(3):
try:
position= int(raw_input("insert position here: "))
except:
print("Invalid input. Input must be a number\n")
else:
print("You have chosen not to specify any position\n")

else:
print("You have chosen not to specify a number\n")

run_occurs(N,item,position)
``````

Ok, i ran the first function called occur(N).N is the number of sets an item must skip before re-occurring.so, i input N=2. here are the results:
N is: 2
34 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']
67 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']
78 reoccurs after 2 sets at:
set2: ['12', '71', '78', '20']
set5: ['78', '63', '82', '28']

the only problem i have with this function is that it only returns one result per integer.In addition to the results above i expected the function to return set7 and set10 for integer 78 and set9 and set12 for integer 34.can anyone help me with this modification.

i ran the second function called occur2(N, item).i input N=2 and item=34.here is the result
34 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']

in addition to this result, i expected the function to return set9 and set12 according to the data above.i think its same modification that function 1 requires that this also requires.

i ran the third fuction called occur3(N, item, position).i used N=3, item=45 and position=1 and here is the reslut:
N is: 3
item is: 45
Position is 1:
45 reoccurs after 3 sets at position 1 in:
set1: ['34', '45', '67', '56']
set5: ['78', '45', '82', '28']

in addtion to this result, i expected the program to return set8 and set12 for item 45.after looking at the code again, i discovered that the function only works for an item that moves from a particular position after N sets to the same postion i.e for the example above, 45 is in postion 1 in both set1 and set5, but 45 in set8 is also in position 1 but not in postion 1 in set12(hence is not returned as a valid output).so, i need help in modifying the code so that the position characteristics only refers to the former position an item occupies in the former set and not the position it occupies in the latter set.

i.e from the example above (using 45 as item), position will refer to postion 1 in set1 only and not position 1 in set5.if this is done, then set8 and set12 will be included in the result if i input N=3, item=45 and position=1..pls gurus in the house, i need your help on these problems.

I really think we should go back to basics. I do not understand the expectations.

Please provide tests. With a given dataset, what is the expected output for given inputs.

In my post I provided 3 test cases.
lapp("88","21") should return (set2,set3) because they are at index 1 in these sets.
The other two should return empty sets.

I really think we should go back to basics. I do not understand the expectations.Please provide tests. With a given dataset, what is the expected output for given inputs.In my post I provided 3 test cases.
lapp("88","21") should return (set2,set3) because they are at index 1 in these sets.The other two should return empty sets.
The other two should return empty sets.