Skip to main content

Posts

Showing posts from February, 2019

Find If A String Has All Unique Characters - Python

Find if a string has all unique characters in Python 3 Set Method def unique_or_not(s):   return len(set(s))==len(s) Iteration Method def unique_or_not2(s):   tl = []   for i in range(len(s)):     ts = s[i]     if ts in tl:       return False     tl.append(ts)   return True Test %timeit unique_or_not('abcdef') 679 ns per loop %timeit unique_or_not2('abcdef') 1.75 µs per loop

Reverse a Sentence and String Compression - Python

Reverse a Sentence and String Comprehension with same logic in Python 3 Reverse a Sentence Solution 1: Using Split & Reverse def reversed_sentence(a):   return " ".join(reversed(a.split())) Solution 2: Using Split and Index def reversed_sentence2(a):   return " ".join(a.split()[::-1]) Solution 3: Without using inbuilt functions | Double While Loop def reversed_sentence3(a):   words = []   length = len(a)   i = 0   while i<length:     if a[i] != " ":       word_start = i       while i<length and a[i] != " ":         i+=1       words.append(a[word_start:i])     i+=1   return " ".join(reversed(words)) Test %timeit reversed_sentence("Hello World! How are you?") 935 ns per loop %timeit reversed_sentence2("Hello World! How are you?") 710 ns per loop %timeit reversed_sentence3("Hello World! How are you?") 5.54 µs per loop String Co...

Largest Continuous Sum of Elements in a List - Python

Largest Continuous Sum of Elements in a List - Python def large_cont_sum(l):   if len(l)==0:     return 0   max_sum = current_sum = l[0]   for num in l[1:]:     current_sum = max(current_sum+num, num)     max_sum = max(current_sum, max_sum)   return max_sum Test large_cont_sum([1,2,-1,-10,3,4,10,-2,-1]) %timeit large_cont_sum([1,2,-1,-10,3,4,10,-2,-1]) Output 17 100000 loops, best of 3: 3.66 µs per loop

Compare Two Lists and Find Missing Element in Second List - Python

Compare Two Lists and Find Missing Element in Second List - Python Solutions Iterating smaller list and removing the element from other list #516 ns def finder(a, b):   for i in b:     a.remove(i)   return a Sorting, Zipping, Comparing each pair and returning first occurrence of mismatching pair #984 ns def finder2(a,b):   a.sort()   b.sort()   for num1, num2, in zip(a,b):     if num1!=num2:       return num1   return a[-1] Adding to dictionary and counting occurrence #5250 ns import collections def finder3(a,b):      d = collections.defaultdict(int)      for n in b:     d[n] += 1      for m in a:     if d[m] == 0:       return m     else:       d[m] -= 1 Subtracting sum of smaller list from larger list #538 ns def finder4(a,b):   return sum(a) - sum(b) Testing Time a = [1,2,...

Python Most Effective Anagram Program

Two strings are anagram if all letters in one string is also there in the other and vice versa There are three anagram logics here: Using Sorted def one(a, b):   if sorted(a)==sorted(b):     return True Using Dictionary def two(a,b):   if len(a) != len(b):     return False   else:     temp_dict = {}     for l in a:       if l in temp_dict:         temp_dict[l] += 1       else:         temp_dict[l] = 1     for l in a:       if l in temp_dict:         temp_dict[l] -= 1       else:         temp_dict[l] = 1     for l in temp_dict:       if temp_dict[l]!=0:         return False     return True Using List def three(a,b):   a1=list(a)   b1=list(b)   if len(a) != len(b):     ret...

Python | Did You Know? For Loop Takes 3x More Time than List Comprehension

Did you know this fact about Python? For Loop Takes 3x More Time than List Comprehension and Range is 2x more effective than List Comprehension Let me show you with an example: For loop + append def method1():   l=[]   for i in range(10000):     l.append(i)   return l For loop + addition def method2():   l=[]   for i in range(10000):     l+=[i]   return l List Comprehension + For loop def method3():   l=[i for i in range(10000)]   return l Range + List def method4():   l=list(range(10000))   return l Now let's look at which functions take how long: %timeit method1() #For loop + append 1000 loops, best of 3: 1300 µs per loop %timeit method2() #For loop + addition 1000 loops, best of 3: 1270 µs per loop %timeit method3() #List Comprehension 1000 loops, best of 3: 583 µs per loop %timeit method4() #Range + List 10000 loops, best of 3: 258 µs per loop

SparkContext in PySpark

SparkContext in PySpark Run this on Jupyter Notebook after installing Spark from pyspark import SparkContext sc = SparkContext() %%writefile example.txt first line second line third line fourth line textFile = sc.textFile('example.txt') textFile.count() textFile.first() secfind = textFile.filter(lambda line: 'second' in line) # RDD secfind PythonRDD[7] at RDD at PythonRDD.scala:43 # Perform action on transformation secfind.collect() ['second line'] # Perform action on transformation secfind.count() 1