📜  弦对| TCS Codevita 2020

📅  最后修改于: 2021-04-23 21:32:37             🧑  作者: Mango

一个人将数字列表移交给String先生,但是String先生只懂字符串。在字符串,他也只理解元音。斯特林先生需要您的帮助,以找到总和达到特定数字D的总对数。

计算数字D的规则如下:

  • 取所有数字并将其转换为文本表示形式。
  • 接下来,从所有文字表示中总结出元音的数量,即{a,e,i,o,u} 。该总和为数字D。
  • 现在,一旦知道了数字D,就找出输入中所有和为D的无序数字对。

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

例子:

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

  • 将每个数字从0100的文本表示形式存储在Map中
  • 遍历数组,对于每个数组元素,将每个数字转换为其文本形式。
  • 查找文本形式中存在的元音总数,并将其存储在变量中,例如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 )