
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
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
- Related Articles
- Program to find out the number of pairs of equal substrings in Python
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Program to find number of good pairs in Python
- Program to find out the number of shifts required to sort an array using insertion sort in python
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- C++ program to find out the number of coordinate pairs that can be made
- Program to find out the number of accepted invitations in Python
- Program to find max number of K-sum pairs in Python
- Program to count number of matches played in tournament in Python
- Program to find number of good leaf nodes pairs using Python
- Program to Find Out the Number of Squares in a Grid in Python
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- Program to find out the sum of the number of divisor of the divisors in Python
- Program to find expected number of shuffle required to sort the elements of an array in Python
