Duplicates in Python #Pythonic

Problem Statement – Given a word you need to check for the occurrence of an alphabet in the word. If the count of the alphabet is greater than 1, it has to be replaced with “)”, else it has to be replaced with “(“.  Let us see the sample answer to the above question –

SAMPLE ANSWER 1

lower() – This function returns the string converted into lower characters. Often used to compare strings in case of ignoring the case.

count() – This function is used as such – string_name.count(alphabet): it returns the count of an alphabet in the string.

duplicates

SAMPLE ANSWER 2

join() method returns a string concatenated with the elements iterable. What join method actually does is it concatenates the elements being passed to it! Below is what is called a single line function. In here, it checks if the word i.e. the string converts it into its lower case and counts for an alphabet in it. Depending on the count, the correct value is returned!

screenshot from 2019-01-16 22:54:33

SAMPLE ANSWER 3

Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. In the below code, look at the outermost part of the code, you will find the for loop, which stores the alphabet of the word in the variable.

screenshot from 2019-01-16 23:23:35

The above are some of the ways in which you can find the duplicates and change them! In case, you have other options as well, let us know.

Happy Learning 🙂



Leave a comment