| Bytes | Lang | Time | Link |
|---|---|---|---|
| 079 | Swift 6 | 240305T003913Z | macOSist |
| 009 | Vyxal | 240304T225015Z | emanresu |
| 008 | Japt | 151227T013250Z | ETHprodu |
| 014 | V vim | 210409T014613Z | Razetime |
| 081 | brainfuck | 210412T054602Z | RezNesX |
| 016 | K ngn/k | 210409T155547Z | coltim |
| 035 | Vim | 210408T232843Z | user |
| 012 | Vim | 210408T235621Z | Leo |
| 009 | Jelly | 210408T215549Z | caird co |
| 018 | APL Dyalog Unicode | 180603T183557Z | Erik the |
| 020 | Ruby | 150711T093845Z | manatwor |
| 007 | TeaScript | 151117T045259Z | Downgoat |
| 172 | Oracle SQL 11.2 | 160209T200354Z | Jeto |
| 037 | jq 1.5 | 150714T193743Z | manatwor |
| 810 | πΌππππ | 151229T223744Z | Mama Fun |
| 073 | Emacs Lisp | 151225T164617Z | Lord Yuu |
| 039 | Javascript ES6 | 151216T223634Z | Qwertiy |
| nan | 151217T112212Z | alexcat3 | |
| 047 | Haskell | 150712T003616Z | nimi |
| 036 | ><> | 151117T144044Z | Aaron |
| 050 | bash | 151117T215444Z | F. Hauri |
| 060 | JavaScript | 151117T192634Z | Amechra |
| 008 | rs | 150723T023317Z | kirbyfan |
| 138 | Batch | 150713T064741Z | unclemea |
| 039 | Julia | 150715T083753Z | Glen O |
| 123 | Java | 150710T225000Z | Stretch |
| 080 | VisualFoxPro any version | 150715T153520Z | Chris |
| 041 | Julia | 150711T000542Z | Alex A. |
| 084 | Mumps | 150714T191734Z | zmerch |
| 077 | Java | 150712T235939Z | Olivia T |
| 047 | Haskell | 150711T000523Z | lynn |
| 057 | ECMAScript 6 | 150711T003123Z | Downgoat |
| 052 | R | 150711T034117Z | Alex A. |
| 006 | Gema | 150711T102328Z | manatwor |
| 010 | REGXY | 150712T161112Z | Jarmex |
| 015 | Perl | 150711T094244Z | manatwor |
| 062 | Javascript | 150711T161741Z | Ismael M |
| 039 | JavaScript ES6 | 150711T074845Z | edc65 |
| 036 | golflua | 150711T160636Z | manatwor |
| 064 | K5 | 150711T160212Z | kirbyfan |
| 013 | CJam | 150710T221720Z | Optimize |
| 013 | Retina | 150710T215707Z | randomra |
| 050 | Python 2 | 150711T090758Z | feersum |
| 053 | Python 3 | 150711T073913Z | Sp3000 |
| nan | Python 2 | 150711T013058Z | Kade |
| 052 | C | 150710T235248Z | BrainSte |
| 011 | GNU sed | 150710T232938Z | Digital |
| 011 | Pyth | 150710T222450Z | izzyg |
| 019 | Pyth | 150710T215539Z | Maltysen |
Swift 6, 81 79 bytes
let f={(s:inout _)in while(s+"").contains("^"){s={s.replacing}()(/.\^H/,"",1)}}
Call it like this:
var s = "Horse^H^H^H^H^HCow"
f(&s)
print(s) // "Cow"
Vyxal, 9 bytes
β^H/ΖΞ»$αΉͺp
Try it Online! Port of caird coinheringaahing's Jelly answer.
/ # Split on
β^H # "^H"
ΖΞ»--- # Reduce by
p # Prepend to the first item
$αΉͺ # The second with its last item removed
Japt, 8 bytes
Ue/.\^H/
How it works
U // Take the input,
e/.\^H/ // and recursively replace any char followed by "^H" with
// (nothing, defaults to an empty string).
// Implicit: output last expression
brainfuck, 81 bytes
>,[[>+>+<<-]-[>-<---]>---------[>>+<<[-]]>>-[+<[-]<<<,[-]>>]<[<<+>>-]<,]<[<]>[.>]
K (ngn/k), 23 16 bytes
-7 bytes by rereading question (specifically, "^ will only appear as ^H")
{x_/3#-1+x?"^"}/
Removes one instance of ?^H on each invocation, and is run until convergence (i.e. no more ^'s are present).
{...}/run function on (implicit) input until it converges-1+x?"^"get the index of the character immediately prior to the first^inxx_/3#drop the value occurring at that index three times
Vim, 38 35 bytes
Saved 3 bytes thanks to Leo!
qa:%s/\([^H]\|\^\@<!.\)\^H//g
@aq@a
See Razetime's answer for a shorter and smarter version of this.
qa:%s/\([^H]\|\^\@<!.\)\^H//g
@aq@a
qa Start recording a macro a
:%s/ Substitute in entire file
\([^H]\|\^\@<!H\) Regex for the character to be deleted
[^H] A character that isn't H
\| or
H an H
\^\@<! that doesn't have a ^ before it
\^H All of that followed by ^H
// Replace with empty string
g Global flag so it replaces multiple times
Enter the command
@a Recursively call macro a
q Stop recording
@a Run the macro
```
Vim, 12 bytes
qqt^3xh@qq@q
Makes use of the fact that "^ will only appear as ^H".
Explanation
qqt^3xh@qq@q
qq @qq@q Define an indefinitely looping recursive macro and immediately call it
t^ Move to just before the next ^ character in the line
3x Delete 3 characters (the one to be deleted, ^, and H)
h Move left 1 step (to be in position for the next search)
Jelly, 9 bytes
ΕαΉ£βΎ^HαΉ;Β₯/
Not a new approach, but the question was missing a Jelly answer
How it works
ΕαΉ£βΎ^HαΉ;Β₯/ - Main link. Takes a string S on the left
βΎ^H - Yield "^H"
ΕαΉ£ - Split on "^H"
Β₯/ - Reduce by the following:
αΉ - Remove the last character
; - And append the right argument
Ruby, 27 24 20 bytes
(19 characters code + 1 character command line option.)
$_=$`+$'while/.\^H/
Thanks to:
- Ventero for suggesting to use the global variables (-4 characters)
Sample run:
bash-4.3$ ruby -pe '$_=$`+$'"'"'while/.\^H/' <<< "Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."
Be nice to this gentleman, he's visiting from corporate HQ.
TeaScript, 7 bytes [Not competing]
Not competing as TeaScript was made after this challenge was posted. This is here as reference.
xW/.\^H
This uses the new TeaScript 3, and recursive replaces to remove the characters
Oracle SQL 11.2, 172 bytes
WITH v(s,i)AS(SELECT:1,INSTR(:1,'^')FROM DUAL UNION ALL SELECT LPAD(s,i-2)||SUBSTR(s,i+2),INSTR(s,'^',1,2)-3 FROM v WHERE i>0AND'^'<>s)SELECT s FROM v WHERE INSTR(s,'^')=0;
Un-golfed
WITH v(s,i) AS -- Recursive view, s-> string, i->pos of first ^
(
SELECT :1,INSTR(:1,'^') -- Initialisation view
FROM DUAL
UNION ALL
SELECT LPAD(s,i-2)||SUBSTR(s,i+2), -- Remove the ^ at pos i and the characters before and after
INSTR(s,'^',1,2)-3 -- Compute the pos of the next ^ (the 2nd of s as before the remove just above)
FROM v
WHERE i>0 -- Exit clause : no more ^
AND s<>'^' -- Needed to circumvent oracle's cycle detection, without it 123^H45^H^H^H78^H will fail
)
SELECT s FROM v WHERE INSTR(s,'^')=0; -- Keep only the row without any ^
jq 1.5, 41 37 bytes
(34 characters code + 3 characters command line option.)
reduce(./"^H")[]as$t("";.[:-1]+$t)
Sample run:
bash-4.3$ bin/jq -R -r 'reduce(./"^H")[]as$t("";.[:-1]+$t)' <<< "Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."
Be nice to this gentleman, he's visiting from corporate HQ.
On-line test (Passing -R through URL is not supported β so input passed as JSON string literal. Passing -r through URL is not supported β check Raw Output yourself.)
πΌππππ, 8 chars / 10 bytes (noncompetitive)
Γ―Δ/.\^H/
Works like all of the other JSGL's.
Emacs Lisp, 45 73 bytes
(lambda(s)(while(string-match".^H"s)(set's(replace-match"" nil nil s)))s)
Searches for the first occurence of anything else and ^H as long as it exists and replaces it with an empty string.
Old, incorrect version
(lambda(s)(replace-regexp-in-string".^H"""s))
Javascript ES6, 41 39 bytes
f=s=>s==(s=s.replace(/.\^H/,""))?s:f(s)
JavaScript, 60 Bytes
I know there is already a JS answer here, but wanted to do it without regexes, because regex is really almost a language by itself. I'm sorry if you feel I shouldn't post this, this is my first post.X is the string to be operated upon.
while((z=x.indexOf('^D'))>0){x=x.slice(0,z-1)+x.slice(z+2);}
Test
Put this into your browser's address bar.
javascript:x=prompt('Enter the sentence to be erased');while((z=x.indexOf('^D'))>0){x=x.slice(0,z-1)+x.slice(z+2);}alert(x);
I love JS because it will let you do things like saying (z=x.indexOf('^D))>0, and it will both assign z.indexOf('^D'); to x and evaluate z.indexOf('^D') in the condition, which saves me precious bytes in this problem. It's not very useful otherwise, but it's fun!
Haskell, 52 47 bytes
import Data.Lists
foldl1((++).init).splitOn"^H"
Usage example:
> map (foldl1((++).init).splitOn"^H") ["Horse^H^H^H^H^HCow", "123^H45^H^H^H78^H", "Digital Trauma^H^H^H^H^H^H^H^H^H^H^H^H^H^HMaria Tidal Tug^H^H^H^H^H^H^H^H^H^H^H^H^H^H^HDigital Trauma"]
["Cow","17","Digital Trauma"]
How it works:
splitOn"^H" -- split on substring "^H", e.g "Horse^H^H^H^H^HCow" -> ["Horse","","","","","Cow"]
. -- then
foldl1( ) -- fold from left by
init -- first dropping the last char from the left argument
(++). -- second concatenating left and right argument
bash, 50 bytes
while [ "$a" != "${a/?^H/}" ];do a=${a/?^H/};done
Sample:
a=$'Horse^H^H^H^H^HCow'
while [ "$a" != "${a/?^H/}" ];do a=${a/?^H/};done
echo $a
Cow
a="Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."
while [ "$a" != "${a/?^H/}" ];do a=${a/?^H/};done
echo $a
Be nice to this gentleman, he's visiting from corporate HQ.
This could work with real (binaries) backspaces as well:
a=$'Be nice to this fool\b\b\b\bgentleman, he'\'$'s visiting from corporate HQ.'
echo $a.. but:
Be nice to this gentleman, he's visiting from corporate HQ... but:
printf %q\\n "$a"
$'Be nice to this fool\b\b\b\bgentleman, he\'s visiting from corporate HQ.'
while [ "$a" != "${a/?$'\b'/}" ];do a=${a/?$'\b'/};done
printf %q\\n "$a"
Be\ nice\ to\ this\ gentleman\,\ he\'s\ visiting\ from\ corporate\ HQ.
JavaScript, 60 bytes
b=n=>(v=n.search('^H'))>0?b(n.replace(n.slice(v-1,v+2),'')):n
Not as good as edc65's answer, but I wanted to try something that didn't use Regex.
rs, 8 bytes
Technically, this doesn't count, as it depends on a feature I added after this question was posted. However, I think it's pretty neat.
+?1.\^H/
Batch - 138 bytes
@!! 2>nul||cmd/q/v/c%0 %1&&exit/b
set s=%1&for /F %%a in ('"prompt $H&echo on&for %%b in (1)do rem"')do set D=%%a
echo %s:^H=!D! !D!%
The first line is a way of saving a few bytes over the lengthy @echo off&setLocal enableDelayedExpansion (which turns echo off and enables the delayed expansion of variables, in case you were wondering). I explained it in Tips for Golfing in Batch.
The second line is a neat little trick to save the a backspace control character into a variable. It's pretty hacky, and I can't pretend to take credit for it. It's sort of explained here. Basically uses the prompt command to generate a backspace character and captures it in a variable - in this case !D!.
The final line then performs the simple string manipulation of - replace ^H with !D!<SPACE>!D!.
C:\>bsp.bat "testing^H^H^H test"
"test test"
Unfortunately it breaks with cases like "AAA^HB^H^H" - where it should produce "A", it instead produces "A"B. Which is somewhat confusing. I'll have to look into how Batch string manipulation works in some more depth.
C:\>bsp.bat "AAA^HB^H^H"
"A"B
Thanks to to some helpful people over here - I now realize that I was only saving the backspace character (0x08), and so was only overwriting the characters. It now works with examples like the following:
C:\>bsp.bat "AAA^HB^H^H"
"A"
Julia, 41 39 bytes
s->foldl((t,v)->chop(t)v,split(s,"^H"))
What it's doing is using ^H as a delimiter, and then removing the last character on each string then concatenating the next string before removing the last character again. Unlike the other Julia answer, this is not a recursive function.
Note: I've removed the function name from the definition. Originally, it said f(s)= rather than s->, and you used it as f("AAA^HB^H^H")... but I'm saving two bytes by letting it be "anonymous", and use itself as its name. You use it like this:
(s->foldl((t,v)->chop(t)v,split(s,"^H")))("AAA^HB^H^H")
(you can also assign a variable to it as f=s->foldl((t,v)->chop(t)v,split(s,"^H")), then f("AAA^HB^H^H") will work)
Java - 123 bytes
I personally like the g---1 part the best.
String f(char[] a){String b="";for(int g=-1;++g<a.length;b=(a[g++]=='^'?b.substring(0,b.length()-1):b+a[g---1]));return b;}
expanded (slightly):
String f(char[] a) {
String b = "";
for (int g = -1;
++g < a.length;
b = (a[g++]=='^'
? b.substring(0, b.length() - 1)
: b + a[g---1])
);
return b;
}
(Visual)FoxPro any version 80 bytes
PARA t
DO WHILE AT('^H',t)>0
t = STRT(t,SUBS(t,AT('^H',t)-1,3))
ENDDO
RETU t
Repeating string translation to empty by finding ^H and backing up one character.
Julia, 58 42 41 bytes
Saved 16 bytes thanks to manatwork and 1 thanks to Glen O!
f(s)='^'βs?f(replace(s,r".\^H","",1)):s
This creates a recursive function that accepts a string and returns a string.
This replaces one occurrence of ^H at a time with an empty string while the input contains ^.
Examples:
julia> f("123^H45^H^H^H78^H")
"17"
julia> f("pizza is alright^H^H^H^H^H^Hwesome")
"pizza is awesome"
Mumps, 84 Bytes
R Z S T="",Y=$L(Z,"^H") F I=1:1:Y{S T=T_$P(Z,"^H",I) S:I<Y T=$E(T,1,$L(T)-1)} W !,T
This could probably be made shorter as a function (1 byte I was able to save in quick testing) but I kinda like the one-liner aspect... :-)
The braces come from the Intersystems Cache flavour of Mumps which is what I'm most versed in.
Java, 78 77 bytes
String f(String a){while(!a.equals(a=a.replaceFirst(".\\^H","")));return a;}
Haskell, 47 bytes
h(a,_:_:b)=f$init a++b;h(x,_)=x
f=h.span(/='^')
Defines a function f :: String -> String. How it works:
f "ab^Hc^Hd"
=== h ("ab", "^Hc^Hd") (find ^H)
=== f ("a" ++ "c^Hd") (backspace)
=== f "ac^Hd" (join)
=== h ("ac", "^Hd") (find ^H)
=== f ("a", "d") (backspace)
=== f "ad" (join)
=== h ("ad", "") (find ^H)
=== "ad" (no ^H: base case)
ECMAScript 6, 57 bytes
s=>{while(~s.indexOf`^H`)s=s.replace(/.\^H/,'');return s}
This is probably golfable, just gotta think of a way probably not
R, 54 52 bytes
f=function(s)ifelse(s==(r=sub(".\\^H","",s)),r,f(r))
Same basic idea as my Julia answer. This creates a recursive function that accepts a string and returns a string. If the input is equal to itself with a single occurrence of ^H removed, return it, otherwise call the function again.
You can try it online!
Gema, 6 bytes
?#\^H=
Sample run:
bash-4.3$ gema -p '?#\^H=' <<< 'pizza is alright^H^H^H^H^H^Hwesome'
pizza is awesome
CW, because the fool vs. gentleman example takes far too long. (Killed after a day. Maybe a glitch in the interpreter? All other examples here are processed in fractions of seconds.) Gema's recursive pattern not seems to be affected by the recursion level, but the amount of non-matching text increases processing time exponentially.
REGXY, 10 bytes
Uses REGXY, a regex substitution based language. Replaces any character followed by ^H with nothing. The second line then executes which is just a pointer to the previous line, repeating the substitution until it fails to match.
/.\^H//
//
This compiles and executes correctly with the sample interpreter in the link above, but the solution is perhaps a bit cheeky as it relies on an assumption in the vagueness of the language specification. The spec states that the first token on each line (before the /) acts as a label, but the assumption is that a null label-pointer will point back to the first command in the file with a null label (or in other words, that 'null' is a valid label). A less cheeky solution would be:
a/.\^H//
b//a
Which amounts to 13 bytes.
Perl, 20 16 15 bytes
(14 characters code + 1 character command line option.)
s/.\^H//&&redo
Sample run:
bash-4.3$ perl -pe 's/.\^H//&&redo' <<< "Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."
Be nice to this gentleman, he's visiting from corporate HQ.
Javascript, 62 bytes
Not the shortest one, but works fine.
t=prompt();while(t.match(R=/.\^H/))t=t.replace(R,'');alert(t);
This probably can be shortened a lot!
JavaScript (ES6), 39 bytes
f=s=>(t=s.replace(/.\^H/,''))!=s?f(t):t
// TEST
Out=x=>O.innerHTML+=x+'\n'
Test=_=>(Out(I.value + "\n-> " + f(I.value)),I.value='')
;["Horse^H^H^H^H^HCow"
,"Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."
,"123^H45^H^H^H78^H"
,"Digital Trauma^H^H^H^H^H^H^H^H^H^H^H^H^H^HMaria Tidal Tug^H^H^H^H^H^H^H^H^H^H^H^H^H^H^HDigital Trauma"]
.forEach(t => Out(t + "\n-> " + f(t)))
#I { width:400px }
<pre id=O></pre>
<input id=I><button onclick='Test()'>-></button>
golflua, 36 bytes
\f(s)@o!=s o=s;s=s:g(".^H","",1)$~s$
Sample run:
Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio
> \f(s)@o!=s o=s;s=s:g(".^H","",1)$~s$
> w(f("Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ."))
Be nice to this gentleman, he's visiting from corporate HQ.
K5, 64 bytes
K isn't really designed for this kind of work...
{[s]$[2>#s;s;`=t:*&{"^H"~2#x_s}'1+!-2+#s;s;,/2#2!|(0,t,3+t)_s]}/
CJam, 14 13 bytes
q"^H"/{W\ts}*
How it works
q e# Read the entire input
"^H"/ e# Split it on occurrences of string "^H"
{ }* e# Reduce on the split array
W\t e# This is the tricky part. We know that if there are two parts that we
e# are reducing on, they must be separated by "^H". Which in turn means
e# that from the first part, last characters needs to be deleted
e# So we simply put the second part in place of the last character of the
e# first part.
s e# Doing the above makes it a mixed array of character and string.
e# So we convert it to a single string, ready to be served as first part
e# in next reduce iteration
UPDATE: 1 byte saved thanks to jimmy23013
Retina, 13 bytes
+`.\^H(.*)
$1
The two lines should go to their own files but you can run the code as one file with the -s flag.
At each step we delete the first match for .\^H in the string. We repeat this (with the + modifier) until no deletion happens.
Python 2, 50
It's a bit odd having a second lambda in there, but seems to be the best Python so far.
lambda s:reduce(lambda a,b:a[:-1]+b,s.split('^H'))
Python 3, 53 bytes
o=""
for x in input().split("^H"):o=o[:-1]+x
print(o)
But personally I like this wordier version better:
H=input().split("^H")
print(eval("("*~-len(H)+")[:-1]+".join(map(repr,H))))
The interesting thing is that
'B''a''c''k''h''a''n''d''e''d'[:-1][:-1][:-1][:-1][:-1][:-1]
actually works and gives 'Back', so I tried to map ^H -> [:-1] and any other char c -> 'c' then eval, but unfortunately you can't have any strings afterwards without a +, so this fails:
'B''a''c''k''h''a''n''d''e''d'[:-1][:-1][:-1][:-1][:-1][:-1]'s''p''a''c''e''s'
Python 2, 74 + 2 = 76 Bytes
I've tried a few approaches so far, this is the best I've been able to come up with so far.
n=input();o='';c=0
for l in n:d=l=='^';o=[o+l*(1-c),o[:-1]][d];c=d
print o
C, 52 bytes
j;f(char*s){for(j=0;*s=s[j];s[j]==94?s--,j+=3:s++);}
We define a function f that takes a pointer to the string as input. After the function call, that pointer will contain a modified string.
A simple test:
int main(int argc, char** argv) {
char buf[300] = "Digital Trauma^H^H^H^H^H^H^H^H^H^H^H^H^H^HMaria Tidal Tug^H^H^H^H^H^H^H^H^H^H^H^H^H^H^HDigital Trauma";
f(buf);
printf(buf);
return 0;
}
The above prints:
Digital Trauma
GNU sed, 11 bytes
:;s/.^H//;t
Test output:
$ echo "Horse^H^H^H^H^HCow
Be nice to this fool^H^H^H^Hgentleman, he's visiting from corporate HQ.
123^H45^H^H^H78^H
Digital Trauma^H^H^H^H^H^H^H^H^H^H^H^H^H^HMaria Tidal Tug^H^H^H^H^H^H^H^H^H^H^H^H^H^H^HDigital Trauma" | sed ':;s/.^H//;t'
Cow
Be nice to this gentleman, he's visiting from corporate HQ.
17
Digital Trauma
$
Pyth, 11 bytes
.U+PbZcz"^H
.U+PbZcz"^H
Implicit: z = input()
cz"^H z.split("^H")
.U reduce, with the first element of the list as the initial value.
Pb Remove the last character of what we have so far.
+ Z And add on the next segment.
Print implicitly.
Pyth - 19 bytes
Reduce works really, really well with this but it only does one char at a time so I had to spend almost as many chars as the actual algo to do a replace ^H with linebreak. Looking for a better way to do that.
u?+GHnHbPGjbcz"^H"k