Onr Liner Python
Onr Liner Python
1. Anagram
from collections import Counter
s1 = 'below'
s2 = 'elbow'
2. Binary to decimal
decimal = int('1010', 2)
print(decimal) #10
7. Quicksort
qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x
< l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])
This is not efficient and we can do the same using the below formula.
sum_n = n*(n+1)//2
20. if-else
print("even") if 4%2==0 else print("odd")
29. Timestamp
import time; print(time.time())
First, let us sort the list using the sorted() method. The sorted method
will return the sorted list.
sorted([5, 2, 9, 1])# [1, 2, 5, 9]
Next, let us sort this using the sort() method. The sort() method will
sort the original list and not return anything.
li = [5, 2, 9, 1]
li.sort()
print(li)
# 1, 2, 5, 9