Skip to content

Commit e9f4583

Browse files
committed
complete all the free challenges in coderbyte
1 parent df97f8c commit e9f4583

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Here are my solutions for coding challenges in [coderbyte](https://www.coderbyte.com)
1+
Here are my solutions for coding challenges in [coderbyte](https://www.coderbyte.com).
22

33
Hope you have fun with these coding challenges.
44

@@ -19,10 +19,14 @@ Keep updating.
1919
- I hate bruteforce!
2020
- Scale Balancing
2121
- Vowel Square
22+
- Simple searching.
23+
- Closest Enemy II
24+
- The key to solve this challenge is to handle wrappingl.
25+
- Question Marks
2226

2327
## Medium:
2428
- 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)
29+
- 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 [wikiedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle#Solutions).
2630

2731
## Hard
2832
- Kaprekars Constant

coderbyte-ClosestEnemyII.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Challenge
3+
Have the function ClosestEnemyII(strArr) read the matrix of numbers stored in strArr which will be a 2D matrix that contains only the integers 1, 0, or 2. Then from the position in the matrix where a 1 is, return the number of spaces either left, right, down, or up you must move to reach an enemy which is represented by a 2. You are able to wrap around one side of the matrix to the other as well. For example: if strArr is ["0000", "1000", "0002", "0002"] then this looks like the following:
4+
5+
0 0 0 0
6+
1 0 0 0
7+
0 0 0 2
8+
0 0 0 2
9+
10+
For this input your program should return 2 because the closest enemy (2) is 2 spaces away from the 1 by moving left to wrap to the other side and then moving down once. The array will contain any number of 0's and 2's, but only a single 1. It may not contain any 2's at all as well, where in that case your program should return a 0.
11+
12+
Sample Test Cases:
13+
14+
Input:["000", "100", "200"]
15+
Output:1
16+
17+
Input:["0000", "2010", "0000", "2002"]
18+
Output:2
19+
'''
20+
21+
def ClosestEnemyII(array):
22+
enemies = []
23+
for i, row in enumerate(array):
24+
for j, col in enumerate(list(row)):
25+
if col == '1': px, py = (i, j)
26+
if col == '2': enemies.append((i, j))
27+
moves = []
28+
for x, y in enemies:
29+
no_wrap = abs(px - x) + abs(py - y)
30+
col_wrap, row_wrap = abs(px - x) + abs(py - (y - len(array))), abs(px - (x - len(array))) + abs(py-y)
31+
moves.append(min(no_wrap, col_wrap, row_wrap))
32+
return min(moves) if moves else 0
33+
print ClosestEnemyII(raw_input())

coderbyte-QuestionsMarks.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Challenge
3+
4+
Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well.
5+
6+
For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.
7+
8+
Sample Test Cases
9+
10+
Input:"aa6?9"
11+
Output:false
12+
13+
Input:"acc?7??sss?3rr1??????5"
14+
Output:true
15+
'''
16+
17+
def QuestionsMarks(s):
18+
numbers = '1234567890'
19+
lastDigit = None
20+
answer = 'false'
21+
for i in range(0, len(s)):
22+
if s[i].isdigit():
23+
if lastDigit:
24+
if int(s[i]) + int(s[lastDigit]) == 10:
25+
if s[lastDigit:i].count('?') == 3:
26+
answer = 'true'
27+
else:
28+
return 'false'
29+
lastDigit = i
30+
31+
return answer
32+
33+
# keep this function call here
34+
print QuestionsMarks(raw_input())

0 commit comments

Comments
 (0)