mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 04:56:29 +00:00
Fix code indentation
This commit is contained in:
parent
1eaa3c0de9
commit
eb1c9f64af
@ -14,31 +14,31 @@ output: 2
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
def check(a, b):
|
def check(a, b):
|
||||||
sq = int((a + b) ** 0.5)
|
sq = int((a + b) ** 0.5)
|
||||||
return (sq * sq) == (a + b)
|
return (sq * sq) == (a + b)
|
||||||
|
|
||||||
if len(A) == 1: # corner case
|
if len(A) == 1: # corner case
|
||||||
return int(check(A[0], 0))
|
return int(check(A[0], 0))
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
def permute_distinct(S, i):
|
def permute_distinct(S, i):
|
||||||
global count
|
global count
|
||||||
if i == len(S):
|
if i == len(S):
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
for j in range(i, len(S)):
|
for j in range(i, len(S)):
|
||||||
if S[j] in S[i:j]: # prevent duplicates
|
if S[j] in S[i:j]: # prevent duplicates
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if i > 0 and (not check(S[j], S[i-1])): # invalid solution - branch and bound
|
if i > 0 and (not check(S[j], S[i-1])): # invalid solution - branch and bound
|
||||||
continue
|
continue
|
||||||
|
|
||||||
S[i], S[j] = S[j], S[i]
|
S[i], S[j] = S[j], S[i]
|
||||||
permute_distinct(S, i+1)
|
permute_distinct(S, i+1)
|
||||||
|
|
||||||
S[i], S[j] = S[j], S[i] # backtrack
|
S[i], S[j] = S[j], S[i] # backtrack
|
||||||
permute_distinct(A, 0)
|
permute_distinct(A, 0)
|
||||||
return count
|
return count
|
||||||
```
|
```
|
||||||
|
|
||||||
Gray Code
|
Gray Code
|
||||||
@ -67,11 +67,11 @@ Example G(2) to G(3):
|
|||||||
```
|
```
|
||||||
```python
|
```python
|
||||||
def gray(self, n):
|
def gray(self, n):
|
||||||
codes = [0, 1] # length 1
|
codes = [0, 1] # length 1
|
||||||
for i in range(1, n):
|
for i in range(1, n):
|
||||||
new_codes = [s | (1 << i) for s in reversed(codes)]
|
new_codes = [s | (1 << i) for s in reversed(codes)]
|
||||||
codes += new_codes
|
codes += new_codes
|
||||||
return codes
|
return codes
|
||||||
```
|
```
|
||||||
|
|
||||||
N Queens
|
N Queens
|
||||||
@ -92,20 +92,18 @@ B = ["cat", "cats", "and", "sand", "dog"]
|
|||||||
|
|
||||||
Output 1:
|
Output 1:
|
||||||
["cat sand dog", "cats and dog"]
|
["cat sand dog", "cats and dog"]
|
||||||
```
|
|
||||||
```python
|
```python
|
||||||
def wordBreak(A, B):
|
def wordBreak(A, B):
|
||||||
B = set(B)
|
B = set(B)
|
||||||
sents = []
|
sents = []
|
||||||
def foo(i, start, sent):
|
def foo(i, start, sent):
|
||||||
word = A[start:i+1]
|
word = A[start:i+1]
|
||||||
if i == len(A):
|
if i == len(A):
|
||||||
if word in B:
|
if word in B:
|
||||||
sents.append((sent + ' ' + word).strip())
|
sents.append((sent + ' ' + word).strip())
|
||||||
return
|
return
|
||||||
|
if word in B:
|
||||||
if word in B:
|
foo(i+1, i+1, sent + ' ' + word)
|
||||||
foo(i+1, i+1, sent + ' ' + word)
|
foo(i+1, start, sent)
|
||||||
foo(i+1, start, sent)
|
foo(0, 0, '')
|
||||||
foo(0, 0, '')
|
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user