Advertisement
kosievdmerwe

Untitled

Nov 13th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. class CombinationIterator:
  2.     def __init__(self, characters: str, combinationLength: int):
  3.         self.cs = list(characters)
  4.         self.len = combinationLength
  5.         self.cur = [i for i in range(self.len)]
  6.  
  7.     def next(self) -> str:
  8.         result = "".join(self.cs[i] for i in self.cur)
  9.        
  10.         max_allowed_idx = len(self.cs) - 1
  11.         while self.cur:
  12.             self.cur[-1] += 1
  13.             if self.cur[-1] <= max_allowed_idx:
  14.                 break
  15.             self.cur.pop()
  16.             max_allowed_idx -= 1
  17.            
  18.         if not self.cur:
  19.             self.cur = None
  20.         else:
  21.             while len(self.cur) < self.len:
  22.                 self.cur.append(self.cur[-1] + 1)
  23.        
  24.         return result
  25.  
  26.     def hasNext(self) -> bool:
  27.         return self.cur is not None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement