I’m new to computer science and really stuck on this question so any help would be great :).
Firstly I was given the following global variable:
ARROWS = ‘<>^v’
The idea is to create a function that takes in a string and an integer (n). The function should then replace the first n number of characters with ‘X’. So far this is easy, however, the problem is that the characters should ONLY be replaced if it is a part of the global variable ARROWS. If it isn’t, it should not be modified but still counts as one of the n numbers. The following exemplifies what needs to be done:
>>>function(‘>>.<>>...’, 4)
‘XX.X>>...’
>>>function(‘>..>..>’, 6)
‘X..X..>’
>>>function(‘..>>>.’, 2)
‘..>>>.’
Please help :)
Hi it seems to me (if you want to avoid using libraries) that you can iterate over the characters in the string and do comparisons to decide if the character needs to be changed. Here is some sample code which should help you.
ARROWS = '<>^v'
def replace_up_to(in_str, n):
# store the new character in this list
result = []
for i, char in enumerate(in_str):
# decide if we need to change the char
if i < n and char in ARROWS:
result.append("X")
continue
result.append(char)
# return a new string from our result list
return "".join(result)