Skip to content

Commit df97f8c

Browse files
committed
more challenges
1 parent 0eaa8e7 commit df97f8c

4 files changed

+114
-2
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ Keep updating.
1616
- First Factorial
1717
- Letter Capitalize
1818
- Correct Path
19-
- I hate using bruteforce!
19+
- I hate bruteforce!
20+
- Scale Balancing
21+
- Vowel Square
22+
23+
## Medium:
24+
- Eight Queens
25+
- This challenge only asks you to determine if there is any attack, which is much more simple than to find solutions that no queen attacking each other as described in [wiki](https://en.wikipedia.org/wiki/Eight_queens_puzzle#Solutions)
2026

2127
## Hard
2228
- Kaprekars Constant
2329
- Chessboard Traveling
30+
- It's all about permutation.
2431
- Maximal Square
2532
- 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.
26-
- Since coderbyte uses python2, and my own laptop uses python3, I have 2 version of the code.
33+
- Since coderbyte uses python2, and my own laptop uses python3, I have 2 versions of the code.
2734
- 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.
2835
- 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"]`
2936
- You can uncomment the `print()` to have a close monitor on how it works. Have fun!

coderbyte-ScaleBalancing.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Challenge
3+
4+
Have the function ScaleBalancing(strArr) read strArr which will contain two elements, the first being the two positive integer weights on a balance scale (left and right sides) and the second element being a list of available weights as positive integers. Your goal is to determine if you can balance the scale by using the least amount of weights from the list, but using at most only 2 weights. For example: if strArr is ["[5, 9]", "[1, 2, 6, 7]"] then this means there is a balance scale with a weight of 5 on the left side and 9 on the right side. It is in fact possible to balance this scale by adding a 6 to the left side from the list of weights and adding a 2 to the right side. Both scales will now equal 11 and they are perfectly balanced. Your program should return a comma separated string of the weights that were used from the list in ascending order, so for this example your program should return the string 2,6
5+
6+
There will only ever be one unique solution and the list of available weights will not be empty. It is also possible to add two weights to only one side of the scale to balance it. If it is not possible to balance the scale then your program should return the string not possible.
7+
8+
Sample Test Cases:
9+
10+
Input:["[3, 4]", "[1, 2, 7, 7]"]
11+
Output:"1"
12+
13+
14+
Input:["[13, 4]", "[1, 2, 3, 6, 14]"]
15+
Output:"3,6"
16+
'''
17+
18+
import itertools
19+
20+
def ScaleBalancing(strArr):
21+
x = list(int(s) for s in strArr[0][1:-1].split(','))
22+
y = list(int(s) for s in strArr[1][1:-1].split(','))
23+
# one
24+
for a in y:
25+
if x[0] + a == x[1] or x[1] + a == x[0]:
26+
return str(a)
27+
# two
28+
for pair in itertools.combinations(y, 2):
29+
if x[0] + pair[0] == x[1] + pair[1] or x[0] + pair[1] == x[1] + pair[0] or x[0] + pair[0] + pair[1] == x[1] or x[1] + pair[0] + pair[1] == x[0]:
30+
return ''.join(str(min(pair)) + ',' + str(max(pair)))
31+
32+
return "not possible"
33+
34+
# keep this function call here
35+
print ScaleBalancing(raw_input())

coderbyte-VowelSquare.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Challenge
3+
Have the function VowelSquare(strArr) take the strArr parameter being passed which will be a 2D matrix of some arbitrary size filled with letters from the alphabet, and determine if a 2x2 square composed entirely of vowels exists in the matrix. For example: strArr is ["abcd", "eikr", "oufj"] then this matrix looks like the following:
4+
5+
a b c d
6+
e i k r
7+
o u f j
8+
9+
Within this matrix there is a 2x2 square of vowels starting in the second row and first column, namely, ei, ou. If a 2x2 square of vowels is found your program should return the top-left position (row-column) of the square, so for this example your program should return 1-0. If no 2x2 square of vowels exists, then return the string not found. If there are multiple squares of vowels, return the one that is at the most top-left position in the whole matrix. The input matrix will at least be of size 2x2.
10+
Sample Test Cases
11+
Input:["aqrst", "ukaei", "ffooo"]
12+
13+
Output:"1-2"
14+
15+
16+
Input:["gg", "ff"]
17+
18+
Output:"not found"
19+
'''
20+
21+
def VowelSquare(strArr):
22+
l = list([[x for x in s] for s in strArr])
23+
row = len(strArr)
24+
col = len(strArr[0])
25+
for i in range(0,row-1):
26+
for j in range(0, col-1):
27+
if l[i][j] in 'aeiou' and l[i][j+1] in 'aeiou' and l[i+1][j] in 'aeiou' and l[i+1][j+1] in 'aeiou':
28+
return str(i) + '-' + str(j)
29+
return 'not found'
30+
31+
# keep this function call here
32+
print VowelSquare(raw_input())

coderbyte-medium-EightQueens.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Challenge
3+
Have the function EightQueens(strArr) read strArr which will be an array consisting of the locations of eight Queens on a standard 8x8 chess board with no other pieces on the board. The structure of strArr will be the following: ["(x,y)", "(x,y)", ...] where (x,y) represents the position of the current queen on the chessboard (x and y will both range from 1 to 8 where 1,1 is the bottom-left of the chessboard and 8,8 is the top-right). Your program should determine if all of the queens are placed in such a way where none of them are attacking each other. If this is true for the given input, return the string true otherwise return the first queen in the list that is attacking another piece in the same format it was provided.
4+
5+
For example: if strArr is ["(2,1)", "(4,2)", "(6,3)", "(8,4)", "(3,5)", "(1,6)", "(7,7)", "(5,8)"] then your program should return the string true. The corresponding chessboard of queens for this input is below (taken from Wikipedia).
6+
7+
![](https://i.imgur.com/zAT24ML.png)
8+
9+
Sample Test Cases:
10+
11+
Input:["(2,1)", "(4,3)", "(6,3)", "(8,4)", "(3,4)", "(1,6)", "(7,7)", "(5,8)"]
12+
13+
Output:"(2,1)"
14+
15+
16+
Input:["(2,1)", "(5,3)", "(6,3)", "(8,4)", "(3,4)", "(1,8)", "(7,7)", "(5,8)"]
17+
18+
Output:"(5,3)"
19+
'''
20+
21+
def EightQueens(strArr):
22+
x = list([[int(i) for i in p[1:-1].split(',')[0]] for p in strArr])
23+
y = list([[int(i) for i in p[1:-1].split(',')[1]] for p in strArr])
24+
#queenList = list([ [int(i) for i in p[1:-1].split(',')[0], int(j) for j in p[1:-1].split(',')[1]] for p in strArr ])
25+
queenList = []
26+
for i in range(0,len(x)):
27+
queenList.append((x[i][0],y[i][0]))
28+
29+
for i in range(0, len(x)-1):
30+
for j in range(i+1, len(x)):
31+
x1, y1 = queenList[i]
32+
x2, y2 = queenList[j]
33+
if x1 == x2 or y1 == y2 or (x1 == y2 and x2 == y1) or x1 - y1 == x2 - y2:
34+
return '(' + str(x1) + ',' + str(y1) + ')'
35+
return 'true'
36+
37+
# keep this function call here
38+
print EightQueens(raw_input())

0 commit comments

Comments
 (0)