Question

In: Computer Science

Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters...

  1. 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 shouldn’t 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. Sample output:


>>> flipSwitches( ['A','A','a','B'] )

{'B'}

>>> flipSwitches( 'ABCba')

{'C'}

>>> flipSwitches( 'sdfjSDSDFasjjfdjldsSDF' )=={'S', 'D', 'F'}

True

use python 3.7

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#required method
def flipSwitches(switches):
    #initializing an empty set to store switches that are on
   
on_switches=set()
    #looping through each element/character in switches
   
for i in switches:
        #checking if i is uppercase or lowercase
       
if i.isupper():
            #simply adding i to on_switches. note that since on_switches is a set,
            #we dont have to worry about duplicates as duplicates are not allowed
            #in sets
           
on_switches.add(i)
        elif i.islower():
            #converting i to upper case
           
i=i.upper()
            #if i is in on_switches, removing it, meaning turning switch i off
           
if i in on_switches:
                on_switches.remove(i)
    #at the end, returning on_switches set
   
return on_switches


Related Solutions

In a c programming Write a program that converts upper case letters to lower case letters...
In a c programming Write a program that converts upper case letters to lower case letters or vice versa: Enter a sentence: What a GREAT movie is! Converted sentence: wHAT_A_great_MOVIE_IS_ Convert all non-alphabetical letters to ‘_’
Write a function that accepts two arguments (say a and b) by value and one argument...
Write a function that accepts two arguments (say a and b) by value and one argument (say c) by reference. Then it calculates the multiplication of all the numbers between a and b (inclusive) and saves the results in c. The function does not return any value.
In python idle 3.9.0 write a function that: i) will count all lower case, upper case,...
In python idle 3.9.0 write a function that: i) will count all lower case, upper case, integers, and special symbols from a given string. The input string is provided by the user. ii) will check if a sting is a palindrome. User supplies the input string.
Write a value returning function named CountLower that counts the number of lower case letters on...
Write a value returning function named CountLower that counts the number of lower case letters on one line of standard input and returns that number. Document the dataflow of the function and show how it would be called from main().
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of...
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically: the first line of the file is a number that indicates the initial value of a Volume object. The remaining lines consist of a single character followed by a space followed by...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
7. Write a function that accepts a sentence as the argument and converts each word to...
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
Write a function in python that accepts a sentence as the argument and converts each word...
Write a function in python that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY. Please, I want the coding indented and then the...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT