📜  字符串对 | TCS Codevita 2020

📅  最后修改于: 2021-09-07 04:42:07             🧑  作者: Mango

一个人将数字列表交给 String 先生,但 String 先生只懂字符串。在字符串,他也只能理解元音。 String 先生需要您的帮助来找出加起来等于某个数字D的对的总数。

数字D的计算规则如下:

  • 获取所有数字并将它们转换为它们的文本表示。
  • 接下来,从所有文本表示中总结元音的数量,即{a, e, i, o, u} 。这个总和是数字D
  • 现在,一旦知道数字D ,找出输入中所有和等于D 的无序数字对。

问题陈述:给定一个由N ( 1 ≤ N ≤ 100 ) 个整数组成的数组arr[] ,任务是将每个数组元素 ( 1 ≤ arr[i] ≤ 100 ) 转换为它们各自的文本表示并打印数组中所有可能对的计数,其总和等于其文本表示中存在的元音总数。如果计数超过 100,则打印“greater 100”
注意:对于数字 100,将其转换为文本表示形式为而不是

例子:

方法:按照以下步骤解决此问题:

  • Map 中存储从0100的每个数字的文本表示。
  • 遍历数组,对于每个数组元素,将每个数字转换为其文本形式。
  • 找出文本形式中存在的元音总数并将其存储在一个变量中,比如D
  • 现在,从给定的数组中生成所有可能的对。
  • 计算总和为D 的所有对。
  • 如果计数超过 100,则“大于 100” 。否则,打印其文本形式。

下面是上述方法的实现:

Python3
# Python3 program for the above approach
  
# Import combinations
from itertools import combinations
  
# Function to check the string pair
def string_pair(n, nums):
    words = {0: 'zero', 1: 'one', 2: 'two', 3: 'three',
             4: 'four', 5: 'five', 6: 'six', 7: 'seven',
             8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven',
             12: 'twelve', 13: 'thirteen', 14: 'fourteen',
             15: 'fifteen', 16: 'sixteen', 17: 'seventeen',
             18: 'eighteen', 19: 'nineteen', 20: 'twenty',
             21: 'twentyone', 22: 'twentytwo', 23: 'twentythree',
             24: 'twentyfour', 25: 'twentyfive', 26: 'twentysix',
             27: 'twentyseven', 28: 'twentyeight', 29: 'twentynine',
             30: 'thirty', 31: 'thirtyone', 32: 'thirtytwo',
             33: 'thirtythree', 34: 'thirtyfour', 35: 'thirtyfive',
             36: 'thirtysix', 37: 'thirtyseven', 38: 'thirtyeight',
             39: 'thirtynine', 40: 'forty', 41: 'fortyone',
             42: 'fortytwo', 43: 'fortythree', 44: 'fortyfour',
             45: 'fortyfive', 46: 'fortysix', 47: 'fortyseven',
             48: 'fortyeight', 49: 'fortynine', 50: 'fifty',
             51: 'fiftyone', 52: 'fiftytwo', 53: 'fiftythree',
             54: 'fiftyfour', 55: 'fiftyfive', 56: 'fiftysix',
             57: 'fiftyseven', 58: 'fiftyeight', 59: 'fiftynine',
             60: 'sixty', 61: 'sixtyone', 62: 'sixtytwo',
             63: 'sixtythree', 64: 'sixtyfour', 65: 'sixtyfive',
             66: 'sixtysix', 67: 'sixtyseven', 68: 'sixtyeight',
             69: 'sixtynine', 70: 'seventy', 71: 'seventyone',
             72: 'seventytwo', 73: 'seventythree', 74: 'seventyfour',
             75: 'seventyfive', 76: 'seventysix', 77: 'seventyseven',
             78: 'seventyeight', 79: 'seventynine', 80: 'eighty',
             81: 'eightyone', 82: 'eightytwo', 83: 'eightythree',
             84: 'eightyfour', 85: 'eightyfive', 86: 'eightysix',
             87: 'eightyseven', 88: 'eightyeight', 89: 'eightynine',
             90: 'ninety', 91: 'ninetyone', 92: 'ninetytwo',
             93: 'ninetythree', 94: 'ninetyfour', 95: 'ninetyfive',
             96: 'ninetysix', 97: 'ninetyseven', 98: 'ninetyeight',
             99: 'ninetynine', 100: 'hundred'}
  
    # Map the string into list of integers
    nums = list(map(int, nums))
  
    # Temporary lists to store list of count
    ls, ls1 = [], []
    count, c = 0, 0
  
    # Iterating through the numbers
    for i in nums:
  
        # Stores the textual form of i
        s = words[i]
        for a in range(len(s)):
            vo = ['a', 'e', 'i', 'o', 'u']
  
            # If it is vowel
            if s[a] in vo:
  
                # Increment the count
                count += 1
  
        # Append the count
        ls.append(count)
        count = 0
  
    # D = sum(count of vowels)
    d = sum(ls)
    for i in nums:
  
      # To find the numbers less
      # that or equal to d,
      # so as to find the pair sum
        if i <= d:
  
            # Append the numbers
            # whose sum can be d
            ls1.append(i)
  
    # Stores all possible pairs in the
    # form of an object list of tuples
    comb = combinations(ls1, 2)
  
    # Traverse all the pairs
    for i in list(comb):
  
        # If sum is equal to d
        if sum(i) == d:
  
            # Increment count
            c += 1
  
    # If count exceeds 100
    if c <= 100:
        print(words)
  
    # Otherwise
    else:
        print("greater 100")
  
  
# Driver Code
if __name__ == '__main__':
  
    # Given Length of string
    n = 5
  
    # Given array
    arr = [1, 2, 3, 4, 5]
  
    # Function Call
    string_pair(n, arr)


输出:
one

时间复杂度: O(N 2 )
辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live