【问题标题】:function that creates a new words by given pattern通过给定模式创建新单词的函数
【发布时间】:2019-03-26 01:14:50
【问题描述】:

我试图创建一个函数,该函数接受一个单词(大写字母和小写字母)并将每个字符映射到一个新字符。模式是每个元音 (AEIOU) 按顺序 (A -> E, E - > I) 成为下一个元音。对于常量字母变成三分之二的字母 (B -> F, C -> G)

>>>'hello'
'lippu'
>>> 'today'
'xuhec'
>>> 'yesterday'
'ciwxivhec'

我知道我必须创建两个列表:

vowels = ['a', 'e', 'i', 'o', 'u']
constants = ['b', 'c','d','f','g','h','j','k','l','m','n','p', 'q','r', 's','t','v','w','x','y', 'z']

并使用 index() 函数来检查当前索引并将 3 添加到它,但在那之后我卡住了。

对于超出列表的情况,字母会循环回来。 (x-z, 和 u)

【问题讨论】:

  • 常量字母会发生什么,例如:Z,z:我们不能在索引中添加 3?
  • @killuminati 以及 u
  • 它将再次从 a 开始所以 (z -> d) (u -> a) @vash_the_stampede @killuminati
  • 是 z -> c 还是 z -> d?

标签: python function


【解决方案1】:

要计算地图,您可以使用enumerate(获取当前的索引)和模(对于大于列表长度的索引),如下所示:

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

vowels_map = {k: vowels[(i + 1) % len(vowels)] for i, k in enumerate(vowels)}
consonants_map = {k: consonants[(i + 3) % len(consonants)] for i, k in enumerate(consonants)}

print(vowels_map)
print(consonants_map)

输出

{'u': 'a', 'a': 'e', 'o': 'u', 'e': 'i', 'i': 'o'}
{'s': 'w', 'z': 'd', 'v': 'y', 'm': 'q', 'f': 'j', 'h': 'l', 'd': 'h', 'g': 'k', 'q': 't', 'n': 'r', 'p': 's', 'k': 'n', 't': 'x', 'y': 'c', 'r': 'v', 'w': 'z', 'x': 'b', 'l': 'p', 'b': 'f', 'j': 'm', 'c': 'g'}

请注意,字典没有顺序,也就是说您可以按以下方式使用它们:

def replace_from_dict(word, table):
    return ''.join(table[c] for c in word)


words = ['hello',
         'today',
         'yesterday']

for word in words:
    print(replace_from_dict(word, { **vowels_map, **consonants_map }))

输出 (来自使用replace_from_dict)

lippu
xuhec
ciwxivhec

【讨论】:

    【解决方案2】:

    我们可以使用itertools.cycle。首先检查i 属于vowelconsonants 的哪个类别(不是常量)。然后从相应的列表中创建一个cycle,使用whilenext,直到我们到达相应的字母。如果它是vowel,我们只需附加next 值,如果它是consonant,我们前进2 个位置,然后附加next 值。使用.join()转回字符串后。

    from itertools import cycle
    
    vwl = ['a', 'e', 'i', 'o', 'u']
    cnst = ['b', 'c','d','f','g','h','j','k','l','m','n','p', 'q','r', 's','t','v','w','x','y', 'z']
    
    s = 'hello'
    new = []
    for i in s.lower():
        if i in vwl:
            a = cycle(vwl)
            while i != next(a):
                next(a)    
            new.append(next(a))
        if i in cnst:
            b = cycle(cnst)
            while i != next(b):
                next(b)
            for x in range(2):
                next(b)
            new.append(next(b))
    
    res = ''.join(new)
    print(res)
    # lippu
    

    适用于包含边缘字母的单词,zumba 生成 daqfe

    【讨论】:

    • 哦,我正在考虑使用 mod (%) 来环绕列表。那行得通吗? (假设我们无法导入)
    • @errick-michael-santos 抱歉,PC 不再使用,但是如果我不使用循环,我可能会使用 try except,一旦停止迭代,重新启动迭代器并继续,可以在我返回时显示将与循环概念一样工作,只是手动回到开头
    【解决方案3】:

    我为边缘情况定义了两个字典,vowel_dictionary 和一个用于字母 x/y/z 的字典以实现换行。我遍历字符串,如果字符是特殊字符,我使用适当的字典来查找单词。但是,如果字符低于 'w' 而不是元音,我只需将 4 添加到其ord 值(ASCII 值)并将其转换为字符。

    def transform(input):
      return_string = ""
      vowel_dictionary = {
        'a': 'e',
        'e': 'i',
        'i': 'o',
        'o': 'u',
        'u': 'a'
      }
      edge_dictionary = {
        'x': 'b',
        'y': 'c',
        'z': 'd'
      }
      for character in input.lower():
        if character in vowel_dictionary:
          return_string += vowel_dictionary[character]
        elif ord(character) <= ord("v"):
          return_string += chr(ord(character) + 4)
        else :
          return_string += edge_dictionary[character]
    
      return return_string
    

    我已经用上面的代码进行了一些测试:

    测试

    transform("hello") # => lippu
    
    transform("today") # => xuhec
    
    transform("yesterday") # => ciwxivhec
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多