Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete char after -> without using a regular expression

Tags:

python

Given a string s representing characters typed into an editor, with "->" representing a delete, return the current state of the editor. For every one "->" it should delete one char. If there are two "->" i.e "->->" it should delete 2 char post the symbol.

Example 1

Input
s = "a->bcz"
Output
"acz"

Explanation

The "b" got deleted by the delete.

Example 2

Input 
s = "->x->z"
Output
empty string

Explanation

All characters are deleted. Also note you can type delete when the editor is empty as well. """ I Have tried following function but id didnt work

def delete_forward(text):
"""
return the current state of the editor after deletion of characters 
"""
f = "->"
for i in text:
    if (i==f):
        del(text[i+1])

How can i complete this without using regular expressions?

like image 679
venkat0107 Avatar asked Oct 20 '20 14:10

venkat0107


People also ask

How do I remove a specific character from a regular expression?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

How do I remove all characters before a character in Java?

s1. trim() . trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.)


2 Answers

Strings do not support item deletion. You have to create a new string.

>>> astring = 'abc->def'
>>> astring.index('->')  # Look at the index of the target string
3
>>> x=3
>>> astring[x:x+3]  # Here is the slice you want to remove
'->d'
>>> astring[0:x] + astring[x+3:]  # Here is a copy of the string before and after, but not including the slice
'abcef'

This only handles one '->' per string, but you can iterate on it.

like image 168
kojiro Avatar answered Oct 08 '22 01:10

kojiro


Here's a simple recursive solution-

# Constant storing the length of the arrow
ARROW_LEN = len('->')

def delete_forward(s: str):
    try:
        first_occurence = s.index('->')
    except ValueError:
        # No more arrows in string
        return s
    if s[first_occurence + ARROW_LEN:first_occurence + ARROW_LEN + ARROW_LEN] == '->':
        # Don't delete part of the next arrow
        next_s = s[first_occurence + ARROW_LEN:]
    else:
        # Delete the character immediately following the arrow
        next_s = s[first_occurence + ARROW_LEN + 1:]
    return delete_forward(s[:first_occurence] + s[first_occurence + ARROW_LEN + 1:])

Remember, python strings are immutable so you should instead rely on string slicing to create new strings as you go.

In each recursion step, the first index of -> is located and everything before this is extracted out. Then, check if there's another -> immediately following the current location - if there is, don't delete the next character and call delete_forward with everything after the first occurrence. If what is immediately followed is not an arrow, delete the immediately next character after the current arrow, and feed it into delete_forward.

This will turn x->zb into xb.

The base case for the recursion is when .index finds no matches, in which case the result string is returned.

Output

>>> delete_forward('ab->cz')        
'abz'
>>> delete_forward('abcz')   
'abcz'
>>> delete_forward('->abc->z')
'bc'
>>> delete_forward('abc->z->')   
'abc'
>>> delete_forward('a-->b>x-->c>de->f->->g->->->->->')
'a->x->de'
like image 35
Chase Avatar answered Oct 08 '22 00:10

Chase