Skip to content

Zikkolion/py-create-dict

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Create dict

Task

You already know, that not all data types can be used as a keys in dictionary in Python. But someone, who pass arguments to function create_dict does not. You need to help him:

Write create_dict function, which will receive as many arguments as needed and create a dictionary, in which keys are these arguments (each argument is different key) provided to the function and values are the positions of these arguments.

Of course, not all arguments provided will be accepted for dict creation. Print f"Cannot add {argument} to the dict!" for each argument, that is not accepted.

It is agreed, that arguments provided to create_dict function will be only of next types: int, float, str, bool, NoneType, list, tuple, set, dict, function.

Also in this task, you should not use hash function or try/except constructions. Hint: use isinstance.

Required Task

Create function create_dict if:

For all arguments provided to create_dict we will have maximum nesting level = 2:

  • create_dict([1, 2, [1, 2]]) - nesting level = 2, because first parameter has list in list - ok.
  • create_dict([1, 2, 3]) - nesting level = 1 - ok, because list is already has level 1.
  • create_dict(1) - nesting level = 0 - ok.
  • create_dict([1, [2, [3, 4]]]) - nesting level = 3 - not ok, won't be passed such list in function.

Examples:

print(create_dict(7, 1, 3))
# {7: 0, 1: 1, 3: 2}
print(create_dict(3, [1, 2], 5))
# Cannot add [1, 2] to the dict!
# {3: 0, 5: 2}
print(create_dict(3, (1, 2), 5))
# {3: 0, (1, 2): 1, 5: 2}
print(create_dict(3, (1, [2, 3]), 5))
# Cannot add (1, [2, 3]) to the dict!
# {3: 0, 5: 2}

Optional Task

Create function create_dict if:

Each collection type provided to the create_dict function is not limited to any nesting level.

If you want to solve this Task - go to tests/test_main.py file and uncomment the full last test :)

Examples:

print(create_dict(7, 1, 3))
# {7: 0, 1: 1, 3: 2}
print(create_dict(3, [1, 2], 5))
# Cannot add [1, 2] to the dict!
# {3: 0, 5: 2}
print(create_dict(3, (1, 2), 5))
# {3: 0, (1, 2): 1, 5: 2}
print(create_dict(3, (1, (10, (7, [2, 3]))), 5))
# Cannot add (1, (10, (7, [2, 3]))) to the dict!
# {3: 0, 5: 2}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%