• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Feb 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # list generate
2. [x for x in range(10)]
3.
4. # loop through string
5. for c in "oklahoma":
6.     print(2*c)
7.
8. # conditional generate
9. [2*c for c in "oklahoma" if c != "o"]
10.
11. # string range
12. "oklahoma"[3:6]
13. "oklahoma"[-2:]
14.
15. def squares():
16.     n = 0
17.     while(True):
18.         yield n*n
19.         n += 1
20.
21. def cubes():
22.     n = 0
23.     while (True):
24.         yield n*n*n
25.         n += 1
26.
27. for i in squares():
28.     if i > 100:
29.         break
30.     print(i)
31.
32. def test_nice(n):
33.     for i in squares():
34.         if i > n:
35.             break
36.         for j in cubes():
37.             if i+j > n:
38.                 break
39.             if i+j == n:
40.                 return i,j
41.     return 0,0
42.
43. def is_prime(n):
44.     if n < 2:
45.         return False
46.     if n == 2:
47.         return True
48.     else:
49.         i = 2
50.         while i*i <= n:
51.             if n % i == 0:
52.                 return False
53.             i += 1
54.         return True
55.
56.
57. def Primes():
58.     yield 2
59.     i = 3
60.     while (True):
61.         if is_prime(i):
62.             yield i
63.         i += 2
64.
65. l = [x for x in range(100) if is_prime(x)]
66.
67.
68. def Collatz(n):
69.     while (True):
70.         yield n
71.         if n == 1:
72.             break
73.         elif n % 2 == 0:
74.             n = n // 2
75.         else:
76.             n = 3 * n + 1
77.
78. def applyf(f, a):
79.     return f(a)
80.
81. def root_f(f, n, low, upper):
82.     if low >= upper - 1:
83.         if f(upper) == n:
84.             return upper
85.         else:
86.             return low
87.     mid = (low + upper) // 2
88.
89.     if f(mid) >= n:
90.         return root_f(f, n, low, mid)
91.     else:
92.         return root_f(f, n, mid, upper)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top