Ejemplo n.º 1
0
        if c in charDict:
            # if index saved in dict less than value in minDuplicateIndex update it
            duplicateFound = True
            if minDuplicateIndex > charDict[c]:
                minDuplicateIndex = charDict[c]
        elif not duplicateFound:
            # otherwise if minDuplicateIndex not assigned add current char to dict
            charDict[c] = index
    if duplicateFound:
        return s[minDuplicateIndex]
    return "No duplicate"


def GetFirstDuplicate(s):
    # Create empty dict
    charDict = {}
    # loop on string
    for c in s:
        # check if char exist in dict return it
        if c in charDict:
            return c
        charDict[c] = True
        # otherwise add it
    return "No duplicate"


m = TestHelper.GenerateRandomString(20, CharType.Uppercase)

print(m, "  First duplicate  ", GetFirstDuplicate(m))
print(m, "  First index  ", GetFirstDuplicateIndex(m))
Ejemplo n.º 2
0
from CharListEnum import CharType
import TestHelper


def ReverseString(s):
    # Create empty string
    r = ''
    # loop on string in reverse order
    for i in range(len(s)-1, -1, -1):
        r += s[i]

    return r
    # add chars to empty string


m = TestHelper.GenerateRandomString(8, CharType.Digit)

print(m, "  ---  ", ReverseString(m))