Fundamentals
Logic
import codewars_test as test from solution import identify_data_type @test.describe("Testing the identify_data_type function") def test_group(): @test.it("Testing for integers") def test_integer(): test.assert_equals(identify_data_type(123), "integer") test.assert_equals(identify_data_type(-987), "integer") @test.it("Testing for floats") def test_float(): test.assert_equals(identify_data_type(12.3), "float") test.assert_equals(identify_data_type(-45.67), "float") @test.it("Testing for strings") def test_string(): test.assert_equals(identify_data_type("hello"), "string") test.assert_equals(identify_data_type("123"), "string") @test.it("Testing for lists") def test_list(): test.assert_equals(identify_data_type([1, 2, 3]), "list") test.assert_equals(identify_data_type([]), "list") @test.it("Testing for tuples") def test_tuple(): test.assert_equals(identify_data_type((1, 2, 3)), "tuple") test.assert_equals(identify_data_type(()), "tuple") @test.it("Testing for dictionaries") def test_dictionary(): test.assert_equals(identify_data_type({"key": "value"}), "dictionary") test.assert_equals(identify_data_type({}), "dictionary") @test.it("Testing for booleans") def test_boolean(): test.assert_equals(identify_data_type(True), "boolean") test.assert_equals(identify_data_type(False), "boolean") @test.it("Testing for unknown data types") def test_unknown(): test.assert_equals(identify_data_type(set([1, 2, 3])), "unknown") test.assert_equals(identify_data_type(lambda x: x), "unknown")
Fundamentals
Logic
🧠 Identifiera Datatyp
✍️ Beskrivning:
Skriv en funktion med namnet identify_data_type
som tar ett argument vid namn value
. Funktionen ska returnera en sträng som representerar value
-argumentets datatyp. 🔍 Din funktion bör kunna identifiera följande datatyper:
-
integer
🔢 -
float
📏 -
string
📜 -
list
📋 -
tuple
🧩 -
dictionary
📖 -
boolean
🔘
⚠️ Begränsningar:
- Om datatypen inte är en av ovanstående, returnera "unknown" 🤷♂️.
- Du får använda Pythons inbyggda
type()
funktion för att bestämma datatypen, men inga andra inbyggda funktioner eller bibliotek får användas i detta syfte.
📌 Exempel:
identify_data_type(123) # "integer"
identify_data_type(12.3) # "float"
identify_data_type("hello") # "string"
identify_data_type([1, 2, 3]) # "list"
identify_data_type((1, 2, 3)) # "tuple"
identify_data_type({"key": "value"}) # "dictionary"
identify_data_type(True) # "boolean"
identify_data_type(set([1, 2, 3])) # "unknown"
def identify_data_type(value):
pass # Ta bort 'pass' och skriv din kod här.
import codewars_test as test
from solution import identify_data_type
@test.describe("Testing the identify_data_type function")
def test_group():
@test.it("Testing for integers")
def test_integer():
test.assert_equals(identify_data_type(123), "integer")
test.assert_equals(identify_data_type(-987), "integer")
@test.it("Testing for floats")
def test_float():
test.assert_equals(identify_data_type(12.3), "float")
test.assert_equals(identify_data_type(-45.67), "float")
@test.it("Testing for strings")
def test_string():
test.assert_equals(identify_data_type("hello"), "string")
test.assert_equals(identify_data_type("123"), "string")
@test.it("Testing for lists")
def test_list():
test.assert_equals(identify_data_type([1, 2, 3]), "list")
test.assert_equals(identify_data_type([]), "list")
@test.it("Testing for tuples")
def test_tuple():
test.assert_equals(identify_data_type((1, 2, 3)), "tuple")
test.assert_equals(identify_data_type(()), "tuple")
@test.it("Testing for dictionaries")
def test_dictionary():
test.assert_equals(identify_data_type({"key": "value"}), "dictionary")
test.assert_equals(identify_data_type({}), "dictionary")
@test.it("Testing for booleans")
def test_boolean():
test.assert_equals(identify_data_type(True), "boolean")
test.assert_equals(identify_data_type(False), "boolean")
@test.it("Testing for unknown data types")
def test_unknown():
test.assert_equals(identify_data_type(set([1, 2, 3])), "unknown")
test.assert_equals(identify_data_type(lambda x: x), "unknown")