My python solution was judged failed on StringIterator("X15D18V8") . But the output wasn't enough to help me fix out.


  • 0
    M
    class StringIterator:
        def __init__(self, compressedString):
            """
            :type compressedString: str
            """
            self.compressedString = compressedString
            self.lenth = len(self.compressedString)
            self.index = 0
            self.used_count = 0
            self.count = 0
            self.skip = 1
    
    
        def next(self):
            """
            make it too
            :rtype: str
            """
            if not self.hasNext():
                return " "
            old = self.compressedString[self.index]
        
            if old not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
                self.used_count += 1
                self.index += 1
                return old
            else:
                if not self.count:
                    self.tmpcount = int(self.compressedString[self.index])
                    while True:
                        if (self.index + self.skip) == self.lenth:
                            break
                        if self.compressedString[self.index + self.skip] not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
                            break
                        self.tmpcount = self.tmpcount * 10 + int(self.compressedString[self.index + self.skip])
                        self.skip += 1
    
                    self.count = self.tmpcount
    
                if self.count == self.used_count:
                    self.index += self.skip
                    self.used_count = 0
                    self.count = 0
                    self.skip = 1
                    return self.next()
                else:
                    self.used_count += 1
                    return self.compressedString[self.index-1]
    
        def hasNext(self):
            """
            :rtype: bool
            """
            if self.index == self.lenth:
                return False
            else:
                return True
    
    

    Any suggestions?


  • 0
    M

    @administrators

    I fix out myself after look my output again . In my code the last char need a case:

        def hasNext(self):
            """
            :rtype: bool
            """
            if self.index == self.lenth :
                return False
            elif (self.index+self.skip) == self.lenth and self.used_count == self.count:
                return False
            else:
                return True
    

    When there is no char any more. This func need to see the count was consumed totally.

    Anyway I suggest leetcode show the total output when the case was failed.

    In this case, a more simple case "e8" can also failed. But leetcode give "X15D18V8" and the output wasn't whole output. Why not using assert case from simple to hard?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.