# Python Program to find out the number of matches in an array containing pairs of (base, number)

Suppose, we are given several different pairs in the format (x, y). Here the x signifies the base of a number and y signifies the number itself. In the list there are pairs that mean the same. We have to check the number of matches in the given number pairs. The given pairs can be redundant, and can also contain invalid base-number combinations.

So, if the input is like num_inputs = 2, input_arr = [(10, 15), (8, 17)], then the output will be 1.

The variable num_inputs specify the number of inputs, and the array input_arr lists the number pairs. Here if we look at the two pairs; 15 in base 10 (decimal) is the same as 17 in base 8 (octal). So, there is one match and we return output value 1.

To solve this, we will follow these steps −

• arr_len := size of input_arr

• temp_dict := a new map containing integer values

• for i in range 0 to num_inputs, do

• num_base := string representation of the first number of pair i in input_arr

• num_val := string representation of the second number of pair i in input_arr

• temp_dict[integer representation of(num_val, integer representation of(num_base)) ] := temp_dict[integer representation of(num_val, integer representation of(num_base)) ] + 1

• cnt := 0
• for each value in list of all values of temp_dict, do
• cnt := cnt + value* floor value of ((value - 1) / 2)
• return cnt

## Example

Let us see the following implementation to get better understanding −

Live Demo

from collections import defaultdict
def solve(num_inputs, input_arr):
arr_len = len(input_arr)
temp_dict = defaultdict(int)
for i in range(num_inputs):
num_base, num_val = str(input_arr[i][0]), str(input_arr[i][1])
temp_dict[int(num_val, int(num_base))] += 1
cnt = 0
for value in temp_dict.values():
cnt += value*(value - 1)//2
return cnt
print(solve(2, [(10, 15), (8, 17)]))

## Input

2, [(10, 15), (8, 17)]

## Output

1

Updated on: 18-May-2021

303 Views