I am an OpenEdge (aka Progress) developer that loves clean code and good looking applications that are easy to use. My main pet project is the Progress DataDigger
FUNCTION cleanString RETURNS CHARACTER
(pcString AS CHARACTER):
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
DO i = 1 TO LENGTH(pcString):
CASE SUBSTRING(pcString,i,1):
WHEN '#' THEN IF c > '' THEN c = SUBSTRING(c, 1, LENGTH(c) - 1).
OTHERWISE c = c + SUBSTRING(pcString,i,1).
END CASE.
END.
RETURN c.
END FUNCTION.
MESSAGE cleanString("abc#def##ghi###") = "abd"
SKIP cleanString("abc#d##c") = "ac"
SKIP cleanString("abc##d######") = ""
SKIP cleanString("#######") = ""
SKIP cleanString("") = ""
VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
Here is a ugly Python one-liner using lambda.
Use Python 3.8
cleanString=lambdas,countPound=0:''.join(filter(lambdachar:notisinstance(char,int),[(countPound:=countPound+1)ifc=='#'elsecifcountPound==0else(countPound:=countPound-1)forcins[::-1]]))[::-1]print(cleanString("Thee# Empires# SS#tateBuildingg#"))# The Empire StateBuilding
print(cleanString("abc#d##c"))# ac
print(cleanString("abc#def##ghi###"))# abd
print(cleanString("abc##d######"))# empty string
print(cleanString("#######"))# empty string
print(cleanString("abc#de##c"))# abc
print(cleanString("abc####dhh##c#"))# d
print(cleanString("abcdef"))# abcdef
print(cleanString("########c"))# c
/**
* Remove backspaces from a string
* @param {String} string
* @throws {Error} If the function gets called with more or less than one argument
* @throws {TypeError} If the function gets called with a non-string argument
* @return {String}
*/functioncleanString(string){if(arguments.length!==1){thrownewError("Expected exactly one argument.");}if(typeofstring!=="string"){thrownewTypeError("Expected first argument to be a string.");}constcharacters=[];for(constcharacterofstring){// If this is a characterif(character!=="#"){characters.push(character);continue;}// If this is a backspace character// And that there is at least one character if(characters.length){characters.pop();}}returncharacters.join("");}
stringcleanString(strings){stringans="";// the answer stringintlength=0;// length of answer stringfor(charc:s){if(c=='#'){// if c == '#' and length > 0, then pop one character and decrement lengthif(length>0){ans.pop_back();length--;}}// else add the character to string and increment lengthelse{ans+=c;length++;}}returnans;}
A long long time ago, I started with Python 1.5 at web.de, founded the MoinMoin wiki project shortly thereafter, and am now enjoying the increased traction that Python recently gets from data science.
Python – No doc strings etc., it's a challenge after all…
importredefclean_text(text,_back_hash=re.compile(r'[^#]#')):changes='#'intextwhilechanges:text,changes=_back_hash.subn('',text)returntext.replace('#','')deftests():data=(("abc#def##ghi###","abd"),("abc#d##c","ac"),("abc##d######",""),("#######",""),("",""),)forinp,outindata:assertclean_text(inp)==out, \
f"{inp!r} cleaned is not {out!r} but {clean_text(inp)!r}"print('All tests OK!')if__name__=='__main__':forxin('abc#de##c','abc####dhh##c#','Thee# Empires# SS#tateBuildingg#'):print(f"{x!r} ⇒ {clean_text(x)!r}")tests()
Rust:
Too easy...
Maybe
match ch:vWhy?
Just to show off
Scala
JavaScript with regex:
Try with
Thanks. Fixt.
Progress 4GL
Here is a ugly Python one-liner using
lambda.Use Python 3.8
Idea is based on
JavaScript
Hope this is right, C++
Python – No doc strings etc., it's a challenge after all…
This is always a good idea to comment your code. Beginners trying to solve these challenges can learn a lot from veterans like you!
Haskell:
PHP:
A quick Clojure solution, leveraging a (linked) list as a stack:
Another JS solution
TypeScript