Write a function "matrix()" that takes a list, an operation, and a location as tuple as an input, and returns:
if Write: The new matrix
if Read: The data at the requested point
for a Write operation: Change the selected location to the writedata argument, then return the new matrix.
for a Read operation: Return the selected location's value.
Return OperationError if neither.
A matrix is just a 2d list, for the purposes of this kumite.
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 0
elif operation == "write":
try:
matrice[location[0]][location[1]] = writedata
return matrice
except Exception as e:
print(e)
return 0
else:
raise OperationError("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]])
Write a function that returns the fibonacci sequence starting at 1, and ending at the first number greater than n inclusive, with the sum of the sequence as a tuple. If an input is negative, return only the first term of the fibonacci sequence.
def fibthing(make=int): fibseq = [] while max(fibseq, default=0) < make: if len(fibseq) > 1: fibseq.append(fibseq[-1] + fibseq[-2]) else: fibseq.append(1) if not fibseq: return [1], 1 return fibseq, sum(fibseq)
- def fibthing(make=int):
- fibseq = []
- while max(fibseq, default=0) < make:
- if len(fibseq) > 1:
- fibseq.append(fibseq[-1] + fibseq[-2])
- else:
- fibseq.append(1)
- if not fibseq:
- return [1], 1
- return fibseq, sum(fibseq)
import codewars_test as test from solution import fibthing import random def myfib(n): seq = [] while max(seq, default=0) < n: if len(seq) > 1: seq.append(seq[-1] + seq[-2]) else: seq.append(1) return seq, sum(seq) @test.describe("Base") def test_group(): @test.it("Base Cases") def test_case(): test.assert_equals(fibthing(199218), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811], 832039)) test.assert_equals(fibthing(199), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233], 609)) test.assert_equals(fibthing(55), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55], 143)) @test.it("Edge Cases") def edge_case(): test.assert_equals(fibthing(1), ([1], 1)) test.assert_equals(fibthing(2), ([1, 1, 2], 4)) test.assert_equals(fibthing(0), ([1], 1)) test.assert_equals(fibthing(-1), ([1], 1)) @test.describe("Random") def random_group(): for _ in range(1000): num = random.randrange(-1000000, 1000000) @test.it(str(num)) def test_random(): test.assert_equals(fibthing(num), myfib(num) if num > 0 else ([1],1))
- import codewars_test as test
- from solution import fibthing
- import random
- def myfib(n):
- seq = []
- while max(seq, default=0) < n:
- if len(seq) > 1: seq.append(seq[-1] + seq[-2])
- else: seq.append(1)
- return seq, sum(seq)
- @test.describe("Base")
- def test_group():
- @test.it("Base Cases")
- def test_case():
- test.assert_equals(fibthing(199218), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811], 832039))
- test.assert_equals(fibthing(199), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233], 609))
- test.assert_equals(fibthing(55), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55], 143))
- @test.it("Edge Cases")
- def edge_case():
- test.assert_equals(fibthing(1), ([1], 1))
- test.assert_equals(fibthing(2), ([1, 1, 2], 4))
- test.assert_equals(fibthing(0), ([1], 1))
- test.assert_equals(fibthing(-1), ([1], 1))
- @test.describe("Random")
- def random_group():
for _ in range(100):num = random.randrange(0, 1000000)- for _ in range(1000):
- num = random.randrange(-1000000, 1000000)
- @test.it(str(num))
- def test_random():
test.assert_equals(fibthing(num), myfib(num))- test.assert_equals(fibthing(num), myfib(num) if num > 0 else ([1],1))
Create a script that creates the fibonacci sequence up to a certain number, but afterwards adds all the numbers and returns the sequence and the sum.
Prioritize functionality without sacrificing readability.
def fibthing(make=int):
fibseq = []
n1 = 0
n2 = 1
cur = 0
sum = 0
while cur < make:
fibseq.append(n2)
cur = n2
n2 = n1 + cur
n1 = cur
for i in fibseq:
sum += i
return fibseq, sum
import codewars_test as test
# TODO Write tests
from solution import fibthing# or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Base")
def test_group():
@test.it("Base Cases")
def test_case():
test.assert_equals(fibthing(199218), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811], 832039))
test.assert_equals(fibthing(199), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233], 609))
test.assert_equals(fibthing(55), ([1, 1, 2, 3, 5, 8, 13, 21, 34, 55], 143))
Added Test Cases
Improved readability
lambda
def Calculator(*args): operators = { '+': lambda a, b: a + b, '-': lambda a, b: a - b, '*': lambda a, b: a * b, '/': lambda a, b: a / b if b != 0 else 0 } if len(args) != 3 or str(args[0]) not in operators: return 0 operator, operand1, operand2 = args return operators[operator](operand1, operand2)
- def Calculator(*args):
- operators = {
- '+': lambda a, b: a + b,
- '-': lambda a, b: a - b,
- '*': lambda a, b: a * b,
- '/': lambda a, b: a / b if b != 0 else 0
- }
if not (str(args[0]) in "+-*/" and len(args) == 3):- if len(args) != 3 or str(args[0]) not in operators:
- return 0
sign, n1, n2 = argsif sign == "/" and n2 == 0:return 0return n1 + n2 if sign == "+" else n1 - n2 if sign == "-" else n1 * n2 if sign == "*" else n1 / n2 if sign == "/" else 0- operator, operand1, operand2 = args
- return operators[operator](operand1, operand2)
import codewars_test as test import random @test.describe("Sample tests") def sample_tests(): test.assert_equals(Calculator("/", 1, 0), 0) test.assert_equals(Calculator("+", 42, 11), 53) test.assert_equals(Calculator("-", 6, 50), -44) test.assert_equals(Calculator("/", 32, 2), 16) test.assert_equals(Calculator("*", 25, 5), 125) test.assert_equals(Calculator(123, 2), 0) operators = ['+', '-', '*', '/'] @test.describe("Random tests") def random_tests(): for _ in range(2000): operator = random.choice(operators) num1 = random.uniform(0, 1000) num2 = random.uniform(1, 1000) description = f"Operator: {operator}, Num1: {num1}, Num2: {num2}" expected = float(eval(f"{num1}{operator}{num2}")) test.assert_equals(Calculator(operator, num1, num2), expected, description)
test.assert_equals(Calculator("/", 1, 0), 0)test.assert_equals(Calculator("+", 42, 11), 53)test.assert_equals(Calculator("-", 6, 50), -44)test.assert_equals(Calculator("/", 32, 2), 16)test.assert_equals(Calculator("*", 25, 5), 125)test.assert_equals(Calculator(123, 2), 0)- import codewars_test as test
- import random
- @test.describe("Sample tests")
- def sample_tests():
- test.assert_equals(Calculator("/", 1, 0), 0)
- test.assert_equals(Calculator("+", 42, 11), 53)
- test.assert_equals(Calculator("-", 6, 50), -44)
- test.assert_equals(Calculator("/", 32, 2), 16)
- test.assert_equals(Calculator("*", 25, 5), 125)
- test.assert_equals(Calculator(123, 2), 0)
- operators = ['+', '-', '*', '/']
- @test.describe("Random tests")
- def random_tests():
- for _ in range(2000):
- operator = random.choice(operators)
- num1 = random.uniform(0, 1000)
- num2 = random.uniform(1, 1000)
- description = f"Operator: {operator}, Num1: {num1}, Num2: {num2}"
- expected = float(eval(f"{num1}{operator}{num2}"))
- test.assert_equals(Calculator(operator, num1, num2), expected, description)
made more test cases so that the idiots who write one-line regex scripts need to write more to deal with Bool, Int, and NoneType :3
import random import codewars_test as test from solution import Connection, where_were_you_when_codewars_died import string locations = ["House", "Bar", "88.131.626.98:25565", random.randint(1,10), "Haus", "There", "Their", "They're", None] # test.assert_equals(actual, expected, [optional] message) @test.describe("Basic Cases") def test_group(): @test.it("test case") def test_case(): test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.') @test.it("random cases") def randomcases(): for i in range(100): findtester = random.randint(1, 1000000) location = random.choice(locations) test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location=location, food_source=findtester), f'I was at {location} consuming {findtester} when codewars died.') @test.describe("Additional Test Cases") def test_group(): @test.it("empty strings") def test_empty_strings(): test.assert_equals(where_were_you_when_codewars_died(activity='', location='', food_source=''), 'I was at consuming when died.') @test.it("long strings") def test_long_strings(): activity = 'a' * 1000 location = 'b' * 1000 food_source = 'c' * 1000 expected = f"I was at {location} consuming {food_source} when {activity} died." test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected) @test.it("special characters") def test_special_characters(): activity = 'c@de#wars' location = '127.0.0.1' food_source = 'coff$e!' expected = f"I was at {location} consuming {food_source} when {activity} died." test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected) @test.it("different data types") def test_different_data_types(): test.assert_equals(where_were_you_when_codewars_died(activity=True, location=123, food_source=3.14), 'I was at 123 consuming 3.14 when True died.') test.assert_equals(where_were_you_when_codewars_died(activity=None, location=None, food_source=None), 'I was at None consuming None when None died.') @test.it("large number of random inputs") def test_large_number_of_random_inputs(): for i in range(100): activity = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(1, 10))) location = random.choice(locations) food_source = random.choice(['coffee', 'pizza', 'burger', 'soda', 'water']) expected = f"I was at {location} consuming {food_source} when {activity} died." test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected) @test.it("None inputs") def test_none_inputs(): test.assert_equals(where_were_you_when_codewars_died(activity=None, location=None, food_source=None), 'I was at None consuming None when None died.') @test.it("integer inputs") def test_integer_inputs(): test.assert_equals(where_were_you_when_codewars_died(activity=123, location=456, food_source=789), 'I was at 456 consuming 789 when 123 died.') @test.it("float inputs") def test_float_inputs(): test.assert_equals(where_were_you_when_codewars_died(activity=3.14, location=2.71, food_source=1.618), 'I was at 2.71 consuming 1.618 when 3.14 died.') @test.it("boolean inputs") def test_boolean_inputs(): test.assert_equals(where_were_you_when_codewars_died(activity=True, location=False, food_source=True), 'I was at False consuming True when True died.') @test.it("random inputs with special characters") def test_random_inputs_with_special_characters(): for i in range(100): activity = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=random.randint(1, 10))) location = random.choice(locations) food_source = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=random.randint(1, 10))) expected = f"I was at {location} consuming {food_source} when {activity} died." test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected)
- import random
- import codewars_test as test
- from solution import Connection, where_were_you_when_codewars_died
- import string
- locations = ["House", "Bar", "88.131.626.98:25565", random.randint(1,10), "Haus", "There", "Their", "They're", None]
- # test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")- @test.describe("Basic Cases")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.')
- @test.it("random cases")
- def randomcases():
locations = ["House", "Bar", "88.131.626.98:25565", random.randint(1,10), "Haus", "There", "Their", "They're", None]- for i in range(100):
- findtester = random.randint(1, 1000000)
- location = random.choice(locations)
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location=location, food_source=findtester), f'I was at {location} consuming {findtester} when codewars died.')
- @test.describe("Additional Test Cases")
- def test_group():
- @test.it("empty strings")
- def test_empty_strings():
- test.assert_equals(where_were_you_when_codewars_died(activity='', location='', food_source=''), 'I was at consuming when died.')
- @test.it("long strings")
- def test_long_strings():
- activity = 'a' * 1000
- location = 'b' * 1000
- food_source = 'c' * 1000
- expected = f"I was at {location} consuming {food_source} when {activity} died."
- test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected)
- @test.it("special characters")
- def test_special_characters():
- activity = 'c@de#wars'
- location = '127.0.0.1'
- food_source = 'coff$e!'
- expected = f"I was at {location} consuming {food_source} when {activity} died."
- test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected)
- @test.it("different data types")
- def test_different_data_types():
- test.assert_equals(where_were_you_when_codewars_died(activity=True, location=123, food_source=3.14), 'I was at 123 consuming 3.14 when True died.')
- test.assert_equals(where_were_you_when_codewars_died(activity=None, location=None, food_source=None), 'I was at None consuming None when None died.')
- @test.it("large number of random inputs")
- def test_large_number_of_random_inputs():
- for i in range(100):
- activity = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(1, 10)))
- location = random.choice(locations)
- food_source = random.choice(['coffee', 'pizza', 'burger', 'soda', 'water'])
- expected = f"I was at {location} consuming {food_source} when {activity} died."
- test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected)
- @test.it("None inputs")
- def test_none_inputs():
- test.assert_equals(where_were_you_when_codewars_died(activity=None, location=None, food_source=None), 'I was at None consuming None when None died.')
- @test.it("integer inputs")
- def test_integer_inputs():
- test.assert_equals(where_were_you_when_codewars_died(activity=123, location=456, food_source=789), 'I was at 456 consuming 789 when 123 died.')
- @test.it("float inputs")
- def test_float_inputs():
- test.assert_equals(where_were_you_when_codewars_died(activity=3.14, location=2.71, food_source=1.618), 'I was at 2.71 consuming 1.618 when 3.14 died.')
- @test.it("boolean inputs")
- def test_boolean_inputs():
- test.assert_equals(where_were_you_when_codewars_died(activity=True, location=False, food_source=True), 'I was at False consuming True when True died.')
- @test.it("random inputs with special characters")
- def test_random_inputs_with_special_characters():
- for i in range(100):
- activity = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=random.randint(1, 10)))
- location = random.choice(locations)
- food_source = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=random.randint(1, 10)))
- expected = f"I was at {location} consuming {food_source} when {activity} died."
- test.assert_equals(where_were_you_when_codewars_died(activity=activity, location=location, food_source=food_source), expected)
where were u wen codewar die?
made a full change to allow for all types of input for activity, location, food_source
from typing import Any def where_were_you_when_codewars_died(activity:Any, location:Any, food_source:Any): db = Connection() record = f"I was at {location} consuming {food_source} when {activity} died." return db.add_record(record)
- from typing import Any
def where_were_you_when_codewars_died(activity:str, location:str, food_source:Any):- def where_were_you_when_codewars_died(activity:Any, location:Any, food_source:Any):
- db = Connection()
- record = f"I was at {location} consuming {food_source} when {activity} died."
- return db.add_record(record)
import random import codewars_test as test from solution import Connection, where_were_you_when_codewars_died # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.') @test.it("random cases") def randomcases(): locations = ["House", "Bar", "88.131.626.98:25565", random.randint(1,10), "Haus", "There", "Their", "They're", None] for i in range(100): findtester = random.randint(1, 1000000) location = random.choice(locations) test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location=location, food_source=findtester), f'I was at {location} consuming {findtester} when codewars died.')
- import random
- import codewars_test as test
- from solution import Connection, where_were_you_when_codewars_died
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.')
- @test.it("random cases")
- def randomcases():
- locations = ["House", "Bar", "88.131.626.98:25565", random.randint(1,10), "Haus", "There", "Their", "They're", None]
- for i in range(100):
- findtester = random.randint(1, 1000000)
test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location="Your Mothers", food_source=findtester), f'I was at Your Mothers consuming {str(findtester)} when codewars died.')- location = random.choice(locations)
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location=location, food_source=findtester), f'I was at {location} consuming {findtester} when codewars died.')
def where_were_you_when_codewars_died(activity:str, location:str, food_source:str): db = Connection() record = f"I was at {location} consuming {food_source} when {activity} died." return db.add_record(record)
def where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'):- def where_were_you_when_codewars_died(activity:str, location:str, food_source:str):
- db = Connection()
if not isinstance(activity, str) or not isinstance(location, str) or not isinstance(food_source, str):raise ValueError('activity, location, and food_source must be strings')- record = f"I was at {location} consuming {food_source} when {activity} died."
- return db.add_record(record)
import random import codewars_test as test from solution import Connection, where_were_you_when_codewars_died # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.') test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.') @test.it("random cases") def randomcases(): for i in range(100): findtester = random.randint(1, 1000000) test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location="Your Mothers", food_source=findtester), f'I was at Your Mothers consuming {str(findtester)} when codewars died.')
- import random
- import codewars_test as test
- from solution import Connection, where_were_you_when_codewars_died
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='127.0.0.1', food_source='coffee'), 'I was at 127.0.0.1 consuming coffee when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source='pizza'), 'I was at Phantom Forces consuming pizza when codewars died.')
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location='Phantom Forces', food_source=9), 'I was at Phantom Forces consuming 9 when codewars died.')
- @test.it("random cases")
- def randomcases():
- for i in range(100):
- findtester = random.randint(1, 1000000)
- test.assert_equals(where_were_you_when_codewars_died(activity='codewars', location="Your Mothers", food_source=findtester), f'I was at Your Mothers consuming {str(findtester)} when codewars died.')