Question 1 Write a function count_clear (state) that takes a state of the cargo
ID: 3587937 • Letter: Q
Question
Question 1 Write a function count_clear (state) that takes a state of the cargo terminal, and returns an integer count of the number of containers in the given state that are currently clear (i.e., have nothing stacked on top of them) Notes and assumptions Note: The ground is not a container, so it doesn't count. You can assume there are no duplicates. . count-clear ( [ [ ' c1 ' , ' c3 ' ] , [ ' c2' , ' ground' ] , c3 ' , ' ground' ) >>> 2 >> count_clear (['c1', c3'], 'c3', "c2'], ['c2', ground'11) 1 >> count_clear (L'c4', ground'], ['c5', 'ground'], ['c6', 'ground']) 3Explanation / Answer
It is asking to find all containers which do not have anything stacked on it
For example
In first example
c3 is on top of c1 but there is noting on top of c2 and c3
In seocnd example
c3 is on top of c1 c2 is on top of c3 but there is nothing on c2
so 1
def count_clear(state):
containers = []
d = {}
for bottom, top in state:
containers.append(bottom)
if top != 'ground':
containers.append(top)
d[bottom] = top
containers = list(set(containers))
keys = d.keys()
result = len(containers) - len(keys)
return result
print(count_clear([['c1', 'c3'], ['c2', 'ground'], ['c3', 'ground']]))
print(count_clear([['c1', 'c3'], ['c2', 'c2'], ['c2', 'ground']]))
print(count_clear([['c', 'ground'], ['c5', 'ground'], ['c6', 'ground']]))
# code link: https://paste.ee/p/OkX01
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.