├── coderbyte-AlphabetSoup.py ├── coderbyte-SimpleAdding.py ├── coderbyte-FirstFactorial.py ├── coderbyte-TimeConvert.py ├── coderbyte-CheckNums.py ├── coderbyte-LetterCapitalize.py ├── coderbyte-FirstReverse.py ├── coderbyte-LongestWord.py ├── coderbyte-SimpleSymbols.py ├── coderbyte-hard-PentagonalNumber.py ├── coderbyte-LetterChanges.py ├── coderbyte-QuestionsMarks.py ├── coderbyte-VowelSquare.py ├── coderbyte-ClosestEnemyII.py ├── coderbyte-CorrectPath.py ├── coderbyte-hard-ChessboardTraveling.py ├── coderbyte-hard-KaprekarsConstant.py ├── coderbyte-hard-MaximalSquare-python3.py ├── README.md ├── coderbyte-ScaleBalancing.py ├── coderbyte-medium-EightQueens.py ├── coderbyte-hard-MaximalSquare.py └── coderbyte-hard-MaximalSquare-python2.py /coderbyte-AlphabetSoup.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function AlphabetSoup(str) take the str string parameter being passed and return the string with the letters in alphabetical order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be included in the string. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def AlphabetSoup(str): 8 | str = "".join(sorted(str)) 9 | # code goes here 10 | return str 11 | 12 | # keep this function call here 13 | print AlphabetSoup(raw_input()) -------------------------------------------------------------------------------- /coderbyte-SimpleAdding.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function SimpleAdding(num) add up all the numbers from 1 to num. For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter num will be any number from 1 to 1000. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def SimpleAdding(num): 8 | sum = num 9 | for i in range(num): 10 | sum += i 11 | return sum 12 | 13 | # keep this function call here 14 | print SimpleAdding(raw_input()) -------------------------------------------------------------------------------- /coderbyte-FirstFactorial.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def FirstFactorial(num): 8 | if num == 1: 9 | return 1 10 | else: 11 | return num * FirstFactorial(num - 1) 12 | 13 | str = input() 14 | print(FirstFactorial(str)) -------------------------------------------------------------------------------- /coderbyte-TimeConvert.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function TimeConvert(num) take the num parameter being passed and return the number of hours and minutes the parameter converts to (ie. if num = 63 then the output should be 1:3). Separate the number of hours and minutes with a colon. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def TimeConvert(num): 8 | hour = int(num/60) 9 | min = num - hour * 60 10 | # code goes here 11 | return str(hour) + ':' + str(min) 12 | 13 | # keep this function call here 14 | print TimeConvert(raw_input()) -------------------------------------------------------------------------------- /coderbyte-CheckNums.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function CheckNums(num1,num2) take both parameters being passed and return the string true if num2 is greater than num1, otherwise return the string false. If the parameter values are equal to each other then return the string -1. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def CheckNums(num1,num2): 8 | if num2 > num1: 9 | return 'true' 10 | elif num2 < num1: 11 | return 'false' 12 | else: 13 | return '-1' 14 | 15 | # keep this function call here 16 | print CheckNums(raw_input()) -------------------------------------------------------------------------------- /coderbyte-LetterCapitalize.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each word. Words will be separated by only one space. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def LetterCapitalize(str): 8 | 9 | list = str.split(" ") 10 | newStr = "" 11 | for word in list: 12 | newStr += word[0].upper() 13 | for i in range(1, len(word)): 14 | newStr += word[i] 15 | newStr += " " 16 | newStr = newStr[:-1] 17 | return newStr 18 | 19 | # keep this function call here 20 | print LetterCapitalize(raw_input()) -------------------------------------------------------------------------------- /coderbyte-FirstReverse.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Challenge 3 | Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH. 4 | Sample Test Cases 5 | 6 | Input:"coderbyte" 7 | 8 | Output:etybredoc 9 | 10 | 11 | Input:"I Love Code" 12 | 13 | Output:edoC evoL I 14 | ''' 15 | 16 | def FirstReverse(str): 17 | lenStr = len(str) 18 | revert = "" 19 | for i in range(lenStr): 20 | revert = revert + str[lenStr-i-1] 21 | # code goes here 22 | str = revert 23 | return str 24 | 25 | # keep this function call here 26 | str = input() 27 | print(FirstReverse(str) -------------------------------------------------------------------------------- /coderbyte-LongestWord.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | import re 8 | 9 | def LongestWord(sen): 10 | list = sen.split(" ") 11 | longest = re.sub(r'[^a-zA-Z0-9]',"", list[0]) 12 | for word in list: 13 | word = re.sub(r'[^a-zA-Z0-9]',"", word) 14 | if len(word) > len(longest): 15 | longest = word 16 | return longest 17 | 18 | # keep this function call here 19 | str = input() 20 | print(LongestWord(str)) -------------------------------------------------------------------------------- /coderbyte-SimpleSymbols.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter. 3 | 4 | Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | ''' 6 | 7 | def SimpleSymbols(str): 8 | str = '=' + str + '=' 9 | for c in str: 10 | if c.isalpha(): 11 | if not str[str.index(c)-1] == '+' or not str[str.index(c)+1] == '+': 12 | return 'false' 13 | return 'true' 14 | 15 | # keep this function call here 16 | print SimpleSymbols(raw_input()) -------------------------------------------------------------------------------- /coderbyte-hard-PentagonalNumber.py: -------------------------------------------------------------------------------- 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-LetterChanges.py: -------------------------------------------------------------------------------- 1 | ### coderbyte 2 | 3 | ''' 4 | Challenge 5 | Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string. 6 | Sample Test Cases 7 | 8 | 9 | Input:"hello*3" 10 | 11 | Output:Ifmmp*3 12 | 13 | 14 | Input:"fun times!" 15 | 16 | Output:gvO Ujnft! 17 | 18 | ''' 19 | 20 | def LetterChanges(str): 21 | letterList = "abcdefghijklmnopqrstuvwxyz" 22 | vowelList = "aeiou" 23 | str = str.lower() 24 | newStr = "" 25 | 26 | # to the next letter 27 | for i in range(len(str)): 28 | if str[i].isalpha(): 29 | index = letterList.find(str[i]) 30 | letter = letterList[index+1] 31 | if vowelList.find(letter) != -1: 32 | letter = letter.upper() 33 | newStr = newStr + letter 34 | else: 35 | newStr = newStr + str[i] 36 | 37 | str = newStr 38 | return str 39 | 40 | # keep this function call here 41 | str = input() 42 | #letterList = "abcdefghijklmnopqrstuvwxyz" 43 | #print(letterList.find("a")) 44 | print(LetterChanges(str)) -------------------------------------------------------------------------------- /coderbyte-QuestionsMarks.py: -------------------------------------------------------------------------------- 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()) -------------------------------------------------------------------------------- /coderbyte-VowelSquare.py: -------------------------------------------------------------------------------- 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-ClosestEnemyII.py: -------------------------------------------------------------------------------- 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-CorrectPath.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Challenge (easy) 3 | 4 | Have the function CorrectPath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position. The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid. 5 | 6 | For example: if str is "r?d?drdd" then your program should output the final correct string that will allow a path to be formed from the top left of a 5x5 grid to the bottom right. For this input, your program should therefore return the string rrdrdrdd. There will only ever be one correct path and there will always be at least one question mark within the input string. 7 | 8 | Sample Test Cases: 9 | 10 | Input:"???rrurdr?" 11 | Output:dddrrurdrd 12 | 13 | 14 | Input:"drdr??rrddd?" 15 | Output:drdruurrdddd 16 | ''' 17 | 18 | def CorrectPath(s): #bruteforce ftw! 19 | import random 20 | while True: 21 | route=[] 22 | tracepos=[] 23 | x=1;y=5;answer=1 24 | for i in s: 25 | if i=="?":i=random.choice("lrud") 26 | if i=="u":y+=1 27 | elif i=="d":y-=1 28 | elif i=="r":x+=1 29 | elif i=="l":x-=1 30 | if (x,y) in tracepos: 31 | answer=0 32 | break 33 | else: tracepos.append((x,y)) 34 | route.append(i) 35 | if x==6 or x==0 or y==0 or y==6: 36 | answer=0 37 | break 38 | if x==5 and y==1 and answer==1: 39 | return "".join(route) 40 | 41 | print CorrectPath(raw_input()) -------------------------------------------------------------------------------- /coderbyte-hard-ChessboardTraveling.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Challenge (hard) 3 | 4 | Have the function ChessboardTraveling(str) read str which will be a string consisting of the location of a space on a standard 8x8 chess board with no pieces on the board along with another space on the chess board. The structure of str will be the following: "(x y)(a b)" where (x y) represents the position you are currently on with x and y ranging from 1 to 8 and (a b) represents some other space on the chess board with a and b also ranging from 1 to 8 where a > x and b > y. Your program should determine how many ways there are of traveling from (x y) on the board to (a b) moving only up and to the right. For example: if str is (1 1)(2 2) then your program should output 2 because there are only two possible ways to travel from space (1 1) on a chessboard to space (2 2) while making only moves up and to the right. 5 | 6 | Hard challenges are worth 15 points and you are not timed for them. 7 | Sample Test Cases 8 | 9 | Input:"(1 1)(3 3)" 10 | 11 | Output:6 12 | 13 | 14 | Input:"(2 2)(4 3)" 15 | 16 | Output:3 17 | 18 | ''' 19 | 20 | def ChessboardTraveling(str): 21 | x, y, a, b = (int(str[1]), int(str[3]), int(str[6]), int(str[8])) 22 | rightSteps = a - x 23 | upSteps = b - y 24 | totalSteps = rightSteps + upSteps 25 | totalSteps_permutation = permutation(totalSteps) 26 | upSteps_permutation = permutation(upSteps) 27 | rightSteps_permutation = permutation(rightSteps) 28 | posibleSteps = int(totalSteps_permutation / (upSteps_permutation * rightSteps_permutation)) 29 | return posibleSteps 30 | 31 | def permutation(num): 32 | result = 1 33 | for i in range(1, num+1): 34 | result *= i 35 | return result 36 | 37 | # keep this function call here 38 | print ChessboardTraveling(raw_input()) -------------------------------------------------------------------------------- /coderbyte-hard-KaprekarsConstant.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Challenge(hard) 3 | 4 | Have the function KaprekarsConstant(num) take the num parameter being passed which will be a 4-digit number with at least two distinct digits. Your program should perform the following routine on the number: Arrange the digits in descending order and in ascending order (adding zeroes to fit it to a 4-digit number), and subtract the smaller number from the bigger number. Then repeat the previous step. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174. 5 | 6 | Hard challenges are worth 15 points and you are not timed for them. 7 | Sample Test Cases 8 | 9 | Input:2111 10 | 11 | Output:5 12 | 13 | 14 | Input:9831 15 | 16 | Output:7 17 | 18 | ''' 19 | 20 | count = 0 21 | 22 | def KaprekarsConstant(num): 23 | global count 24 | if num == '6174': 25 | return count 26 | else: 27 | num1 = "".join(sorted(str(num))) #ascending order 28 | num2 = "".join(sorted(str(num), reverse=True)) #descending order 29 | num1 = int(num1) 30 | num2 = int(num2) 31 | subtractResult = abs(num1 - num2) 32 | diff = str(subtractResult) 33 | diffLength = len(diff) 34 | if diffLength < 4: 35 | for i in range(0, 4-diffLength): 36 | diff = '0' + diff 37 | count += 1 38 | return KaprekarsConstant(str(diff)) 39 | 40 | # keep this function call here 41 | print KaprekarsConstant(raw_input()) -------------------------------------------------------------------------------- /coderbyte-hard-MaximalSquare-python3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Mon Jun 10 16:54:05 2019 5 | 6 | @author: taylorliang 7 | """ 8 | def MaximalSquare(strArr): 9 | l = list([[int(x) for x in s] for s in strArr]) 10 | #print(l) 11 | 12 | row = len(strArr) 13 | col = len(strArr[0]) 14 | F = min(row, col) 15 | #print("row, col = ", row, ', ', col) 16 | #print("original F =", F) 17 | 18 | for f in range(F, 0, -1): # descending searching 19 | #print("======================================================\nfilter size = ", f) 20 | for i in range(0, row-f+1): # from row to row (filter goes from upper to bottom) 21 | # if f == row, do this for loop only once 22 | # i is the row index of the matrix 23 | for j in range(0, col-f+1): # from column to column (filter goes from left to right) 24 | # if f == col, do this loop only once 25 | # j is the column index of the matrix 26 | # strArr[i][j] is the upper left corner of the submatrix 27 | #print(f'Top left coordinate of the submatrix is: i, j = {i}, {j}') 28 | multiply = 1 29 | 30 | # l[m][n] is the elements of the submatrix 31 | for m in range(i, f+i): 32 | for n in range(j, f+j): 33 | #print(f'm, n = {m}, {n}') 34 | multiply *= l[m][n] 35 | #print(f'multiply = {multiply}') 36 | ## after all the multiply operator: 37 | if multiply == 1: 38 | return f*f 39 | return 0 40 | 41 | # copy and paste the test case here: 42 | strArr = ["0111", "1111", "1111", "1111"] 43 | print(MaximalSquare(strArr)) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Here are my solutions for coding challenges in [coderbyte](https://www.coderbyte.com). 2 | 3 | Hope you have fun with these coding challenges. 4 | 5 | Keep updating. 6 | 7 | ## Easy: 8 | - CheckNums 9 | - Longest Word 10 | - Time Convert 11 | - Alphabet Soup 12 | - First Reverse 13 | - Simple Adding 14 | - Letter Changes 15 | - Simple Symbols 16 | - First Factorial 17 | - Letter Capitalize 18 | - Correct Path 19 | - I hate bruteforce! 20 | - Scale Balancing 21 | - Vowel Square 22 | - Simple searching. 23 | - Closest Enemy II 24 | - The key to solve this challenge is to handle wrappingl. 25 | - Question Marks 26 | 27 | ## Medium: 28 | - Eight Queens 29 | - This challenge only asks you to determine if there is any attack, which is much simpler than to find solutions that no queen attacking each other as described in [wikiedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle#Solutions). 30 | 31 | ## Hard 32 | - Kaprekars Constant 33 | - Chessboard Traveling 34 | - It's all about permutation. 35 | - Maximal Square 36 | - 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. 37 | - Since coderbyte uses python2, and my own laptop uses python3, I have 2 versions of the code. 38 | - 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. 39 | - 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"]` 40 | - You can uncomment the `print()` to have a close monitor on how it works. Have fun! 41 | - Pentagonal Number 42 | - 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-ScaleBalancing.py: -------------------------------------------------------------------------------- 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-medium-EightQueens.py: -------------------------------------------------------------------------------- 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()) -------------------------------------------------------------------------------- /coderbyte-hard-MaximalSquare.py: -------------------------------------------------------------------------------- 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()) -------------------------------------------------------------------------------- /coderbyte-hard-MaximalSquare-python2.py: -------------------------------------------------------------------------------- 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()) --------------------------------------------------------------------------------