I am doing a recursion assignment for class. I understand how to use the formula, but I believe I am doing something wrong here along the lines of printing the dictionary out correctly. Can someone please help me figure out how to get the right output?
[('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4218', 'Workshop Room', 3), ('4205', 'Tiled Room', 4), ('Out', 'Outside', 5)]
[('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4205', 'Tiled Room', 4), ('4213', 'STEM Center', 0), ('4218', 'Workshop Room', 3), ('Out', 'Outside', 5)]
[('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('Out', 'Outside', 5), ('4213', 'STEM Center', 0), ('4205', 'Tiled Room', 4), ('4218', 'Workshop Room', 3)]
[('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('Out', 'Outside', 5), ('4213', 'STEM Center', 0), ('4205', 'Tiled Room', 4), ('4218', 'Workshop Room', 3)]
Instead I am getting:
[('4213', 'STEM Center', 0), ('4218', 'Workshop Room', 3), ('4201', 'Foundations Lab', 1), ('4205', 'Tiled Room', 4), ('4204', 'CS Lab', 2), ('out', 'Outside', 5)]
None None
I have gotten this far with figuring this out.
def recursiveSort(sensor_list,n,t): #Recursive Sort
if n == 0:
return sensor_list
for i in range(n-1):
if sensor_list[i][t] > sensor_list[i + 1][t]:
temp = sensor_list[i]
sensor_list[i] =sensor_list[i + 1]
sensor_list[i + 1] = temp
return recursiveSort(sensor_list,n - 1,t)
Dict = {'4213' : ('STEM Center', 0),
'4201' : ('Foundations Lab', 1),
'4204' : ('CS Lab', 2),
'4218' : ('Workshop Room', 3),
'4205' : ('Tiled Room', 4),
'out' : ('Outside', 5),
}
sensor_list=[]
[ sensor_list.append((key,Dict[key][0],Dict[key][1])) for key in Dict ] #Adding values to a dictionary into a tuple using list comprehension
print sensor_list
print recursiveSort(sensor_list,len(sensor_list),0)
print recursiveSort(sensor_list,len(sensor_list),1)
print sensor_list
I ran the code with the proper indentation like below and got the right output.
def recursiveSort(sensor_list,n,t): #Recursive Sort
if n == 0:
return sensor_list
for i in range(n-1):
if sensor_list[i][t] > sensor_list[i + 1][t]:
temp = sensor_list[i]
sensor_list[i] =sensor_list[i + 1]
sensor_list[i + 1] = temp
return recursiveSort(sensor_list,n - 1,t)
Dict = {'4213' : ('STEM Center', 0),
'4201' : ('Foundations Lab', 1),
'4204' : ('CS Lab', 2),
'4218' : ('Workshop Room', 3),
'4205' : ('Tiled Room', 4),
'out' : ('Outside', 5),
}
sensor_list=[]
[sensor_list.append((key,Dict[key][0],Dict[key][1])) for key in Dict ] #Adding values to a dictionary into a tuple using list comprehension
print sensor_list
print recursiveSort(sensor_list,len(sensor_list),0)
print recursiveSort(sensor_list,len(sensor_list),1)
print sensor_list
According to the output you have got, I think the error is with the incorrect indentation of the for loop. Most probably it is looks like this.
if n == 0:
return sensor_list
for i in range(n-1):
if sensor_list[i][t] > sensor_list[i + 1][t]:
temp = sensor_list[i]
sensor_list[i] =sensor_list[i + 1]
sensor_list[i + 1] = temp
The for loop is not getting executed at all because,
And probably your return recursiveSort(sensor_list,n - 1,t)
statement is also out of indent.