Skip to content

Commit 23a48c4

Browse files
committed
some exercises
1 parent a133e02 commit 23a48c4

5 files changed

+60
-0
lines changed

excel-sheet-solumn-number.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def titleToNumber(self, s):
3+
return reduce(lambda x, y: x* 26 + y, map(lambda x : ord(x) - 64, s))
4+
5+
solution = Solution()
6+
print solution.titleToNumber("A")
7+
print solution.titleToNumber("AB")
8+

factorial-trailing-zeros.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
class Solution:
4+
def trailingZeros(self, n):
5+
sum,pow5 = 0, 5
6+
while(n >= pow5):
7+
sum += n/pow5
8+
pow5 *= 5
9+
return sum
10+
11+
solution = Solution()
12+
print "trailing of is 25", solution.trailingZeros(25)
13+
print "trailing of is 100", solution.trailingZeros(100)

number-of-1-bits.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
class Solution:
3+
def hammingWeight(self, n):
4+
weight = 0
5+
nn = -n if n < 0 else n
6+
while(nn != 0):
7+
weight += nn % 2
8+
nn = nn / 2
9+
return weight
10+
a = Solution()
11+
print a.hammingWeight(7)
12+
print a.hammingWeight(-7)
13+
a.hammingWeight(7)

pascals-triangle.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sys
2+
class Solution:
3+
def generate(self, numRows):
4+
pascal = []
5+
for row in range(0, numRows):
6+
cur_list = [ 1 if i == 0 or i == row else pascal[row-1][i] + pascal[row-1][i-1] for i in range(0, row +1)]
7+
pascal.append(cur_list)
8+
return pascal
9+
10+
solution = Solution()
11+
numRows = int(sys.argv[1])
12+
print "numRows : ", numRows
13+
print solution.generate(numRows)

reverse-bits.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
class Solution:
4+
def reverseBits(self, n):
5+
i, l = 0, [0 for i in range(0, 32)]
6+
while n > 0:
7+
l[i] = n %2
8+
n = n / 2
9+
i += 1
10+
return reduce(lambda x, y: x * 2 + y, l)
11+
12+
solution = Solution()
13+
print solution.reverseBits(43261596)

0 commit comments

Comments
 (0)