Ad
Matrix

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.")

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.

Code
Diff
  • 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)

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
Mathematics

Added Test Cases
Improved readability
lambda

Code
Diff
  • 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 = args
    • if sign == "/" and n2 == 0:
    • return 0
    • return 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)

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

where were u wen codewar die?

made a full change to allow for all types of input for activity, location, food_source

Code
Diff
  • 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)
Code
Diff
  • 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)