Skip to content

Commit 9679995

Browse files
committed
zigzagiterator and word_squares
1 parent 2d4276c commit 9679995

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

builtins/dictionary.py

Whitespace-only changes.

builtins/list.py

Whitespace-only changes.

builtins/set.py

Whitespace-only changes.

builtins/tuple.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

queue/zigzagiterator.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class ZigZagIterator:
2+
def __init__(self, v1, v2):
3+
"""
4+
Initialize your data structure here.
5+
:type v1: List[int]
6+
:type v2: List[int]
7+
"""
8+
self.queue=[_ for _ in (v1,v2) if _]
9+
print(self.queue)
10+
11+
def next(self):
12+
"""
13+
:rtype: int
14+
"""
15+
v=self.queue.pop(0)
16+
ret=v.pop(0)
17+
if v: self.queue.append(v)
18+
return ret
19+
20+
def has_next(self):
21+
"""
22+
:rtype: bool
23+
"""
24+
if self.queue: return True
25+
return False
26+
27+
l1 = [1, 2]
28+
l2 = [3, 4, 5, 6]
29+
it = ZigZagIterator(l1, l2)
30+
while it.has_next():
31+
print(it.next())

string/word_squares.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import collections
2+
3+
def word_squares(words):
4+
n = len(words[0])
5+
fulls = collections.defaultdict(list)
6+
for word in words:
7+
for i in range(n):
8+
fulls[word[:i]].append(word)
9+
def build(square):
10+
if len(square) == n:
11+
squares.append(square)
12+
return
13+
for word in fulls[''.join(zip(*square)[len(square)])]:
14+
build(square + [word])
15+
squares = []
16+
for word in words:
17+
build([word])
18+
return squares
19+
20+
dic = ["at","baba","atan","atal"]
21+
print(word_squares(dic))

0 commit comments

Comments
 (0)