| title | Type Based Output | ||
|---|---|---|---|
| tags |
|
Question 1: Data Types
Implement a function data_operations(variable) that takes input: 1 : An integer . 2 : A floating-point number. 3 : A string . 4 : A list . 5 : A dictionary e where keys are strings and values are integers. 6 : A set. The function should perform the following do operation on varible: 1 : Multiply the integer a by 2 and return the result. 2 : Add 5.5 to the float b and return the result. 3 : Return the uppercase version of the string c. 4 : Append the integer 10 to the list d and return it. 5 : Add a new key-value pair {"extra": 100} to the dictionary e and return it. 6 : Add the value 99 to the set f and return it.
Return the result as mentioned above.
<prefix>
</prefix>
<template>
<sol>
def data_operations(variable):
typo = type(variable).__name__
match typo:
case int :
return variable * 2
case float:
return variable + 5.5
case str:
return variable.upper()
case list:
return variable + [10]
case dict :
return {**variable, "extra": 100}
case set :
return variable | {99}
case default:
return "Wrong Input"
</sol>
</template>
<suffix>
</suffix>
<suffix_invisible>
</suffix_invisible>4
(data_operations(variable) == 8)
2.5
(data_operations(variable) == 8.0)
"hello"
(data_operations(variable) == "HELLO")
[1, 2]
(data_operations(variable) == [1, 2, 10])
{"key1": 1}
(data_operations(variable) == {"key1": 1, "extra": 100})
{1, 2, 3}
(data_operations(variable) == {1, 2, 3, 99})
0
(data_operations(variable) == 0)
-1.5
(data_operations(variable) == 4.0)
"world"
(data_operations(variable) == "WORLD")
[]
(data_operations(variable) == [10])
{}
(data_operations(variable) == {"extra": 100})
set()
(data_operations(variable) == {99})