development

Python Coding Tips: Set & List

The following code snippets are very helpful during sets and list operations. Intersection of sets must check for valid case $ cat sets.py s1 = {1,2,3} s2 = {} # empty sets means the intersection is empty too! s3 = {} if set(s1) & set(s2): print(s1.intersection(s2)) else: print(“no intersection”) Result $ python sets.py no intersection […]

Read more
development

Another Solution: Longest Palindrome Substring

class Solution(object): def __init__(self): self.lookup = {“test”} def isPalindrome(self, s, i, k): j = i + k – 1 if s[i] == s[j]: key = self.createKey(i + 1, j – 1) if key in self.lookup: #self.lookup.remove(key) return True return False def createKey(self, i, j): #return “{}:{}”.format(i,j) #return (i, j) #key = bin(i) + bin(j) #return […]

Read more