Python Shoes Matching | Competitive Coding Questions

Python Competitive Coding Questions | Shoes Matching: This practice is based on Shoes Matching in Python programming language.
Submitted by Prerana Jain, on April 25, 2020

Question

Zack has messed up all his shoes. He has N shoes (single shoe, not paired).  You have to search for the partner of each shoe. Find the maximum pair you can make out of them.

You will be given a description of each shoe i.e. the size of the shoes and if it is left shoes (L) or right shoes (R). Two shoes can be paired if they are having the same size and one is left L and the other is right R.

Input

The first line contains a single integer N. The number of shoes Next N lines has a description for each shoe, with spaces, separated size, and L/R.

5
1	L
2	R
3	L
1	R
2	L

Output

A single integer, maximum pairs can be made out of the given shoes.

2

Constraints

1<=N<=1000
Shoe size will be a positive integer less than 100.

Explanation/Solution

In this question, we are given a number of shoes with their size and type whether it is left or right. Our aim is to make a pair of shoes which have the same size and one is left and the other is right. So we make two array one have the shoe size and other having the shoe type. Each time we compare the shoe size with other shoes and also check the types of shoes it is left or right the make one pair of shoes.

To solve this question, we are using Python3.

Python Code

s = int(input())

arr = [ ]
brr = [ ]
p = s

while(s>0):
  s-=1
  a,b = input().split()
  arr.append(int(a))
  brr.append(b)

i,j,count=0,0,0
s = p

while(i<s-1):
  j=i+1
  while(j<s):
    if(arr[i]==arr[j] and brr[j]!= brr[i] and brr[j]!='S' and brr[i]!='S'):
      brr[i]='S'
      brr[j]='S'
      count+=1
      break
    j+=1
  i+=1
print(count)

Output

6
1 L
2 L
3 L
1 R
2 R
3 R
3

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.