Question: [USE PYTHON 3] Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters (the sequence can either be a
[USE PYTHON 3] Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters (the sequence can either be a str or a list, if you write your code correctly, it shouldnt matter). This is a sequence of switches, uppercase means to turn a switch on, and lowercase to turn a switch off. For example, A means to turn the switch A on, and a means to turn the switch A off. (Turning an on switch on again, or an off switch off again, has no effect). The function should then return the set of those switches that are still on at the end of the sequence of switches. For example, the expression flipSwitches( ['A','A','a','B'] ) returns the set {'B'} because, the switch B is turned on during the sequence, whereas the switch A is turned on twice (so still on), then turned off once, so it is off at the end. To receive full credit, your code must use a set that keeps track of the switches that are on. You can then iterate over the sequence and modify the set of switches that are on as appropriate.
All of the following sample output must work in order to pass the doctest:
>>> flipSwitches( ['A','A','a','B'] ) {'B'} >>> flipSwitches( 'ABCba') {'C'} >>> flipSwitches( 'sdfjSDSDFasjjfdjldsSDF' )=={'S', 'D', 'F'} True >>> random.seed(5) >>> flipSwitches( [random.choice('abcdefABCDEF') for i in range(100)] )=={'D', 'B', 'F', 'C'} True >>> random.seed(100) >>> flipSwitches( [random.choice('abcdefABCDEF') for i in range(10000)] )=={'D', 'E', 'A', 'F'} True >>> random.seed(0) >>> flipSwitches( [random.choice('abcdefABCDEF') for i in range(10000)] )=={'E', 'A', 'C'} True
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
