按位置更改字符串中的字符

时间:2022-09-26 11:14:55

question is : Write a function that accepts three parameters, a string and two integers. The string represents a word in a guessing game. The two integer represent positions to keep the letters as a starting point. The remaining letters should be replaced by the * symbol. The function should return the resulting string.

问题是:编写一个接受三个参数的函数,一个字符串和两个整数。该字符串代表猜谜游戏中的一个单词。两个整数表示将字母保持为起点的位置。其余字母应替换为*符号。该函数应返回结果字符串。

The doctests below should make this clear:

下面的文件应该清楚说明:

def hangman_start(strng, pos1, pos2):
    """
    >>> hangman_start("banana", 0, 5)
    'b****a'
    >>> hangman_start("passionfruit", 0, 7)
    'p******f****'
    >>> hangman_start("cherry", 3, 4)
    '***rr*'
    >>> hangman_start("peach", 2, 10)
    '**a**'
    >>> hangman_start("banana", -1, -1)
    '******'
    """
if __name__=="__main__":
    import doctest
    doctest.testmod(verbose=True)

I tried do this as below:

我试过这样做如下:

def hangman_start(strng, pos1, pos2):    
    count=0
    result=""
    while count<len(strng):
        if strng[count]==strng[pos1] or strng[count] == strng[pos2]:
             result += strng[count]
        else:
            result += "*"
        count+=1
    return result

but it does not work properly. such as: hangman_start("banana", 0, 5) i got ba*a*a.

但它无法正常工作。例如:hangman_start(“banana”,0,5)我得到了ba * a * a。

Any kind guy can help me with this?

任何善良的人都可以帮助我吗?

4 个解决方案

#1


If I understand you correctly, you want to replace all characters except on the provided positions by *:

如果我理解正确,你想用*替换所提供位置以外的所有字符:

def hangman_start(strng, pos1, pos2):
  return "".join([char if index in (pos1,pos2) else '*' for index, char in enumerate(strng)])

print hangman_start("asdasd", 3, 4)

The above prints

以上打印

***as*

If you want to stick with your implementation, just replace the character-at-index comparison with just index comparison:

如果你想坚持你的实现,只需用索引比较替换index-index比较:

def hangman_start(strng, pos1, pos2):
  count=0
  result=""
  while count<len(strng):
    if count == pos1 or count == pos2:
      result += strng[count]
    else:
      result += "*"
    count+=1
  return result

While the input here is not large enough for it to matter, I'd like to suggest you append to a list and then join the list, rather than append to a string, as this is much, much more efficient:

虽然这里的输入不够大,但是我想建议你附加到列表然后加入列表,而不是追加到字符串,因为这样更有效:

def hangman_start(strng, pos1, pos2):
  count=0
  result=[]
  while count<len(strng):
    if count == pos1 or count == pos2:
      result.append(strng[count])
    else:
      result.append("*")
    count+=1
  return "".join(result)

Like I said, the input is not large enough for it to matter in this case, but it's a good habit to adopt.

就像我说的那样,在这种情况下,输入的大小不足以让它变得很重要,但这是一个很好的习惯。

#2


def hangman_start(strng, pos1, pos2):    
    count=0
    result=""
    while count<len(strng):
        if count ==pos1 or count== pos2 :
             result += strng[count]
        else:
            result += "*"
        count+=1
    return result

h = hangman_start("banana", 0, 5)

print(h)

the solution is if count ==pos1 or count== pos2 :

解决方案是如果count == pos1或count == pos2:

o/p

b****a

you should compare the NUMERIC position values that you are passing

您应该比较您传递的NUMERIC位置值

#3


This part is wrong.

这部分是错的。

if strng[count]==strng[pos1] or strng[count] == strng[pos2]:

You here try to compare if char strng[count] in position count equals to the char strng[pos1] in position pos1 or to the char strng[pos2] in pos2.

你在这里尝试比较位置计数中的char strng [count]是否等于pos1位置的char strng [pos1]或pos2中的char strng [pos2]。

I think that's not you want.

我想那不是你想要的。

#4


It should be

它应该是

if count==pos1 or count == pos2:

如果count == pos1或count == pos2:

and not

if strng[count]==strng[pos1] or strng[count] == strng[pos2]:

如果strng [count] == strng [pos1]或strng [count] == strng [pos2]:

#1


If I understand you correctly, you want to replace all characters except on the provided positions by *:

如果我理解正确,你想用*替换所提供位置以外的所有字符:

def hangman_start(strng, pos1, pos2):
  return "".join([char if index in (pos1,pos2) else '*' for index, char in enumerate(strng)])

print hangman_start("asdasd", 3, 4)

The above prints

以上打印

***as*

If you want to stick with your implementation, just replace the character-at-index comparison with just index comparison:

如果你想坚持你的实现,只需用索引比较替换index-index比较:

def hangman_start(strng, pos1, pos2):
  count=0
  result=""
  while count<len(strng):
    if count == pos1 or count == pos2:
      result += strng[count]
    else:
      result += "*"
    count+=1
  return result

While the input here is not large enough for it to matter, I'd like to suggest you append to a list and then join the list, rather than append to a string, as this is much, much more efficient:

虽然这里的输入不够大,但是我想建议你附加到列表然后加入列表,而不是追加到字符串,因为这样更有效:

def hangman_start(strng, pos1, pos2):
  count=0
  result=[]
  while count<len(strng):
    if count == pos1 or count == pos2:
      result.append(strng[count])
    else:
      result.append("*")
    count+=1
  return "".join(result)

Like I said, the input is not large enough for it to matter in this case, but it's a good habit to adopt.

就像我说的那样,在这种情况下,输入的大小不足以让它变得很重要,但这是一个很好的习惯。

#2


def hangman_start(strng, pos1, pos2):    
    count=0
    result=""
    while count<len(strng):
        if count ==pos1 or count== pos2 :
             result += strng[count]
        else:
            result += "*"
        count+=1
    return result

h = hangman_start("banana", 0, 5)

print(h)

the solution is if count ==pos1 or count== pos2 :

解决方案是如果count == pos1或count == pos2:

o/p

b****a

you should compare the NUMERIC position values that you are passing

您应该比较您传递的NUMERIC位置值

#3


This part is wrong.

这部分是错的。

if strng[count]==strng[pos1] or strng[count] == strng[pos2]:

You here try to compare if char strng[count] in position count equals to the char strng[pos1] in position pos1 or to the char strng[pos2] in pos2.

你在这里尝试比较位置计数中的char strng [count]是否等于pos1位置的char strng [pos1]或pos2中的char strng [pos2]。

I think that's not you want.

我想那不是你想要的。

#4


It should be

它应该是

if count==pos1 or count == pos2:

如果count == pos1或count == pos2:

and not

if strng[count]==strng[pos1] or strng[count] == strng[pos2]:

如果strng [count] == strng [pos1]或strng [count] == strng [pos2]: