Skip to content

Commit e06bcc0

Browse files
committed
python-100 answer
1 parent dfeb796 commit e06bcc0

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

Diff for: 070-primes.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import math
2+
3+
4+
class PrimeGenerator(object):
5+
6+
def generate_primes(self, max_num):
7+
if max_num is None:
8+
raise TypeError('max_num cannot be None')
9+
array = [True] * max_num
10+
array[0] = False
11+
array[1] = False
12+
prime = 2
13+
while prime <= math.sqrt(max_num):
14+
self._cross_off(array, prime)
15+
prime = self._next_prime(array, prime)
16+
return array
17+
18+
def _cross_off(self, array, prime):
19+
for index in range(prime*prime, len(array), prime):
20+
# Start with prime*prime because if we have a k*prime
21+
# where k < prime, this value would have already been
22+
# previously crossed off
23+
array[index] = False
24+
25+
def _next_prime(self, array, prime):
26+
next = prime + 1
27+
while next < len(array) and not array[next]:
28+
next += 1
29+
return next

Diff for: 071-digits.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
3+
def add_digits(self, val):
4+
if val is None:
5+
raise TypeError('num cannot be None')
6+
if val < 0:
7+
raise ValueError('num cannot be negative')
8+
digits = []
9+
while val != 0:
10+
digits.append(val % 10)
11+
val //= 10
12+
digits_sum = sum(digits)
13+
if digits_sum >= 10:
14+
return self.add_digits(digits_sum)
15+
else:
16+
return digits_sum

Diff for: 072-math_ops.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
3+
def __init__(self, upper_limit=100):
4+
self.max = None
5+
self.min = None
6+
# Mean
7+
self.num_items = 0
8+
self.running_sum = 0
9+
self.mean = None
10+
# Mode
11+
self.array = [0] * (upper_limit + 1)
12+
self.mode_occurrences = 0
13+
self.mode = None
14+
15+
def insert(self, val):
16+
if val is None:
17+
raise TypeError('val cannot be None')
18+
if self.max is None or val > self.max:
19+
self.max = val
20+
if self.min is None or val < self.min:
21+
self.min = val
22+
# Calculate the mean
23+
self.num_items += 1
24+
self.running_sum += val
25+
self.mean = self.running_sum / self.num_items
26+
# Calculate the mode
27+
self.array[val] += 1
28+
if self.array[val] > self.mode_occurrences:
29+
self.mode_occurrences = self.array[val]
30+
self.mode = val

Diff for: 073-power_of_two.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
3+
def is_power_of_two(self, val):
4+
if val is None:
5+
raise TypeError('n cannot be None')
6+
if val <= 0:
7+
return False
8+
return (val & (val - 1)) == 0

Diff for: 074-sum.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
3+
def sum_two(self, a, b):
4+
if a is None or b is None:
5+
raise TypeError('a or b cannot be None')
6+
result = a ^ b;
7+
carry = (a&b) << 1
8+
if carry != 0:
9+
return self.sum_two(result, carry)
10+
return result

Diff for: 075-sub.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
3+
def sub_two(self, a, b):
4+
if a is None or b is None:
5+
raise TypeError('a or b cannot be None')
6+
result = a ^ b
7+
borrow = (~a & b) << 1
8+
if borrow != 0:
9+
return self.sub_two(result, borrow)
10+
return result

0 commit comments

Comments
 (0)