Last active
June 21, 2018 07:06
-
-
Save a3linux/6a7ef9275a372b258301292ea5e5c01b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
one line fib | |
fib = lambda n: n if n <= 2 else fib(n-1) + fib(n-2) | |
super_fib = lambda n: 1 if n < 2 else 2 * super_fib(n-1) | |
swipe-step search Yang triangle | |
def stopwise(l, x, m, n): | |
row = 0 | |
col = n - 1 | |
while row < n - 1 and col >= 0: | |
if l[row][col] < x: | |
row += 1 | |
elif l[row][col] > x: | |
col -= 1 | |
else: | |
return True | |
return False | |
Find the longest palindromes | |
def palindromes(text): | |
text = text.lower() | |
results = [] | |
for i in range(len(text)): | |
for j in range(0, i): | |
chunk = text[j:i + 1] | |
if chunk == chunk[::-1]: | |
results.append(chunk) | |
return text.index(max(results, key=len)), results | |
import itertools | |
def longest_palindrome(s): | |
lp, lp_len = '', 0 | |
for start, stop in itertools.combinations(range(len(s)+1), 2): | |
ss = s[start:stop] # substring | |
if (len(ss) > lp_len) and (ss == ss[::-1]): | |
lp, lp_len = ss, len(ss) | |
return lp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment