For example in while A and B
, if A is False, it's not necessary to evaluate B. Then will B be evaluated in this case?
Similarly, in if A or B
, if A is True, it's not necessary to evaluate B.
Specific context is this problem, I wrote
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
output = []
i = 0
while ( i < len(nums) ):
head = nums[i]
while ( i <= len(nums)-2 ) and (nums[i+1] == nums[i] + 1): ### question here
i += 1
tail = nums[i]
if head == tail:
output.append(str(head))
else:
output.append(str(head) + '->' + str(tail))
i += 1
I don't know if it works yet (troubled by other bugs). In the line commented with 'question here', (nums[i+1] == nums[i] + 1)
will cause index exceeding string length if i==len(nums)-1
, so I added ( i <= len(nums)-2 )
trying to prevent that.
Any suggestions on how to fix/avoid/circumvent this is appreciated.