Reputation: 572
I'm new to python, so let's say I have 2 input, the first one is an int with name my_shoes
, the next is array list of the colour shoes called colour_list
.
my_shoes is contained the number of each shoe that I have, and colour_list has contained the list of the array for each colour of them, and it represented by numbers.
case 1
my_shoes = 10
colour = [1,1,2,2,2,3,3,4,5,6]
output
"I have 3 pairs of shoes"
explanation: because there's colour 1 who had to be pairs and 2 who had be pairs, and who had been pairs
my_shoes = 9
colour = [1,1,2,2,3]
output
"The number has no match"
my_shoes = 9
colour = [1,2,1,2,1,3,3,3,3]
output
"I have 4 pairs of shoes"
I've tried with this code
my_shoes = 11
colour_list = ['a','b','c','d','e','b','n','n','c','c','h',]
to_be_pair = ()
duplicates = []
while my_shoes == range(len(colour_list)):
for values in some_list:
if some_list.count(values) == 2:
to_be_pair += 1
if values not in to_be_pair:
to_be_pair.append(values)
print("i have : ",to_be_pair , "pairs")
but the output was says i have : () pairs
Upvotes: 1
Views: 973
Reputation: 8508
Alternate to Counter is to use set and list.count() option.
Set in python will remove duplicates and create a set of unique values. You can then iterate through the set and do a count of each of the elements against the original colours list. That way you get count of each element. Since you need to get pairs, divide the count by 2. By using //
it will automatically floor the value and give you an int. The sum of all the returned values will be the total pairs of shoes.
The implementation of that will be as shown below:
def get_pairs(shoes, colours):
if len(colours) < shoes:
return f'The number {shoes} has no match in {colours}'
pairs = sum(colours.count(c)//2 for c in set(colours))
if pairs > 0:
return f'I have {pairs} pairs of shoes in {colours}'
else:
return f'The number {shoes} has no match in {colours}'
print (get_pairs(10, [1,1,2,2,2,3,3,4,5,6]))
print (get_pairs(8, [1,1,2,2,3]))
print (get_pairs(9, [1,2,1,2,1,3,3,3,3]))
print (get_pairs(6, [1,2,3,4,5,6]))
The output of this will be:
I have 3 pairs of shoes in [1, 1, 2, 2, 2, 3, 3, 4, 5, 6]
The number 8 has no match in [1, 1, 2, 2, 3]
I have 4 pairs of shoes in [1, 2, 1, 2, 1, 3, 3, 3, 3]
The number 6 has no match in [1, 2, 3, 4, 5, 6]
You can replace the get_pairs function to have variables too as follows:
print (get_pairs(my_shoes, colours))
Upvotes: 1
Reputation: 4799
Basically, the idea is to count how many times two things occur in a list. To keep track of what we've seen, we can use a dictionary.
For simplicity, we can use a counter, then do some simple division to get the number of pairs:
from collections import Counter
def get_pairs(colours):
colour_counts = Counter(colours)
pairs = 0
for colour in colour_counts.values():
pairs += colour // 2
return pairs
The Counter
object keeps track of how many of each item we've seen so far. Then, we take the counts (and ignore the key/identity of the item, since we only care about counts). Next, we do integer division by 2, which will round down to give us the number of pairs for that particular item type. By keeping a cumulative sum, we end up with our total number of pairs.
Upvotes: 2