| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | R | 240621T175809Z | int 21h |
| 070 | C gcc | 230704T182735Z | l4m2 |
| 9625 | Vyxal | 230625T135748Z | lyxal |
| 016 | Stax | 230630T172727Z | recursiv |
| 053 | Ruby | 230630T093403Z | Siim Lii |
| 063 | JavaScript ES6 | 161120T002856Z | edc65 |
| 108 | Rockstar | 230628T114726Z | Shaggy |
| 094 | Python 3 | 230626T120939Z | spooky_s |
| 018 | Thunno 2 | 230624T174421Z | The Thon |
| 012 | Vyxal | 221017T102716Z | DialFros |
| 012 | V | 170214T074422Z | DJMcMayh |
| 016 | MATL | 161120T034051Z | Luis Men |
| 271 | Assembly MIPS | 190905T130114Z | Andrew B |
| 099 | Python 3 108 | 190905T055234Z | David |
| 079 | PowerShell | 180523T010752Z | Veskah |
| 195 | Pascal FPC | 180823T161343Z | AlexRace |
| 016 | Japt R | 180521T052617Z | Bubbler |
| 124 | Java 10 | 161121T153351Z | Kevin Cr |
| 010 | Stax | 180521T185527Z | recursiv |
| 135 | Python 3 | 180521T163637Z | Makefile |
| 102 | Yabasic | 180521T150927Z | Taylor R |
| 072 | Python 3 | 180521T094822Z | Guoyang |
| 052 | Excel | 170428T191245Z | Greedo |
| 093 | Python 3 | 161123T084354Z | AvahW |
| 084 | PHP | 170418T142900Z | Jör |
| 123 | Clojure | 170217T182304Z | Carcigen |
| 082 | Bash + Unix utilities | 170214T084902Z | Mitchell |
| 065 | SmileBASIC | 170214T204541Z | 12Me21 |
| 025 | Vim | 161119T195326Z | DJMcMayh |
| 082 | Scala | 161123T100150Z | sprague4 |
| 017 | Pyke | 161121T173615Z | Blue |
| 147 | Swift 3 | 161202T021958Z | rabidaud |
| 091 | C# | 161201T225313Z | adrianmp |
| 091 | JavaScript | 161121T201924Z | Oliver |
| 079 | C | 161121T131023Z | user5634 |
| 050 | Haskell | 161119T212711Z | dianne |
| 142 | Python 3 | 161120T153712Z | Vedvart1 |
| 062 | Ruby | 161121T050525Z | Ben Hili |
| 194 | Racket | 161122T173808Z | rnso |
| 273 | Python 2 73 Bytes | 161122T155641Z | Lulhum |
| 104 | Java 8 | 161122T155926Z | Xanderha |
| 137 | C# | 161121T085423Z | paldir |
| nan | sed | 161122T002004Z | Ray |
| 127 | TSQL | 161121T224155Z | dfundako |
| 023 | 05AB1E | 161121T152238Z | Oliver N |
| 074 | JavaScript ES6 | 161120T013033Z | Neil |
| 112 | C# | 161121T133620Z | psycho |
| 034 | Groovy | 161121T151114Z | Magic Oc |
| 078 | PHP | 161119T201629Z | Titus |
| 029 | Perl 5 | 161119T205924Z | msh210 |
| 125 | c90 | 161120T181155Z | Bobby Sa |
| 066 | Python 2 | 161119T200645Z | R. Kap |
| 064 | Mathematica | 161120T024932Z | LegionMa |
| 086 | JavaScript ES2015 | 161119T233336Z | Sethi |
| 089 | JavaScript ES6 | 161119T232537Z | Bassdrop |
| 2625 | 05AB1E | 161119T223136Z | Osable |
| 022 | Pip | 161119T214910Z | DLosc |
| 083 | Python 3 | 161119T214603Z | dvorkant |
| 027 | Dyalog APL | 161119T214614Z | ngn |
| 038 | Perl 6 | 161119T203723Z | Brad Gil |
| 027 | Retina | 161119T194541Z | Martin E |
| 078 | Mathematica | 161119T194707Z | Greg Mar |
R, 91 bytes
\(s,p,`+`=utf8ToInt,w=which(+s!=+p)){for(j in w[1!=w])s=c(s,intToUtf8((+s)[F<-c(F,-j)]));s}
This solution outputs a vector/array of strings. Although removal of the 2nd character, not equal to the pattern argument from the string
can be done with sub, I could not construct a short enough regex that would result in a solution of less than 100 bytes (it was 116 bytes).
Instead, I have applied the classic scheme, which has often proved to give the golfiest solutions: string -> charcodes -> string.
C (gcc), 70 bytes
f(x,v,i)char*x,*i;{for(puts(i=x);*++i;)*i-v&&puts(x,strcpy(i--,i+1));}
C (gcc), 65 bytes(32-bit) by c--
f(x,v){char*i;for(puts(i=x);*++i;)*i-v&&puts(x,strcpy(i--,i+1));}
Vyxal, 77 bitsv1, 9.625 bytes
ḣ≬‡⁰≠Ǒ⟇↔Ṡ+
Explained
The very first explanation generated using Luminespire, a currently work-in-progress explanation formatting assistant. Now with metadata!
ḣ≬‡⁰≠Ǒ⟇↔Ṡ+
ḣ # Push first character of input, then all of input minus first character
≬ ↔ # Repeat and collect results of applying the following on the top of the stack until invariant:
‡ Ǒ # Find the first index of:
⁰≠ # a character that isn't the second input
⟇ # Remove that character from the top of the stack
Ṡ # Convert each item to a string via vectorised summation
+ # Prepend the first letter to each
Stax, 16 bytes
Å<4R@Γ↓ α¿εN~;è<
Approach
It uses repeated application of a regex-based replacement with callback. For example, if the target char is e, then it uses the regex .[^e].*. In the callback, it removes the character at position 1. It repeats this until no further replacement is possible.
Ruby, 53 bytes
->(a,c){puts a;puts a while a.sub! /(?!^)[^#{c}]/,''}
Ungolfed
->(a,c) do # Define a lambda with 2 arguments, the string and the character
puts a # output the string unchanged
while( # Start a loop
# continue until this replacement did not replace anything
a.sub!( # replace in place
# the first occurrence of the character in the string
# other than the beginning of the string
/(?!^)[^#{c}]/,
# with nothing
''
)
) do
puts a # if replaced something, output the remaining string
end
end
The double puts a seems redundant and annoys me, but couldn't figure out a way to improve it.
JavaScript (ES6), 63 64 69
Thanks to @Shaggy for shaving 1 byte!
Returning a single string with newlines
s=>c=>[...s].map((x,i,z)=>i&&x!=c&&(s+=`
`+z.join(z[i]='')))&&s
F=
s=>c=>[...s].map((x,i,z)=>i&&x!=c&&(s+=`
`+z.join(z[i]='')))&&s
function update() {
var s=S.value,c=C.value[0]
O.textContent=F(s)(c)
}
update()
<input id=S value='Hello world!' oninput='update()'>
<input id=C value='!' oninput='update()'>
<pre id=O></pre>
Rockstar, 108 bytes
Takes the string as the first line of input and the character as the second.
listen to S
listen to K
say S
cut S
roll S in F
while S
roll S in N
if N's K
let F be+N
join S in O
say F+O
Try it (Code will need to be pasted in)
Python 3, 94 bytes
def f(n,s,d=1):
print(n)
if set(n[1:])-{s}:
if n[d]==s:d+=1
n=n[:d]+n[d+1:];f(n,s,d)
Happy to have broken 100 bytes but I'm sure it could be golfed down.
Thunno 2, 18 bytes
Ḣȥ(ȤJ¢£x;ḢYẊy¹=?yȥ
Explanation
Ḣȥ(ȤJ¢£x;ḢYẊy¹=?yȥ # Implicit input
Ḣȥ # Extract the first character, and add it to the global array
( ; # While loop
ȤJ # (condition) Join the global array
¢ # Print it with no newline
£ # Print top of stack with a newline
x # Push x (initialised to 1)
Ḣ # (body) Extract the first character
Y # Pop and store in y
Ẋ # Without popping, store the rest in x
y # Push y again
¹= # Is it equal to the second input?
? # If it is:
yȥ # Add y to the global array
Vyxal, 12 bytes
$o(…:⁰v≠TḢh⋎
Explained
$o(…:⁰v≠TḢh⋎
$ # swap top 2 items
o # remove
( # loop
… # print without popping
: # duplicate
⁰ # last input
v # vectorise
≠ # not equal
T # truthy indices
Ḣ # remove head [0]
h # head
⋎ # bitwise or
V, 12 bytes
òYpó.a]òd
Hexdump:
00000000: f259 70f3 2e93 8412 615d f264 .Yp.....a].d
I have tested this with the latest version of V available before the challenge, and everything runs correctly, making this answer competing.
Explanation:
ò ò " Recursively:
Yp " Duplicate this line
ó " Remove:
.a] " A compressed regex
d " Delete our extra line
The compressed regex translates to
.\zs[^e]
Which means
. " Any character
\zs " Leave the previous character out of the selection
[^e] " Any character except for 'e' (Or whatever is given for input)
This version uses a shortcut for Yp that wasn't available when this challenge was posted.
MATL, 20 16 bytes
y-f1X-"t[]@X@q-(
Try it online! Or verify test cases: 1, 2, 3, 4, 5.
Bonus
Modified code to see the string being gradually shrunk (offline compiler):
y-f1X-"tt.3Y.XxD[]@X@q-(].3Y.XxDvx
Or try it at MATL Online!
Explanation
y % Implicitly input string and char. Duplicate string onto top
- % Subtract. Gives nonzero for chars in the input string that are
% different from the input char
f % Array of indices of nonzero values
1X- % Remove 1 from that array. This gives an array of the indices of
% chars to be removed from the string
" % For each
t % Duplicate previous string
[] % Push empty array
@ % Push index of char to be removed. But this index needs to be
% corrected to account for the fact that previous chars have
% already been removed...
X@q- % ... So we correct by subtracting the 0-based iteration index
( % Assign empty array to that position, to remove that char
% Implicitly end for each
% Implicitly display
Assembly (MIPS, SPIM), 271 bytes
.text
.globl main
main:
lw $4 8($5)
lb $10 ($4)
lw $8 4($5)
move $9 $8
i:
move $4 $8
li $2 4
syscall
k:
move $4 $9
addi $9 1
lb $2 ($9)
beq $2 $10 k
beq $2 $0 e
l:
lb $2 ($4)
sb $2 1($4)
addi $4 -1
bge $4 $8 l
li $4 10
li $2 11
syscall
addi $8 1
j i
e:
li $2 10
syscall
I've realized when not doing syscalls, the 'a' and 'v' registers are free, which allowed me to save a few bytes. It's also a huge code smell, but this is code golf, so that's irrelevant. Takes the input via command-line arguments (I could do STDIN equivalent, but that costs more), and outputs to system out.
A detailed version that explains what all this does can be found here
Python 3: 108 99 bytes
def g(s,o):
r,p=s[0],print
for i in range(1,len(s)):
if s[i]==o:r+=s[i]
else:p(r+s[i:])
p(r)
Then invoke the function with:
g('alphabet', 'a')
Output:
alphabet
aphabet
ahabet
aabet
aaet
aat
aa
PowerShell, 94 89 84 79 bytes
param($s,$c)$s
for(;++$y-lt$s.length){if($s[$y]-cne$c){($s=$s.remove($y--,1))}}
Works by just chipping out a letter, starting at the 2nd, if it doesn't match and prints the resulting string; otherwise just increments i and this goes until it falls off. Had to use the case-sensitive -ne because PowerShell is insensitive by default. It takes a string and a char.
Pascal (FPC), 195 bytes
var s:string;c:char;i,j:word;begin readln(s);read(c);i:=2;while i<length(s)+1do begin if c=s[i]then i:=i+1 else begin writeln(s);for j:=i to length(s)do s[j]:=s[j+1];Dec(s[0])end;end;write(s)end.
String is in first line of input, character is in second line of input.
Japt -R, 18 16 bytes
¬ËUoÈ¥Vª!YªY>E
â
Unpacked & How it works
Uq mDEF{UoXYZ{X==V||!Y||Y>E
Uâ
Uq mDEF{ Split input into chars and map...
UoXYZ{ Filter on the original input...
X==V|| Keeping chars same as 2nd input (V),
!Y|| 1st char,
Y>E and indices exceeding outer index
\n Close all braces implicitly and save the result to U
Uâ Take unique elements from U
Turns out that trying to use regex is way too verbose here.
The -R flag joins the resulting array of strings with newline, saving three bytes <space>qR at the end. Also, this code exploits the JS feature of Array.map and Array.filter, where the index is passed as the 2nd argument (E and Y in the code above).
Java 10, 155 140 139 124 bytes
c->s->{var r=s+"\n";for(int i=0;++i<s.length();)if(s.charAt(i)!=c)r+=(s=s.substring(0,i)+s.substring(i--+1))+"\n";return r;}
Explanation:
c->s->{ // Method with character and String parameters and String return-type
var r=s+"\n"; // Result-String, starting at the input-String with trailing new-line
for(int i=0;++i<s.length();)
// Loop over the characters of the String, skipping the first
if(s.charAt(i)!=c)
// If the current character and the input-character are equal
r+=(s=s.substring(0,i)+s.substring(i--+1))
// Remove this character from the String `s`
+"\n"; // And append the new `s` with trailing new-line to the result-String
return r;} // Return the result-String
Old 139 bytes recursive answer:
void c(String s,int c){System.out.println(s);for(int i=1;i<s.length();)if(s.charAt(i++)!=c){c(s.substring(0,i-1)+s.substring(i),c);break;}}
-1 bytes thanks to @Eugene. (Next time make a comment instead of editing someone else's post, please.)
Explanation:
void c(String s,int c){ // Method with String and integer parameters and no return-type
System.out.println(s); // Print the input-String with trailing new-line
for(int i=1;i<s.length();)// Loop over the characters of the String, skipping the first
if(s.charAt(i++)!=c){ // If the current character and the input-character are equal
c(s.substring(0,i-1)+s.substring(i),c);
// Remove this character, and do a recursive call
break;}} // And stop the loop
Stax, 10 bytes
Ä▬ä+QÜ├┌9◘
Unpacked, ungolfed, and commented, it looks like this.
Q print the input string without popping
1:/ split after the first character, and push both parts separately
e.g. "H" "ello World!"
c,- remove instances of the character from the second part
m for each remaining character, run the rest of the program and print the result
|- remove the first instance of the character
b+ copy both strings on the stack and append
Python 3, 135 bytes
(lambda a:print(a[0])or[a.__setitem__(0,a[0][0]+a[0][1:].replace(x,'',1))or print(a[0]) for x in a[0][1:] if x!=a[1]])([input(),input()])
Yabasic, 102 bytes
An Anonymous function that takes input as two strings and outputs to the console.
Input""s$,v$
For i=2To Len(s$)If Mid$(s$,i,1)<>v$Then?s$:s$=Mid$(s$,1,i-1)+Mid$(s$,i+1):i=1Fi
Next
?s$
Python 3, 72 bytes
def e(i,k):
for r in i:
if r!=k:i=i[0]+i[1:].replace(r,'',1);print(i)
e('😋🥕🍎🥓🥑🥓🥑🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆','🥓')
Go on a diet:
😋🥕🍎🥓🥑🥓🥑🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🍎🥓🥑🥓🥑🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥑🥓🥑🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🥑🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🥒🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🍆🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🥔🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🍆🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🥓🍅🥜🥓🥔🍅🍄🍆
😋🥓🥓🥓🥜🥓🥔🍅🍄🍆
😋🥓🥓🥓🥓🥔🍅🍄🍆
😋🥓🥓🥓🥓🍅🍄🍆
😋🥓🥓🥓🥓🍄🍆
😋🥓🥓🥓🥓🍆
😋🥓🥓🥓🥓
Excel, 52 Bytes
=LEFT(A1)&REPT(A2,LEN(A1)-LEN(SUBSTITUTE(A1,A2,"")))
where A1 and A2 are the string and the substitute letter respectively
I realise that may not actually answer the question, the wording is functions may return arrays but I'm guessing I need all the intermediate steps. So I've added
VBA, 142 137 (-4) bytes
Sub x(a, b)
For i = 2 To Len(a)
c = Mid(a, i, 1)
If c = "" Then End 'or d = 1 / Len(c) but then we quit on an error
If b<>c Then a = Replace(a, c, "", , 1): i = 1: Debug.?a
Next
End Sub
Which I think is the right byte count but I'm not sure how extra lines are treated. called in the same way as the Excel version. Returns all the steps in the Immediate Window
Python 3, 94 93 Bytes
b=list(input())
c=input()
i=1
while i<len(b):
while b[i]==c:i+=1
b.pop(i);print(''.join(b))
Thanks to @Challenger5 for pointing out that I had misread the question
PHP, 84 Bytes
a REGEX based answer
for($a=$argv[$c=1];$c;$a=preg_replace("#^(.+)[^$argv[2]]#","$1",$a,1,$c))echo"$a\n";
You can use the second input as REGEX for chars that are not removed
Clojure, 123 bytes
(fn[[f & p]l](loop[[n & m :as r]p a f](println(str a(apply str r)))(if m(recur(if(= n l)(rest m)m)(if(= n l)(str a n)a)))))
Definitely not optimal. I tried getting rid of the two ifs in the recur, but the inclusion of a let made it bigger!
Loops over the string, popping the first char. If it equals the supplied character, it's added to the accumulator, and dropped from the remaining string.
(defn ceee [[first-char & rest-phrase] letter] ; Deconstruct the first letter off right from the parameter list
(loop [[next-char & rest-remaining :as r] rest-phrase ; Deconstruct the remaining string apart
acc first-char] ; Keep track of the letters to show at the start
(println (str acc (apply str r))) ; Print the prefix accumulator, then the remaining phrase
(if rest-remaining ; Recur if there's part of the phrase left
(recur (if (= next-char letter)
(rest rest-remaining)
rest-remaining)
(if (= next-char letter)
(str acc next-char)
acc)))))
Bash + Unix utilities, 89 88 85 84 82 bytes
z=$1
for((;${#z}-n;)){
echo "$z"
n=${#z}
z=`sed "s/^\(.[$2]*\)[^$2]/\1/"<<<"$z"`
}
Note that you can't write just $2 instead of [$2] in the regex; if you do that, it will work most of the time, but it will fail if the second argument is a special character like * or /.
Edit: Thanks to @ckjbgames for 3 bytes.
SmileBASIC, 65 bytes
INPUT S$,C$?S$@L
IF I&&S$[I]!+C$THEN S$[I]=""?S$ELSE I=I+1
GOTO@L
Vim, 27, 26, 25 bytes
DJqqYp:s/.\zs[^<C-r>-]<CR>@qq@qD
Input comes in this format:
e
codegolf.stackexchange.com
My naive first approach is three bytes longer:
i:s/.\zs[^<Right>]<Esc>"addqqYp@a@qq@qdd
I'm also happy with this answer because it starts with my name.
DJqq:t$|s/.\zs[^<C-r>"]<CR>@qq@qD
DJMcMayhem
See the similarity? Eh?
Less successful approaches:
i:s/.\zs[^<Right>]<Esc>"addqqYp@a@qq@qdd
i:s/.\zs[^<Right>]<CR>@q<Esc>"adkqqYp@aq@qdd
DJ:s/.\zs[^<C-r>"]<CR>uqqYp@:@qq@qdd
DJqq:t.|$s/.\zs[^<C-r>"]<CR>@qq@qdd
Explanation:
D " Delete everything on this first line
J " Remove this empty line
qq " Start recording into register 'q'
Y " Yank this line
p " And paste it
:s/ " Run a substitute command on the last line in the buffer. Remove:
. " Any character
\zs " Move the selection forward (so we don't delete the first one)
[^<C-r>-] " Followed by anything *except* for the character we deleted
<CR> " Run the command
@q " Call register 'q'
q " Stop recording
@q " Run the recursive macro
D " Delete our extra line
Scala, 107 82 bytes
Thanks to corvus_192 for a scala code-golf discussion link (appreciated!)
Golfed
1.to(s.size-1)./:(s take 1)((p,i)=>if(s(i)!=c){println(p+s.drop(i));p}else p+s(i))
Ungolfed
(1 until s.length).foldLeft((s.take(1), List[String]())) { case ((prefix, acc), i) =>
s.charAt(i) match {
case char if char != c => (prefix, acc :+ (prefix + s.drop(i)))
case char => (prefix + char, acc)
}
}._2.foreach(println)
Using foldLeft means we can 'accumulate' the prefix, and print a new line when the next character does not equal 'c'. Short-hand for foldLeft is /:
Ungolfed, we can also accumulate the results and just print the results at the end (no side effects), plus the match statement makes it a little cleaner
Pyke, 26 19 17 bytes
jljjhF3<Q/Q*jih>s
- Q = input
j - j = input_2
ljjhF3 - for (i, j, j[0]) for i in range(len(j))
< - j[:i]
Q/ - ^.count(Q)
Q* - ^*Q
s - sum(j[0], ^, V)
jih> - j[i+1:]
Swift 3 - 151 147 bytes
Swift isn't the ideal language for golfing, particularly when it relates to string indexing. This is the best I could do:
func c(a:String,b:String){print(a);var q=Array(a.characters),i=1;while i<q.count{if "\(q[i])" != b{q.remove(at:i);c(a:String(q),b:b);break};i=i+1}}
Unfortunately, Swift needs spaces around != (but not for ==), and Swift 3 dropped the ++ operator. The trick for both of these is to convert to a character array which allows integer indexing, and using string interpolation of a character to convert back to a String ("\(c)").
Ungolfed:
func c(a:String, b:String) {
print(a)
var q = Array(a.characters)
var i = 1
while i < q.count {
if "\(q[i])" != b {
q.remove(at:i)
c(a: String(q), b: b)
break
}
i=i+1
}
}
Previous, non-recursive solution
func c(a:String,b:String){var q=Array(a.characters),e={q.removeFirst()},z="\(e())";print(a);while !q.isEmpty{"\(e())"==b ? z=z+b : print(z+String(q))}}
func c(a:String, b:String) {
var q = Array(a.characters)
var z = "\(q.removeFirst())"
print(a)
while !q.isEmpty {
if "\(q.removeFirst())" == b {
z = z + b
}else{
print(z + String(q))
}
}
}
C#, 91 bytes
s=>c=>{var r=s;for(int i=1;i<s.Length;)if(s[i++]!=c)r+="\n"+(s=s.Remove(--i,1));return r;};
Anonymous function which merges each line of output into a string and returns the result, since the function may return an array of strings instead of will return.
Full program with ungolfed method and test cases:
using System;
namespace CeeeeeeeeProgram
{
class Program
{
static void Main(string[] args)
{
Func<string, Func<char, string>> f =
s => c =>
{
var r = s;
for (int i = 1; i < s.Length; )
if (s[i++] != c)
r += "\n" + (s = s.Remove(--i, 1));
return r;
};
// test cases:
Console.WriteLine(f("codegolf.stackexchange.com")('e'));
Console.WriteLine(f("Test Cases")('s'));
Console.WriteLine(f("Make a \"Ceeeeeeee\" program")('e'));
Console.WriteLine(f("Hello World!")('!'));
Console.WriteLine(f("Hello World!")('z'));
Console.WriteLine(f("alphabet")('a'));
Console.WriteLine(f("upperCASE")('e'));
}
}
}
Alternatively, an anonymous function with no return type which will print the output to stdout line by line will count 105 bytes:
(s,c)=>{Console.WriteLine(s);for(int i=1;i<s.Length;)if(s[i++]!=c)Console.WriteLine(s=s.Remove(--i,1));};
JavaScript, 91 bytes
function(p,k){for(var i=1;i<p.length;)p[i]!=k?console.log(p=p.slice(0,i)+p.slice(i+1)):i++}
Huge thanks to ETHproductions for the help!
C, 83 79 bytes
char x[];i;m(char*v,c){for(x[i]=*v;*v;)*++v-c?printf("%s%s\n",x,v):(x[++i]=c);}
Haskell, 50 bytes
w@(a:x)%c|(d,_:y)<-span(==c)x=w:(a:d++y)%c|0<1=[w]
Defines a function (%) returning a list of strings.
Explanation
(%) is called as w%c, with w being the input string, and c the character to keep. In short, this definition works by separating w into the first character (a) and the remainder (x), splitting x at the first occurrence of a character other than c, and recursively calling itself with that one character dropped.
w@(a:x)%c -- define (%) with w separated into a and x.
|(d,_:y)<-span(==c)x -- split x; bind the string of `c` to d, and the rest
-- to _:y, dropping first character and calling the rest y.
=w:(a:d++y)%c -- if there was a character to drop, cons w onto the
-- sequence gained by recursively calling (%) with that
-- character removed (i.e. call with a:d++y).
|0<1=[w] -- if nothing needed to be dropped, the result sequence is
-- simply the one-element list [w]
Python 3, 67 63 142 Bytes
a,b,l1=input(),input(),0
for n in range(len(a)):
l2=l1
l1=''.join([a[:len(a)-n]]+[x for x in a[len(a)-n:] if x==b])
if l2!=l1: print(l1)
It takes a as the string and b as the char, then goes through each letter in the string and adds it to the list if it's the first one or the character. Then it joins and prints.
EDIT: Fixed the problem mentioned by @R. Kap where it included copies of the first character as well, and shortened it to 63 bytes in the meantime.
EDIT 2: Now I realize you have to print out each string along the way. Brilliant. Well, this code is no longer golfed, but rather driven to the hole on a moped.
Ruby, 148 139 97 90 83 77 62 bytes
a,c=$*;p s=""+a;i=1;while d=s[i];(d!=c)?(s[i]="";p s):i+=1;end
Not sure if amateur code is accepted on this exchange but I'm interested in learning to code golf although I'm terrible at it, any help on how I'd get this program looking as small as the others here?
EDIT:
Replaced puts with p
Removed a tonne of whitespace and counted bytes correctly thanks to Wheat Wizard
Thanks to challenger5 went from s=gets.chop;c=gets.chop; to s,c=gets.chop,gets.chop;
replaced then with ; and gets.chop with gets[0] thanks Mhutter!
Taking input as command line variables now, eg. prog.rb helloworld l
Thanks to numerous improvements by jeroenvisser101 replacing a=s.dup with s=""+a and the previous if statement if s[i]!=c;s[i]="";p s;else i+=1;end with (d!=c)?(s[i]="";p s):i+=1; huge improvement!
Racket 194 bytes
(let p((s s)(n 1)(t substring)(a string-append))(displayln s)(cond[(>= n(string-length s))""]
[(equal? c(string-ref s n))(p(a(t s 0 n)(t s n))(+ 1 n)t a)][else(p(a(t s 0 n)(t s(+ 1 n)))n t a)]))
Ungolfed:
(define (f s c)
(let loop ((s s)
(n 1))
(displayln s)
(cond
[(>= n (string-length s))""]
[(equal? c (string-ref s n))
(loop (string-append (substring s 0 n) (substring s n))
(add1 n))]
[else
(loop (string-append (substring s 0 n) (substring s (add1 n)))
n)])))
Testing:
(f "Test cases" #\s)
(f "codegolf.stackexchange.com" #\e)
Output:
Test cases
Tst cases
Tst cases
Ts cases
Tscases
Tsases
Tsses
Tsses
Tsss
Tsss
""
codegolf.stackexchange.com
cdegolf.stackexchange.com
cegolf.stackexchange.com
cegolf.stackexchange.com
ceolf.stackexchange.com
celf.stackexchange.com
cef.stackexchange.com
ce.stackexchange.com
cestackexchange.com
cetackexchange.com
ceackexchange.com
ceckexchange.com
cekexchange.com
ceexchange.com
ceexchange.com
ceechange.com
ceehange.com
ceeange.com
ceenge.com
ceege.com
ceee.com
ceee.com
ceeecom
ceeeom
ceeem
ceee
""
Python 2 - 65 73 Bytes
lambda s,c:[s[0]+c*s[1:i].count(c)+s[i+1:]for i in range(len(s))]
And a 76 Bytes recursive solution, because despite being longer than the firt one, I kinda like it better:
f=lambda s,c,i=1:s[i:]and[[s]+f(s[:i]+s[i+1:],c,i),f(s,c,i+1)][s[i]==c]or[s]
Java 8, 104 bytes
Golfed:
(x,y)->{String q="(.["+y+"]*)",s=x;while(!x.matches(q))s+="\n"+(x=x.replaceFirst(q+".","$1"));return s;}
Ungolfed
(x, y) -> {
String q = "(.[" + y + "]*)", s = x; // Store pattern for matching, and start our 'list' of strings.
while (!x.matches(q)) { // While our string doesn't fit a valid 'Ceeeeee' pattern...
s += "\n" + ( // We append our list of strings with a newline and...
x = x.replaceFirst(q + ".", "$1")// A version of the string where the next character that isn't our given character is removed through regex replacement.
);
}
return s; // We then return the newline-delimited string.
}
Output:
Hello world!
Hllo world!
Hll world!
Hllworld!
Hllorld!
Hllrld!
Hllld!
Hlll!
Hlll
Function is a lambda (BiFunction used in test code). Takes a String and a Character, and returns a String.
C#, 135 138 :( 137 bytes
Golfed:
IEnumerable<string>F(string s,char c){int i=1,l;for(;;){yield return s;l=s.Length;while(i<l&&s[i]==c)i++;if(i==l)break;s=s.Remove(i,1);}}
Ungolfed:
IEnumerable<string> F(string s, char c)
{
int i = 1, l;
for (;;)
{
yield return s;
l = s.Length;
while (i < l && s[i] == c)
i++;
if (i == l)
break;
s = s.Remove(i, 1);
}
}
Function returns collection of strings.
EDIT1: @psycho noticed that algorithm was not implemented properly.
EDIT2: Created variable for s.Length. One byte saved thanks to @TheLethalCoder.
sed, 76 (code) + 3 (-nr flags) = 79 bytes
N;s/(.*)\n(.).*/\2\1/
:L
h;s/.//;p;x
/^(.)(.\1*)$/q
s/^(.)(.\1*)./\1\2/
b L
Commented:
# Read in both lines, and swap the order so we can use
# back references to check for the special character
N;s/(.*)\n(.).*/\2\1/
:L
# Erase the first character (temporarily) and print the line
h;s/.//;p;x
# Terminate once only the second character in the buffer doesn't match the first
/^(.)(.\1*)$/q
# Erase the first non-matching character
s/^(.)(.\1*)./\1\2/
b L
Unfortunately, back references aren't permitted within character classes, so we're forced to repeat ourselves a bit on the last three lines. If it were possible, we could replace b L with t L, and remove /^(.)(.\1*)$/q entirely, thus dropping this down to 65 bytes.
Usage:
$ cat in
Test Cases
s
$ sed -nrf foo.sed < in
Test Cases
Tst Cases
Ts Cases
TsCases
Tsases
Tsses
Tsss
TSQL, 127 bytes(Excluding variable definitions)
DECLARE @1 VARCHAR(100)='codegolf.stackexchange.com'
DECLARE @2 CHAR(1) = 'o'
DECLARE @ char=LEFT(@1,1)WHILE patindex('%[^'+@2+']%',@1)>0BEGIN SET @1=STUFF(@1,patindex('%[^'+@2+']%',@1),1,'')PRINT @+@1 END
Formatted:
DECLARE @1 VARCHAR(100) = 'codegolf.stackexchange.com'
DECLARE @2 CHAR(1) = 'o'
DECLARE @ CHAR = LEFT(@1, 1)
WHILE patindex('%[^' + @2 + ']%', @1) > 0
BEGIN
SET @1 = STUFF(@1, patindex('%[^' + @2 + ']%', @1), 1, '')
PRINT @ + @1
END
05AB1E, 26 24 23 bytes
Thanks @Kade for 2 bytes!
Thanks @Emigna for 1 byte!
¬UDvy²k0Êiy¡¬s¦yý«Xs«=¦
Uses the CP-1252 encoding. Try it online!
y²k0Ê could be y²Ê but the " messes it up.
This probably could be golfed more because « is repeated twice. Please leave a comment if you have any suggestions or ways to golf it down more.
JavaScript (ES6), 74 bytes
(s,c)=>[...t=s.slice(1)].map(d=>c!=d?s+=`
`+s[0]+(t=t.replace(d,``)):s)&&s
C#, 122 117 112 bytes
IEnumerable F(string s,char c){for(int i=0;i<s.Length;++i)if(i<1||s[i]!=c)yield return i>0?s=s.Remove(i--,1):s;}
Ungolfed :
public IEnumerable F(string s, char c) {
for (int i = 0; i < s.Length; ++i) {
if (i < 1 || s[i] != c)
yield return i > 0 ? s = s.Remove(i--, 1) : s;
}
}
Returns a collection of strings.
Groovy, 34 bytes
{a,b->a.inject{i,r->i+=r==b?b:""}}
PHP, 88 84 86 85 82 81 78 bytes
1 byte saved thanks to @IsmaelMiguel, 3 bytes thanks to @user59178, 3 bytes inspired by @user59178
while($b=substr("$argv[1]\n",$i++))$a>""&$b[0]!=$argv[2]?print$a.$b:$a.=$b[0];
takes input from command line arguments; run with php -r <code> '<string>' <character>
- appends a newline to the input for an implicit final print.
That adds54 bytes of code, but saves on the output and an additionalecho$a;.
Perl 5, 29 bytes
I got 35 bytes using Strawberry Perl: 31 bytes, plus 1 for -nE instead of -e, plus 3 for space + -i (used for the single-letter input; the longer string is from STDIN).
chomp;say;s/(.)[^$^I]/$1/&&redo
However, I've no doubt this is doable without chomp; using <<<, which is 29 bytes, even though I can't test it myself using Strawberry.
say;s/(.)[^$^I]/$1/&&redo
Thus:
perl -im -nE'say;s/(.)[^$^I]/$1/&&redo' <<< "example"
c90, 129 125 bytes
with whitespace:
main(q, x)
{
for (char **v = x, *a = v[1], *b = a, *c;*b++;)
for (c = a; c == a | *c == *v[2] && *b != *v[2] && putchar(*c),
++c != b || *b != *v[2] && !puts(b););
}
without whitespace:
main(q,x){for(char**v=x,*a=v[1],*b=a,*c;*b++;)for(c=a;c==a|*c==*v[2]&&*b!=*v[2]&&putchar(*c),++c!=b||*b!=*v[2]&&!puts(b););}
ungolfed:
#include <stdio.h>
int main(int argc, char **argv)
{
char *a = argv[1];
for (char *b = a + 1; *b; b++) {
if (*b == *argv[2]) {
continue;
}
putchar(*a);
for (char *c = a + 1; c != b; c++) {
if (*c == *argv[2]) {
putchar(*c);
}
}
puts(b);
}
}
This takes a pointer to the start of the string, and loops, iterating this pointer until it reaches the end of the string. Within the loop, it prints the first character, then any instances of the second argument it finds between the start of the string and the pointer. After this, it calls puts on the pointer, printing out the rest of the string.
This must be compiled on a system where sizeof(int) == sizeof(char*). +3 bytes otherwise.
This is the first time I've tried code golfing here, so I'm sure there are some optimizations to be made.
Python 2, 71 66 bytes:
f,m=input();k=f[0]
while f:a=f[0]==m;k+=f[0]*a;f=f[1+a:];print k+f
A full program. Takes 2 inputs through STDIN in the format '<String>','<Char>'.
Also, here is a recursive solution currently at 140 bytes:
Q=lambda c,p,k='',j=1,l=[]:c and Q(c[1:],p,k+c[0]*(j<2)+c[0]*(c[0]==p),j+1,l+[k+c])or'\n'.join(sorted({*l},key=l.index))+('\n'+k)*(k not in l)
This one should be called in the format print(Q('<String>','<Char>')).
Mathematica, 64 bytes
Most@FixedPointList[StringReplace[#,b_~~Except@a:>b,1]&,a=#2;#]&
Anonymous function. Takes two strings as input, and returns a list of strings as output. Works by repeatedly removing the first non-instance of the character.
JavaScript (ES2015) - 86 bytes
let f=([a,...s],c,r=[])=>r[0]==a+s?r:f(a+s.replace(eval(`/[^${c}]/`),''),c,[a+s,...r])
JavaScript ES6, 89 bytes
I thought this would be an easy challenge, but I'm pretty sure I'm missing something here..
Uses recursion and returns an array of strings
(c,i=1,r)=>f=a=>a[i]?a[i++]==c?f(a):f(g=a.slice(0,i-1)+a.slice(i--),(r=r||[a]).push(g)):r
F=
(c,i=1,r)=>f=a=>a[i]?a[i++]==c?f(a):f(g=a.slice(0,i-1)+a.slice(i--),(r=r||[a]).push(g)):r
G=_=>A.value.length>1 && (O.innerHTML = F(E.value)(A.value).join`
`)
G()
<input id=A oninput='G()' value='alphabet'>
<input id=E oninput='G()' value='a'>
<pre id=O>
05AB1E, 26 25 bytes
¬ˆ[¦Ðg_#¬²k0Qi²ˆë¯J?,]¯J?
¬ˆ Put the first character into an array
[ While true
¦ Remove the first character
Ð Triplicate
g_# if the string is empty, break
¬²k0Qi if the first character is equal to the one specified in the input
²ˆ Add it to the array
ë Else
¯J? Display the array
, Display the remaining string
] End while
¯J? Display the last string
Please note that ¬²k0Q could be rewritten ¬²Q, but for some reason it doesn't work when the current character is a quote mark: Q returns the actual string instead of a boolean and it causes an infinite loop.
This code can be golfed further since ¯J? is duplicated. Moving this part in the loop would remove the duplication and would also allow to drop the closing square bracket.
Pip, 22 26 24 22 bytes
Lv+#Paa@oQb?++oPaRA:ox
Takes string as first command-line argument, character as second. Try it online!
Explanation
Loops over characters of input; if the character equals the special character, move on to the next one; if not, delete it and print the string.
An ungolfed version (a, b get cmdline args; o starts with a value of 1, x is ""):
P a Print a
L #a-1 Loop len(a)-1 times:
I a@o Q b If a[o] string-eQuals b:
++o Increment o
E { Else:
a RA: o x In-place in a, Replace char At index o with x (i.e. delete it)
P a Print a
}
Golfing tricks:
- The loop header for
Lis only evaluated once, so we can sneak the initial print in there.#Pa-1won't work becausePis low-precedence (it would parse as#P(a-1)), but we can rearrange it tov+#Pa, using thevvariable preinitialized to-1. - The
RA:operator returns the new value ofa, so we can print that expression instead of having a separatePastatement. - Now both of the branches of the if statement are single expressions, so we can use the ternary operator
?instead.
Python 3, 87 83 bytes
s=open(0).read()
p,c,s=s[0],s[-2],s[:-3]
for h in s:p+=s[:h==c];s=s[1:];print(p+s)
Explanation: I chose python 3 instead of python 2, because it allows to use file descriptors in open (and 0 is the descriptor of the standard input). In each loop iteration p keeps track of stored prefix. s[:h==c] works as follows: boolean expression h==c is casted into integer (0 or 1), and then the corresponding amount of characters from the beginning of s are taken.
P.S. The loop in the initial version was:
for h in s:
if h==c:p+=h
s=s[1:];print(p+s)
Dyalog APL, 27 bytes
{×i←⊃1+⍸⍺≠1↓⎕←⍵:⍺∇⍵/⍨i≠⍳≢⍵}
⍺ is the excluded character, ⍵ is the initial string
print argument; find index i of the first non-⍺ after the first char; if found, call recursively with i removed
Perl 6, 47 40 38 bytes
->\a,\b{a,{S/^(."{b}"*:)./$0/}...^{$^a eq $^b}}
->\a,\b{a,{S/^(."{b}"*:)./$0/}...^&[eq]}
{$^b;$^a,{S/^(."$b"*:)./$0/}...^&[eq]}
Expanded:
{ # lambda with two placeholder parameters 「$a」 and 「$b」
$^b; # declare second parameter
$^a, # declare first parameter, and use it to seed the sequence
{ # bare block lambda with implicit parameter 「$_」
S/ # string replace and return
^ # beginning of string
( # capture into 「$0」
. # the first character
"$b"* # as many 「$b」 as possible
: # don't allow backtracking
)
. # any character ( the one to be removed )
/$0/ # put the captured values back into place
}
...^ # repeat that until: ( and throw away the last value )
&[eq] # the infix string equivalence operator/subroutine
}
The reason ...^ was used instead of ... is that &[eq] wouldn't return True until the last value was repeated.
Retina, 28 27 bytes
Byte count assumes ISO 8859-1 encoding.
;{G*1`
R1r`(?!^|.*¶?\1$)(.)
Explanation
;{G*1`
There's a lot of configuration here. The stage itself is really just G1`, which keeps only the first line, discarding the input character. * turns it into a dry run, which means that the result (i.e. the first line of the string) is printed without actually changing the string. { tells Retina to run both stages in a loop until the string stops changing and ; prevents output at the end of the program.
R1r`(?!^|.*¶?\1$)(.)
This discards the first character which a) isn't at the beginning of the input, b) isn't equal to the separate input character.
Mathematica, 78 bytes
Damn you Martin Ender, I was almost first :p
(i=2;a={c=#};While[i<=Length@c,If[c[[i]]==#2,i++,c=c~Drop~{i};a=a~Append~c]];a)&
Unnamed function; straightforward implementation with a While loop and a few temporary variables.
