python one liners

Solutions on MaxInterview for python one liners by the best coders in the world

showing results for - "python one liners"
Maite
16 Aug 2017
1   1 # Palindrome Python One-Liner
2   2 phrase.find(phrase[::-1])
3   3 
4   4 # Swap Two Variables Python One-Liner
5   5 a, b = b, a
6   6 
7   7 # Sum Over Every Other Value Python One-Liner
8   8 sum(stock_prices[::2])
9   9 
10  10 # Read File Python One-Liner
11  11 [line.strip() for line in open(filename)]
12  12 
13  13 # Factorial Python One-Liner
14  14 reduce(lambda x, y: x * y, range(1, n+1))
15  15 
16  16 # Performance Profiling Python One-Liner
17  17 python -m cProfile foo.py
18  18 
19  19 # Superset Python One-Liner
20  20 lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])
21  21 
22  22 # Fibonacci Python One-Liner
23  23 lambda x: x if x<=1 else fib(x-1) + fib(x-2)
24  24 
25  25 # Quicksort Python One-liner
26  26 lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
27  27 
28  28 # Sieve of Eratosthenes Python One-liner
29  29 reduce( (lambda r,x: r-set(range(x**2,n,x)) if (x in r) else r), range(2,int(n**0.5)), set(range(2,n)))
30
similar questions
queries leading to this page
python one liners