Matrix
class OperationError(Exception): def __init__(self, message): super().__init__(message) def matrix(matrix=list, operation=str, location=tuple, writedata=None): try: if operation == "read": return matrix[location[0]][location[1]] elif operation == "write": matrix[location[0]][location[1]] = writedata return matrix else: raise OperationError("That is not a valid operation for this system.") except Exception as e: print(e) return -1
def matrix(matrice=list, operation=str, location=tuple, writedata=None):if operation == "read":try:return matrice[location[0]][location[1]]except Exception as e:print(e)return 0elif operation == "write":try:matrice[location[0]][location[1]] = writedatareturn matriceexcept Exception as e:print(e)return 0else:raise OperationError("That is not a valid operation for this system.")- class OperationError(Exception):
- def __init__(self, message):
- super().__init__(message)
- def matrix(matrix=list, operation=str, location=tuple, writedata=None):
- try:
- if operation == "read":
- return matrix[location[0]][location[1]]
- elif operation == "write":
- matrix[location[0]][location[1]] = writedata
- return matrix
- else:
- raise OperationError("That is not a valid operation for this system.")
- except Exception as e:
- print(e)
- return -1
import codewars_test as test # TODO Write tests from solution import matrix # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "read", (0,1)), 2) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "write", (0,1), 7), [[1,7,3], [4,5,6], [7,8,9]]) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "read", (1,2)), 6) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "write", (1,2), 7), [[1,2,3], [4,5,7], [7,8,9]]) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "write", (0, 0), 7), [[7,2,3], [4,5,6], [7,8,9]]) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "write", (0, -1), 7), [[1,2,7], [4,5,6], [7,8,9]]) test.assert_equals(matrix([[1,2,3], [4,5,6], [7,8,9]], "write", (0) ), -1) try: matrix([[1,2,3], [4,5,6], [7,8,9]], "invalid operation", None, ) except OperationError as oe: test.assert_equals(oe.message, "That is not a valid operation for this system.")
- import codewars_test as test
- # TODO Write tests
- from solution import matrix # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "read", (0,1)), 2)
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "write", (0,1), 7), [[1,7,3],
- [4,5,6],
- [7,8,9]])
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "read", (1,2)), 6)
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "write", (1,2), 7), [[1,2,3],
- [4,5,7],
- [7,8,9]])
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "write", (0, 0), 7), [[7,2,3],
- [4,5,6],
- [7,8,9]])
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "write", (0, -1), 7), [[1,2,7],
- [4,5,6],
- [7,8,9]])
- test.assert_equals(matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "write", (0) ), -1)
- try:
- matrix([[1,2,3],
- [4,5,6],
- [7,8,9]], "invalid operation", None, )
- except OperationError as oe:
- test.assert_equals(oe.message, "That is not a valid operation for this system.")
function returnhundred() { return parseInt(('onehundred'.split('').map(x=>x.charCodeAt()).reduce((a,b)=>{ return (a > b) ? a - b : b - a}) / 4).toString(2)) }
- function returnhundred() {
return 'd'.charCodeAt();- return parseInt(('onehundred'.split('').map(x=>x.charCodeAt()).reduce((a,b)=>{ return (a > b) ? a - b : b - a}) / 4).toString(2))
- }
removed list creations and limitation to pow(2, 12)
def convert_decimalBinary(number): bin = "" while(number > 0): if number % 2 == 1: bin = "1" + bin else: bin = "0" + bin number = int(number/2) return int(bin)
def convert_decimalBinary(number: int) -> int:"""Convert number into binary without using bin()"""powers = [pow(2, i) for i in range(12)][::-1]binary = ['0' for i in range(len(powers))]for i, num in enumerate(powers):if num <= number:number = number - numbinary[i] = '1'return int(''.join(binary))- def convert_decimalBinary(number):
- bin = ""
- while(number > 0):
- if number % 2 == 1:
- bin = "1" + bin
- else:
- bin = "0" + bin
- number = int(number/2)
- return int(bin)