We need more programming challenges. We should start off small: First non-repeating character of a string. Any language you like.
34 15 Apr 2016 09:42 by u/greaseBTC
Example input
yellow
tooth
Example output
y
h
Pro tip: Add four extra indents to your code for formatting.
52 comments
6 u/RayLomas 15 Apr 2016 12:01
Goddamnit... I should be working, and I'm writing a bash one liner...
Doesn't match the input precisely, but it's easy to adjust
6 u/VxMxPx 15 Apr 2016 12:49
No PHP? Here's one liner.
Edit: modified from two, to one line.
1 u/RayLomas 15 Apr 2016 15:35
This one takes the cake. Beauty of for ($i=0 or $var=fread(STDIN, 8192); is astonishing. I'm 100% serious.
0 u/VxMxPx 15 Apr 2016 22:01
Ha! Thank you. I knew I'll have to be a little bit creative if I'll go with PHP. ;)
3 u/djdevin 15 Apr 2016 16:17
different approach
1 u/VxMxPx 15 Apr 2016 22:02
This is wonderful, I was actually thinking about functional approach.
0 u/djdevin 17 Apr 2016 06:47
thanks, I call it the "lazy" approach
1 u/eqi397 16 Apr 2016 00:18
thats not really a one liner, more like 5 lines crammed into one
0 u/VxMxPx 16 Apr 2016 03:06
I'm pretty much sure that's the definition of one liners. But of course, this particular example is not something anyone should be using in production, for obvious reasons.
6 u/sir_hanselot 15 Apr 2016 15:41
Less Python:
2 u/i_am_triggered 15 Apr 2016 23:23
If you would use generator expression instead of list comprehension and next() instead of [0] it would stop on first match instead of going through whole string. Only difference would be IterationStop exception instead of IndexError when no match.
I didn't test it, but I'm pretty sure I'm right :)
5 u/main_gi 15 Apr 2016 13:42
Python
I love syntax errors. >_>
5 u/i_am_triggered 15 Apr 2016 23:20
This is not Python code, this is C code written in Python :P
3 u/main_gi 16 Apr 2016 04:43
Considering I don't know C, I estimate I will only get this joke 10 years later. @_@
1 u/TheBookWasBetter 16 Apr 2016 08:05
Python is an interpreted language built on C. Everything you write in python is viewed as C by your computer.
1 u/Tommstein 16 Apr 2016 09:10
Pretty sure that's not what he meant. Not least of which because your computer doesn't execute C either.
0 u/SelfReferenceParadox 17 Apr 2016 06:56
If we're going that route, isn't C just viewed as Assembly?
3 u/SelfReferenceParadox 17 Apr 2016 06:57
What he means is that the code is written the way you'd write it in C. In Python the preferred method would probably be more functional.
4 u/MadCamel 16 Apr 2016 07:20
Perl:
C. Pointer math is fun.
2 u/svipbo 16 Apr 2016 13:54
C version fails for input
aba. The code here printsaeven thoughais a duplicate.For
aabbc, it thinks the answer isb.1 u/MadCamel 16 Apr 2016 15:20
Thanks for giving it a try! I thought I was completely buried down here. That's proper behaviour given the specification as I understand it.
If I were asked to re-phrase the specification: Print the first character that isn't a repeat of the last.
1 u/svipbo 16 Apr 2016 15:26
I thought something like that, but
toothis supposed to beh. It isn'tt, becausetappears twice, even though the twot's aren't next to each other. I think it means the first character that doesn't appear in the string twice.3 u/RevanProdigalKnight 15 Apr 2016 13:31
JavaScript (ES6):
Written in plainer JavaScript (ES5) with comments:
0 u/greaseBTC [OP] 28 Apr 2016 18:30
Couldn't you stop at the first occurence of lastIndexOf === indexOf
I suppose that wouldn't match your ES6 exactly.
0 u/RevanProdigalKnight 28 Apr 2016 22:23
Yeah, I was trying to match the ES6 precisely, though I noticed a few days after posting that I could have used
arr = word.split('')instead of the manual loop.2 u/greaseBTC [OP] 15 Apr 2016 09:45
2 u/smeezekitty 16 Apr 2016 03:30
Shitty C
and a bit golfed
1 u/svipbo 16 Apr 2016 13:32
Here's my shitty C:
2 u/Tommstein 16 Apr 2016 06:14
In linear time, because you little bastards are performance-challenged:
Linear assuming the locs dictionary doesn't fuck shit up. Let's call it linearish. I'm not sure how Python dictionaries are implemented, but even the most retarded implementation on the planet should still only result in the quadratic time that you gits love so much.
2 u/TheBookWasBetter 16 Apr 2016 08:06
Can someone please write this in functional Scala?
1 u/Vladar 15 Apr 2016 11:33
Nim
0 u/Vladar 15 Apr 2016 14:10
Nim using strutils
1 u/Owoc 15 Apr 2016 13:15
Java:
4 u/Tommstein 16 Apr 2016 05:32
And this is why nobody likes Java.
2 u/Owoc 16 Apr 2016 13:21
It could alternatively be written like MrKequc's snippet to be much more concise, but slower:
1 u/Tommstein 16 Apr 2016 19:35
Yeah, I actually like your original algorithm, linear time I believe instead of all the quadratic BS that dominates the solutions here, the Java implementation just made me throw up in my mouth.
1 u/effusive_ermine 15 Apr 2016 17:57
1 u/rwbj 15 Apr 2016 20:55
C# without LINQ
1 u/irl_badass_here 17 Apr 2016 06:38
In K:
Example output:
1 u/greaseBTC [OP] 28 Apr 2016 18:38
I think you won at code golf.
1 u/SelfReferenceParadox 17 Apr 2016 06:54
In Haskell:
0 u/thefloodcontrol 15 Apr 2016 13:40
Copied from /tech/ ?
0 u/pushthis 16 Apr 2016 00:16
program that uses infographics to describe cannabis profits per resale price based on purchase price and resale portion size.
0 u/dizzydog 20 Apr 2016 22:05
Recursive Java:
0 u/whisky_cat 24 Apr 2016 11:30
fuck indents, especially four of them
0 u/greaseBTC [OP] 28 Apr 2016 18:38
It's that or two spaces at the end of every line.
ctrl-h \n \n[4 spaces]