Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # list generate
- [x for x in range(10)]
- # loop through string
- for c in "oklahoma":
- print(2*c)
- # conditional generate
- [2*c for c in "oklahoma" if c != "o"]
- # string range
- "oklahoma"[3:6]
- "oklahoma"[-2:]
- def squares():
- n = 0
- while(True):
- yield n*n
- n += 1
- def cubes():
- n = 0
- while (True):
- yield n*n*n
- n += 1
- for i in squares():
- if i > 100:
- break
- print(i)
- def test_nice(n):
- for i in squares():
- if i > n:
- break
- for j in cubes():
- if i+j > n:
- break
- if i+j == n:
- return i,j
- return 0,0
- def is_prime(n):
- if n < 2:
- return False
- if n == 2:
- return True
- else:
- i = 2
- while i*i <= n:
- if n % i == 0:
- return False
- i += 1
- return True
- def Primes():
- yield 2
- i = 3
- while (True):
- if is_prime(i):
- yield i
- i += 2
- l = [x for x in range(100) if is_prime(x)]
- def Collatz(n):
- while (True):
- yield n
- if n == 1:
- break
- elif n % 2 == 0:
- n = n // 2
- else:
- n = 3 * n + 1
- def applyf(f, a):
- return f(a)
- def root_f(f, n, low, upper):
- if low >= upper - 1:
- if f(upper) == n:
- return upper
- else:
- return low
- mid = (low + upper) // 2
- if f(mid) >= n:
- return root_f(f, n, low, mid)
- else:
- return root_f(f, n, mid, upper)
Add Comment
Please, Sign In to add comment