0

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.

Any kind guy can help me with this?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497

4 Answers4

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:

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.

Community
  • 1
  • 1
EvenLisle
  • 4,672
  • 3
  • 24
  • 47
0
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 :

o/p

b****a

you should compare the NUMERIC position values that you are passing

backtrack
  • 7,996
  • 5
  • 52
  • 99
0

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.

I think that's not you want.

wanderlust
  • 1,826
  • 1
  • 21
  • 25
-1

It should be

if count==pos1 or count == pos2:

and not

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

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
Smart Home
  • 801
  • 7
  • 26