Asking about how to write the code

Define a function replaceCharAtPos(orig,pos) which receives two input parameters, a string (orig) and a positive integer number (pos).

If the number is a position within the string, the function should return a new string. The new string should be the original string except that instead of the original character in that position pos, it should have the position number itself (if the position number has more than 1 digit, the character should be replaced by the digit in the units part of the position number, e.g. 2 if the position number is 12).

If the number pos is not a valid position within the original string orig, the string returned should be exactly the same as the original string.

For example
replaceCharAtPos('abcd',1) should return 'a1cd'
replaceCharAtPos('abcd',10) should return 'abcd'
replaceCharAtPos('abcdefghijklmn',12) should return 'abcdefghijkl2n'

As an example, the following code fragment:
print (replaceCharAtPos("abcd",2))
should produce the output:

You are viewing a single comment. View All
JoshDaBosh (79)

I'm not sure if this is your cs course hw but I'd suggest trying it yourself first.

Anyway, here's the function:

def replaceCharAtPos(s, i):
    if i > (len(s)-1):
        return s
    s = list(s)
    s[i] = str(i)
    return ''.join(s)
JoshDaBosh (79)

@TZLALA how is it not correct

JoshDaBosh (79)

@TZLALA unless you're just copy-pasting into your homework portal i don't see why it wouldn't work

what's the issue?


@JoshDaBosh How can I deal with this?