Answers for "Python One Liners"

0

Python One Liners

1 # Palindrome Python One-Liner
   2 phrase.find(phrase[::-1])
   3 
   4 # Swap Two Variables Python One-Liner
   5 a, b = b, a
   6 
   7 # Sum Over Every Other Value Python One-Liner
   8 sum(stock_prices[::2])
   9 
  10 # Read File Python One-Liner
  11 [line.strip() for line in open(filename)]
  12 
  13 # Factorial Python One-Liner
  14 reduce(lambda x, y: x * y, range(1, n+1))
  15 
  16 # Performance Profiling Python One-Liner
  17 python -m cProfile foo.py
  18 
  19 # Superset Python One-Liner
  20 lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])
  21 
  22 # Fibonacci Python One-Liner
  23 lambda x: x if x<=1 else fib(x-1) + fib(x-2)
  24 
  25 # Quicksort Python One-liner
  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 
  28 # Sieve of Eratosthenes Python One-liner
  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)))
Posted by: Guest on August-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language