I’m learning Python and here is my first Snippet

This past week I started learning Python, and I find Python to be really, really cool. I’m using Python’s official website tutorials to learn, and I got to a point that I wanted to test out what I’ve learned so far. I went to my good codewars.com to find some fun exercises, and I found this one:

Problem
Write a function that will return the count of distinct case-insensitive alphabetic characters that occur ore than once in the given string. The given string can be assumed to contain only uppercase and lowercase alphabets.

Tests:

“abcde” -> 0 # no characters repeats more than once
“aabbcde” -> 2 # 'a' and 'b'
“aabbcdeB” -> 2 # 'a' and 'b'
“indivisibility” -> 1 # 'i'
“Indivisibilities” -> 2 # 'i' and 's'

Solution

#================================
# Count the number of duplicates
#================================
def duplicate_count(text):
    # convert string to Uppercase and then to a list
    text = list(text.upper());

    # create an empty list for the distinctValues
    distinctList = [];

    # char represents each letter of the incoming text
    for char in text:
        # create a temporary list without the char that we are using
        temp = text[:];
        indexToDelete = temp.index(char);
        del temp[indexToDelete];

        # check if the char is found in the temp, if yes, then added to the distinct-list.
        if(char in temp):
            # if the char is already in the disctinct-list, then DONT added it
            if(char not in distinctList):
                distinctList.append(char);

    # return the length of the distinct-list
    return len(distinctList);
Share Button

Leave a Comment

Your email address will not be published.