Skip to content

Commit 175cc50

Browse files
committed
add pentagonal number
1 parent d8e45f9 commit 175cc50

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ Keep updating.
1919
## Hard
2020
- Kaprekars Constant
2121
- Chessboard Traveling
22-
- MaximalSquare
22+
- Maximal Square
2323
- I think this one is very hard when it comes to matrix and filter, and how to handle filter movement within a matrix by pure code without using some matrix handling library.
2424
- Since coderbyte uses python2, and my own laptop uses python3, I have 2 version of the code.
2525
- For python2 version, you can just copy and paste the code on to the [coderbyte online editor](https://www.coderbyte.com/information/Maximal%20Square) and test the code.
2626
- For python3, you can run it on your own computer. You need to copy and paste the test case on strArr, for example: `strArr = ["0111", "1111", "1111", "1111"]`
27-
- Have fun!
27+
- You can uncomment the `print()` to have a close monitor on how it works. Have fun!
28+
- Pentagonal Number
29+
- Although it is a hard challenge, it is not hard at all. All you need to do is to find the regular pattern of how the pentagon grows.

coderbyte-PentagonalNumber.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Have the function PentagonalNumber(num) read num which will be a positive integer and determine how many dots exist in a pentagonal shape around a center dot on the Nth iteration. For example, in the image below you can see that on the first iteration there is only a single dot, on the second iteration there are 6 dots, on the third there are 16 dots, and on the fourth there are 31 dots.
3+
4+
![image](https://i.imgur.com/fYj3yvL.png)
5+
6+
Your program should return the number of dots that exist in the whole pentagon on the Nth iteration.
7+
8+
Hard challenges are worth 15 points and you are not timed for them. Use the Parameter Testing feature in the box below to test your code with different arguments.
9+
10+
Sample test case:
11+
12+
input: 2
13+
output: 6
14+
15+
input: 5
16+
output: 51
17+
'''
18+
19+
def PentagonalNumber(num):
20+
if num == 1:
21+
return 1
22+
else:
23+
return (num-1)*5 + PentagonalNumber(num-1)
24+
25+
26+
# keep this function call here
27+
print PentagonalNumber(raw_input())

coderbyte-hard-MaximalSquare.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Challenge (hard)
3+
4+
Have the function MaximalSquare(strArr) take the strArr parameter being passed which will be a 2D matrix of 0 and 1's, and determine the area of the largest square submatrix that contains all 1's. A square submatrix is one of equal width and height, and your program should return the area of the largest submatrix that contains only 1's. For example: if strArr is ["10100", "10111", "11111", "10010"] then this looks like the following matrix:
5+
6+
1 0 1 0 0
7+
1 0 1 1 1
8+
1 1 1 1 1
9+
1 0 0 1 0
10+
11+
For the input above, you can see the bolded 1's create the largest square submatrix of size 2x2, so your program should return the area which is 4. You can assume the input will not be empty.
12+
13+
Hard challenges are worth 15 points and you are not timed for them.
14+
15+
Sample Test Cases
16+
17+
Input:["0111", "1111", "1111", "1111"]
18+
19+
Output:9
20+
21+
Input:["0111", "1101", "0111"]
22+
23+
Output:1
24+
'''
25+
def MaximalSquare(strArr):
26+
l = list([[int(x) for x in s] for s in strArr])
27+
row = len(strArr)
28+
col = len(strArr[0])
29+
F = min(row, col)
30+
31+
for f in range(F, 0, -1): # descending searching
32+
for i in range(0, row-f+1): # from row to row (filter goes from upper to bottom)
33+
# if f == row, do this for loop only once
34+
# i is the row index of the matrix
35+
for j in range(0, col-f+1): # from column to column (filter goes from left to right)
36+
# if f == col, do this loop only once
37+
# j is the column index of the matrix
38+
# strArr[i][j] is the upper left corner of the submatrix
39+
multiply = 1
40+
41+
# l[m][n] is the elements of the submatrix
42+
for m in range(i, f+i):
43+
for n in range(j, f+j):
44+
multiply *= l[m][n]
45+
## after all the multiply operator:
46+
if multiply == 1:
47+
return f*f
48+
return 0
49+
50+
print MaximalSquare(raw_input())

0 commit comments

Comments
 (0)