Newbie Programming - Dividing a List into Dictionary Based on User Input - Few Scenarios

python

#1

Hi there,

I am a newbie into Python and programming in general. While studying python list and dictionaries I stumbled upon a scenario, hear me out please :).

Problem statement:

Scenario 1:
Given a series of numbers list
my_l = [1,3]
gc_i = 1 #i want 1 group

write a function that returns a dictionary like this
my_d = calculate_group (my_l, gc_i);

result
{
0: “1,3”
}

Scenario 2:
Given a series of numbers list
my_l = [1,3]
gc_i = 2 #i want two groups

write a function that returns a dictionary like this
my_d = calculate_group (my_l, gc_i);

result
{
0: “1”,
1: “3”
}

Scenario 3:
Given a series of numbers list
my_l = [1,3,2,33,34,36,86,88,99,5]
gc_i = 4 #i want 4 groups

write a function that returns a dictionary like this
my_d = calculate_group (my_l, gc_i);

result
{
0: "1,2,3,5"
1: “33,34,36”,
2: “86,88”,
3: “99”
}

Now my question is how to divide a list into dictionary based on number of groups provided?

Really appreciate your help.

Thanks

Sam.


#2

does the result have to be string? Can’t it be list?

the first two scenarios aren’t that challenging, but i am looking for a pattern in scenario 3.

the code should be the same for all 3 scenarios? That is tricky


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.