All programs should be one line long!
Long live the Magical Python One-Liner!!
basicOp=lambda o,v,w:v/w if o=='/'else{'+':v+w,'-':v-w,'*':v*w}[o]if o in'+-*'else"Invalid Operation" # Python one-liners are turing-complete, so why would you ever use more than that?
def basicOp(operation, value1, value2):if operation == "+":return value1 + value2if operation == "-":return value1 - value2if operation == "*":return value1 * value2if operation == "/":return value1 / value2return "Invalid Operation"- basicOp=lambda o,v,w:v/w if o=='/'else{'+':v+w,'-':v-w,'*':v*w}[o]if o in'+-*'else"Invalid Operation"
- # Python one-liners are turing-complete, so why would you ever use more than that?
Test.describe("Basic Tests") test.assert_equals(basicOp("-", 2, 1), 1) test.assert_equals(basicOp("+", 3, 3), 6) test.assert_equals(basicOp("/", 8, 2), 4) test.assert_equals(basicOp("*", 8, 0), 0) test.assert_equals(basicOp("*", 4, 5), 20) test.assert_equals(basicOp("@", 6, 3), "Invalid Operation") Test.describe("Complex Tests") test.assert_equals(basicOp("-", -12, 9), -21) test.assert_equals(basicOp("+", 23, 17), 40) test.assert_equals(basicOp("/", 111, 37), 3) test.assert_equals(basicOp("-", 230, 133), 97) test.assert_equals(basicOp("*", 42, 63), 2646)
- Test.describe("Basic Tests")
- test.assert_equals(basicOp("-", 2, 1), 1)
- test.assert_equals(basicOp("+", 3, 3), 6)
- test.assert_equals(basicOp("/", 8, 2), 4)
- test.assert_equals(basicOp("*", 8, 0), 0)
- test.assert_equals(basicOp("*", 4, 5), 20)
- test.assert_equals(basicOp("@", 6, 3), "Invalid Operation")
- Test.describe("Complex Tests")
- test.assert_equals(basicOp("-", -12, 9), -21)
- test.assert_equals(basicOp("+", 23, 17), 40)
- test.assert_equals(basicOp("/", 111, 37), 3)
- test.assert_equals(basicOp("-", 230, 133), 97)
- test.assert_equals(basicOp("*", 42, 63), 2646)
def factorise(a): # takes much less than O(a), unless a is prime prime_factors = {} p = 1 while a > 1: p += 1 while a % p == 0: if p in prime_factors: prime_factors[p] += 1 else: prime_factors[p] = 1 a = a//p return prime_factors def totient(a): """python 3.6.0""" primes = factorise(a) totient = 1 for i in primes: # formula for totient from prime factorisation totient *= i**(primes[i]-1) * (i-1) return totient
from math import gcd- def factorise(a): # takes much less than O(a), unless a is prime
- prime_factors = {}
- p = 1
- while a > 1:
- p += 1
- while a % p == 0:
- if p in prime_factors: prime_factors[p] += 1
- else: prime_factors[p] = 1
- a = a//p
- return prime_factors
- def totient(a):
- """python 3.6.0"""
return len([b for b in range(a) if (gcd(a, b) == 1)])- primes = factorise(a)
- totient = 1
- for i in primes: # formula for totient from prime factorisation
- totient *= i**(primes[i]-1) * (i-1)
- return totient
test.assert_equals(totient(1), 1) test.assert_equals(totient(2), 1) test.assert_equals(totient(3), 2) test.assert_equals(totient(4), 2) test.assert_equals(totient(5), 4) test.assert_equals(totient(6), 2) test.assert_equals(totient(7), 6) test.assert_equals(totient(8), 4) test.assert_equals(totient(9), 6) test.assert_equals(totient(128), 64) test.assert_equals(totient(127), 126) test.assert_equals(totient(1001), 720)
- test.assert_equals(totient(1), 1)
- test.assert_equals(totient(2), 1)
- test.assert_equals(totient(3), 2)
- test.assert_equals(totient(4), 2)
- test.assert_equals(totient(5), 4)
- test.assert_equals(totient(6), 2)
- test.assert_equals(totient(7), 6)
- test.assert_equals(totient(8), 4)
test.assert_equals(totient(9), 6)- test.assert_equals(totient(9), 6)
- test.assert_equals(totient(128), 64)
- test.assert_equals(totient(127), 126)
- test.assert_equals(totient(1001), 720)