Move History

Fork Selected
  • Description

    where were u wen codewar die?

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

    Code
    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)
    
    Preloaded Code
    import sqlite3
    
    
    class Connection:
        def __init__(self):
            self.conn = sqlite3.connect('kumite.db')
            self.curr = self.conn.cursor()
            self.create_table()
    
        def create_table(self):
            sql = """CREATE TABLE IF NOT EXISTS kumite (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            record TEXT       
            )"""
            self.curr.execute(sql)
    
        def add_record(self, data):
            sql = """INSERT INTO kumite (record) VALUES (?);"""
            try:
                self.curr.execute(sql, (data,))              
            except sqlite3.Error as e:
                print(f'Exception: {e}')
            try:
                i = self.curr.lastrowid
                sql = """SELECT record  FROM kumite WHERE id = ?"""
                return self.curr.execute(sql, (i,)).fetchone()[0]  
            except sqlite3.Error as e:
                print(f'Exception: {e}')
            finally:
                self.conn.commit()
                self.conn.close()       
    
    Test Cases
    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.')
                
    
  • Code
    • 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)
    Test Cases
    • 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.')