r/dailyprogrammer • u/Cosmologicon 2 3 • Nov 06 '12
[11/6/2012] Challenge #111 [Easy] Star delete
Write a function that, given a string, removes from the string any * character, or any character that's one to the left or one to the right of a * character. Examples:
"adf*lp" --> "adp""a*o" --> """*dech*" --> "ec""de**po" --> "do""sa*n*ti" --> "si""abc" --> "abc"
Thanks to user larg3-p3nis for suggesting this problem in /r/dailyprogrammer_ideas!
46
Upvotes
8
u/DannyP72 Nov 06 '12 edited Nov 06 '12
Ruby
edit: typo
5
u/skeeto -9 8 Nov 06 '12 edited Nov 06 '12
Emacs Lisp,
JavaScript,
5
u/cooper6581 Nov 06 '12
C
4
u/Ledrug 0 2 Nov 06 '12
C. Only print the result of deletions, not making a copy, though the idea is the same.
4
u/[deleted] Nov 06 '12
J -- create a binary vector of where the '*'s are, shift it left and right and OR those three together, then filter chars based on that.
Or another, more interesting (but slower) way -- calculating the distances to the closest '*' per char:
4
u/[deleted] Nov 06 '12
CoffeeScript
4
u/skeeto -9 8 Nov 07 '12
Common Lisp,
Finally have a good use for destructuring bindings in a
loop.7
u/prophile Nov 06 '12
In Haskell:
1
u/Scroph 0 0 Nov 06 '12
In the fifth example, shouldn't it be "i" ? The first star removes a and n so the word becomes "s*ti" and the second then removes s and t, leaving only the i letter.
Reserving this post for my solution.
1
u/ssuda Nov 08 '12
3
u/alecbenzer Nov 06 '12
new to go, trying to familiarize myself with it:
3
u/recursive Nov 07 '12
Zippy python:
3
u/nagasgura 0 0 Nov 07 '12
Python one-liner:
3
u/flatterflatflatland 0 0 Nov 08 '12
Java, using Regex:
3
u/ahlk 0 0 Nov 24 '12
Perl one-liner
5
u/[deleted] Nov 07 '12 edited Nov 13 '12
Python:
I fucking love regular expressions.
EDIT: Fixed the regex. I escaped an
*inside of a character class. Derp.4
u/[deleted] Nov 06 '12 edited Nov 06 '12
python:
2
u/skeeto -9 8 Nov 06 '12
POSIX C,
Usage:
Output:
2
u/the_mighty_skeetadon Nov 06 '12
I did this one when it was just an idea! =) Here's my Ruby answer, using Regex:
2
u/s23b 0 0 Nov 06 '12
Perl:
the +1 is there so it returns the string even if no results are found.
2
u/[deleted] Nov 07 '12
coffeescript
2
u/taterNuts Nov 08 '12 edited Nov 15 '12
C#:
edit:
doesn't work in all cases, will refactor tomorrowshould work now, tried doing it without regex although it's probably not the best solution. // updated with /u/jappacappa's fix2
u/ChristopherShine Nov 09 '12
Couple days late, but non-regex JavaScript:
2
u/thoneney Nov 20 '12 edited Nov 20 '12
In C:
3
u/[deleted] Nov 07 '12
JAVA
2
u/[deleted] Nov 06 '12
Python. Any feedback would be much appreciated.
2
u/kirsybuu 0 1 Nov 06 '12
D language.
2
u/mowe91 0 0 Nov 06 '12 edited Nov 06 '12
in Python
without regex. Very ugly, I am sure -- I am beginner :)
1
u/[deleted] Nov 06 '12
groovy
1
u/hmmdar 0 0 Nov 06 '12
Non-regex method in go
1
u/meowfreeze Nov 06 '12 edited Nov 13 '12
Python.
1
u/pitkali Nov 06 '12
Common Lisp, non-regexp version for the fun of learning a new language:
1
u/shaggorama Nov 07 '12
Stress programming because of the election. Here's python without RegEx:
1
u/CujoIHSV Nov 07 '12 edited Nov 07 '12
C++
Edit: In retrospect, I probably could have done both parts of this with iterators to maintain consistency, but this still works just fine.
1
u/charmlesscoin 0 0 Nov 07 '12 edited Nov 07 '12
Learning C now, threw together this thing with my own print function. Probably not the best approach, but it works.
1
u/dgamma3 Nov 07 '12
Javascript - noob.
Not sure how to combine the output though, any suggestions?
1
u/pbl24 Nov 08 '12
Not thoroughly tested, but here's my pass using Python and list comprehensions:
1
u/eagleeye1 0 1 Nov 08 '12
Python regex and non-regex comparison
1
u/error1f1f 0 0 Nov 09 '12 edited Nov 09 '12
In C++:
1
u/timmense Nov 09 '12 edited Nov 09 '12
Python (non regex). Thanks to the fantastic 'Learn To Program' course from Coursera for teaching me Python.
1
u/learnin2python 0 0 Nov 09 '12
python noob comments welcome.
1
u/Miss_Moss Nov 10 '12
C++
1
u/fruitcakefriday Nov 10 '12 edited Nov 11 '12
Python, can specify culling range. Commented.
1
u/takac00 Nov 11 '12
Clojure solution:
1
u/PonchoMF Nov 12 '12
Like a thousand years later my answer (On python), but I'm new at reddit and I just saw this.
1
u/Zamarok Nov 12 '12
JavaScript
1
u/letterboxed 0 0 Nov 13 '12
1
u/thegoodvibe 0 0 Nov 13 '12
Ok, better late than never. this is my first time, so my code is a bit longer than some of the others i have seen here. (java)
Also, please let me know what you think, as i mentioned, im a new programmer, so I'm still getting the hang of things.
1
u/thebugfinder Nov 14 '12
Hi everyone, this is my Ruby code, i tried to stay away from the regexp and take a complete different approach.
Ouput:
1
u/TimeWizid Nov 14 '12 edited Nov 15 '12
Using C# and LINQ, this solution gets the neighbors for each char and returns the chars that aren't stars and that don't have any stars for neighbors.
Edit: Method syntax actually works fairly nicely for assigning filtered:
1
u/[deleted] Nov 14 '12
I'm very new to Python. Here's what I came up with:
1
u/strider978 Nov 14 '12
C++
1
u/bob1000bob Nov 14 '12 edited Nov 14 '12
A c++ version that doesn't suck:
The output:
1
u/ahoy1 Nov 17 '12
my hackneyed solution in python
It's ugly, but I'm pretty new at programming.
1
u/Steve132 0 1 Nov 21 '12
1
u/Ty-chan 0 0 Nov 29 '12
Despite being beyond late, I finished it in some rather ugly code.
python
1
u/kaoskastle 0 0 Dec 09 '12 edited Dec 09 '12
My Ruby effort. Brand new to programming, so this one took me a while to cook up. Looking through the examples here from more experienced Ruby users, it looks like I have a lot to learn, haha. Anyway, here's my attempt; probably not the best solution, but it works!
edit: cleaned up the regex a bit
1
u/wocamai Dec 10 '12
My late python 2.7 submission. Kind of janky but it works.
1
u/Quasimoto3000 1 0 Dec 25 '12
Simple python solution. Kinda ugly but works.
1
u/cdelahousse Dec 25 '12
C
1
u/cdelahousse Dec 25 '12
Javascript
1
u/ttr398 0 0 Jan 05 '13
VB.Net because fuck you.
Also I'm not super happy with this one. I struggled with it a bit. Any feedback would be great. I settled on this iterative approach after failing to make recursion work.
My original solution was essentially the unstar() function, but with a call to itself on the output. This only worked for the first run through the word, ie - the first star.
As may be clear I've not really used recursion yet, so again - any pointers v. welcome. Not entirely confident that recursion would have been the right way to go with this anyway (excluding the obvious pattern matching possibilities).
1
u/marekkpie Jan 08 '13 edited Jan 18 '13
Lua. Pretty simple with pattern matching.
To explain the pattern:
Erlang:
1
u/nanermaner 1 0 Jan 12 '13
It's Hideous, I'm a beginner and I'm not proud of it, I had a hard time having it deal with when the first or the last character in the string is a *, but it works. Java:
}
1
u/RichardBoyd42 Apr 05 '13 edited Apr 10 '13
ruby
1
u/[deleted] May 04 '13
javascript
regex:
not regex:
-1
u/gammadistribution 0 0 Nov 06 '12 edited Nov 06 '12
The hardest part for me is concisely making a docstring :\
EDIT: I was hoping to do it without regexp. Too inelegant though.
Also if you are downvoting me, please leave a reply explaining why.
0
u/robotreader Nov 06 '12
I got a little carried away. Ruby's fun!
0
u/bfabri Nov 13 '12 edited Nov 13 '12
My solution in javascript:
0
u/[deleted] Nov 06 '12
[deleted] — view deleted comment
0
u/[deleted] Nov 07 '12
[deleted] — view deleted comment