Knowee
Questions
Features
Study Tools

K digit elementsGiven a list of tuples, extract all tuples having K digit elements.Constraints:NAExample:Input :554 234 55222 2312 45782 Output :[(34, 55), (12, 45), (78,)] Explanation:All tuples have numbers with 2 digits.

Question

K digit elementsGiven a list of tuples, extract all tuples having K digit elements.Constraints:NAExample:Input :554 234 55222 2312 45782 Output :[(34, 55), (12, 45), (78,)] Explanation:All tuples have numbers with 2 digits.

🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to extract all tuples from a list that have elements with K digits. Here is a step-by-step solution in Python:

  1. First, define a function that takes a list of tuples and an integer K as input.
def extract_tuples(lst, K):
  1. Initialize an empty list to store the tuples that meet the condition.
    result = []
  1. Iterate over each tuple in the list.
    for tup in lst:
  1. For each tuple, iterate over its elements.
        for num in tup:
  1. Check if the number of digits in the current element is equal to K. You can convert the number to a string and use the len() function to get the number of digits.
            if len(str(num)) == K:
  1. If the condition is met, append the tuple to the result list. To avoid duplicates, check if the tuple is not already in the result list before appending.
                if tup not in result:
                    result.append(tup)
  1. After iterating over all tuples, return the result list.
    return result

The complete function is:

def extract_tuples(lst, K):
    result = []
    for tup in lst:
        for num in tup:
            if len(str(num)) == K:
                if tup not in result:
                    result.append(tup)
    return result

You can call this function with a list of tuples and an integer K to get the desired output. For example:

print(extract_tuples([(554, 234), (55222,), (2312, 45782)], 2))  # Output: [(234, 554), (45782,)]

This problem has been solved

Similar Questions

Given Binary Tuple representing  binary representation of number, convert to integer.Constraints:NAExample:Input:1 1 0Output :6

Number and its cubeGiven a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number.Constraints:NAExample:Input:1 2 3Output:[(1, 1), (2, 8), (3, 27)]

Common elementGiven two lists a, b. Check if two lists have at least one element common in them.Constraints:NAExample:Input :1 2 3 4 55 6 7 8 9Output :True

Problem StatementFind the remainder on dividing every element of first tuple with the element of second tuple .Input FormatInput elements of first tupleInput elements of second tupleOutput FormatPrint the remainders of first tuple with second tupleSample Input11,74,85,66,32,54,2510,12,15,14,12,3,15Sample Output(1, 2, 10, 10, 8, 0, 10)

Problem StatementYou are given a string s of length n. You want to find all the distinct non-empty strings that can be formed from s such that:No two adjacent characters from the string s are chosen in the string that we make from s.Since the answer can be large print it modulo 109+7.Input FormatThe only line of input contains a string s.Constraints1 <= n <= 105Output FormatReturn an integer as asked.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.