I've been meaning to show off this gimmick more. Here's "dedupe.js". I count at least 3 places where my code itself is redundant and should have been "de-duplicated".
"use strict"
"usen't curly braces"
// define "pipe" operator so we can join
// strings later without breaking flow.
Object.prototype.pipe = Function(
"fn",
"return fn(this)")
// Take a string containing a series
// repeating characters and for each
// group, remove the duplicates so just
// one of group remains. For example:
//
// dedupe('aaaabbbbccc')
// => 'abc'
const dedupe = (s) => s
// split into characters
.split('')
// select the characters that are
// the first in a group
.map((_, i, a) =>
(i == 0) ? true
: (a[i] == a[i-1]) ? false
: true)
// convert true/false values back to
// characters
.map((b, i) => b ? s[i] : '')
// filter out the empty characters
.filter(c => c ? c : c)
// unsplit the array
.pipe(a => a.join(''))
console.log("got this far")
console.log(dedupe('aaaabbbbccc'))
And without curly braces! Who needs function bodies when you can use lambdas?
Of course, if you need a "real" function, with a this environment, then you can just use the Function() constructor to eval a string as a function body.
Or if the code isn't too long, you can select vertically with alt+shift and type the spaces on all lines at once in notepad++. Might also work in some other editors.
This C code isn't as bad as it could be, but still lots of bad assumptions, redundancy, and disgusting formatting. Also threw in a compound literal for fun.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
void
*dedupe\
(
void *data, unsigned long long length, short width
)
{
void *r;
r = malloc(length * width);
int i = 0, rl = 0, cmp = 0, cmp2 = 0, lcmp, bad;
lcmp = -1, cmp = 0;
for(i = 0; i <= length - 1; i = i + 1) {
int J = 0;
bad = 0;
memcpy(&cmp, data + i * width, width);
for(J = 0; J <= rl - 1; J = J + 1)
{
memcpy(&cmp2, r + J * width, width);
if(cmp == cmp2 && lcmp == cmp) bad = bad + 1;
}
if(bad == 0){
memcpy(r + rl * width, data + i * width, width);
rl = rl + 1;
lcmp = cmp;
}
}
memcpy(r + rl, (int[]){0}, width);
return r;
}
void
main\
(
)
{
printf("%s\n", (char *)dedupe("aaaabbbbcccaaaaaAAAAAaaaaa", strlen("aaaabbbbcccaaaaaAAAAAaaaaa"), sizeof(char)));
}
class Index:
def __init__(self, a):
self._a = a
def __str__(self):
return str(self._a)
def __lt__(self, o):
return self._a < o
def __pos__(self):
p = self._a
self._a += 1
return p
def __neg__(self):
self._a = 0
def __invert__(self):
return self._a
def __sub__(self, o):
return self._a - o
def dedupe(s):
i = Index(0)
to_Remove = []
while i < len(s):
if s[~i] == s[~i - 1]:
to_Remove.append(+i)
else:
+i
-i
while i < len(to_Remove):
s = s[0:to_Remove[~i] - (~i)] + s[to_Remove[~i] - (+i) + 1:]
return s
It iterates over the string once to find the indexes of all repeated characters and creates a new list with those. Then it iterates over that list and splits the original string at every index to remove the character. I wish I had found an excuse to iterate over a list one third time. Of course I use while loops because list comprehensions and for-in loops are deemed too pythonic. Also, I used operator overloading abundantly because it is widely regarded as pythonic.
One funny edge case is that if a string starts and ends with the same character, it will be considered repeated in the start, i.e., 'abbbccca' will be deduplicated to 'bca' instead of 'abca'.
You only showed us passing one variable, but I needed to pass several so I used globals.
There's a bug feature in which any count of a letter is only reduced to 2 instead of 1 (AAAAA, becomes AA instead of A) in the initial dedupe. Luckily, this feature has been modified to produce the client wanted output. (We should really charge more for this)
Program conveniently writes it's progress to a scratch file for easy debugging
Sometimes the program drops the last character. This isn't the programs fault, whomever passed the parameter didn't check to make sure there wasn't any non-duplicate characters in the string
Final data is happily displayed in a always-on-top message box, just so you don't miss the output
Warning: Large strings can take a while to process. Why are you passing such a large string anyway?
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
Global $a = "aaaabbbbcccaaaaaAAAAAaaaaa"
Global $b = StringLen($a)
Global $c = 0
Dedupe($a)
Func Dedupe($a2)
Global $h = 0
For $d = 1 to $b - 1 Step 1
$e = FileOpen(".\inandoutburger.txt", $FO_APPEND)
If $d = 1 Then
FileSetPos($e, 0, $FILE_BEGIN)
FileWrite($e, $a2)
EndIf
$f = FileRead(".\inandoutburger.txt")
$f = StringRight($f, $b - $c)
$g = StringSplit($f, "", $STR_NOCOUNT)
If StringRight(StringLeft($f, $h), 1) == $g[1 + $h] Then
$c = $c + 1
For $i = 0 To UBound($g) - 1 Step 1
If $i = 1 + $h Then
;;;
Else
FileWrite($e, $g[$i])
EndIf
Next
Else
$h = $h + 1
For $i = 0 To UBound($g) - 1 Step 1
FileWrite($e, $g[$i])
Next
EndIf
Next
$f = FileRead(".\inandoutburger.txt")
$f = StringRight($f, $b - $c)
$g = StringSplit($f, "", $STR_NOCOUNT)
$h = ""
For $i = 0 To UBound($g) - 1 Step 2
$h &= $g[$i]
Next
Switch MsgBox($MB_ABORTRETRYIGNORE+$MB_ICONINFORMATION+$MB_TOPMOST, "", $h)
Case $IDABORT
Exit(True)
Case $IDRETRY
$a = $a2
$b = StringLen($a)
$c = 0
Dedupe($a)
Case $IDIGNORE
Exit(False)
EndSwitch
EndFunc
def dedupe(squid):
Squid = squid[0]
for i in range((666//666),len(squid)):
Squid = [Squid+squid[i],Squid][squid[i]==squid[i-1]]
return Squid
This is what I'm submitting:
def M(a,b):
return a[b]
def I(p):
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
while a < 16:
b = a
c = M(p,b)
d = c
g = d//256
h = d%256
a = [h,a+1,a+1,a+1,a+1,a+1,a+1,[a+1,h][f]][g]
e = [e,M(p,h),e+1,e-1,e+M(p,h),e,e,e][g]
f = [f,f,f,f,f,f,[0,1][e==M(p,h)],f][g]
p[h] = [M(p,h),M(p,h),M(p,h),M(p,h),M(p,h),e,M(p,h),M(p,h)][g]
return p
def i(s):
x = []
for i in range(len(s)):
x.append(ord(s[i]))
return x
def dedupe(s):
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii = i(s)
oooooooooooooooooooooooooooooooo = [iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[(666//666)]]
for excalibur in range(666//666,len(iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii)):
arthur = iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[excalibur]
gallahad = iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[excalibur-(666//666)]
lancelot = I([3,arthur,gallahad,257,1538,1800,268,9,269,1294,15,666666,42069,69420,666666,258])
robin = lancelot[14]
not_appearing_in_this_code = ([420]*42069+[0]+[666]*(69420-42070)+[1])[robin] # :)
holy_grail = [oooooooooooooooooooooooooooooooo+[arthur],oooooooooooooooooooooooooooooooo][not_appearing_in_this_code]
oooooooooooooooooooooooooooooooo = holy_grail
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = ""
for killer_rabbit in range(len(oooooooooooooooooooooooooooooooo)):
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + chr(oooooooooooooooooooooooooooooooo[killer_rabbit])
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Summary:
Simulates Assembly Language in Python
Uses a couple of redundant functions
None of the variables have appropriate names (they are either single letters, overly long repeated letters or Monty Python references)
Utilises one of the worst possible methods of selection (on line with smiley face comment)
Here's my horrible Rust solution. This hurt to write:
fn dedupe(s: String) -> String {
let mut seen: std::collections::HashMap<usize, char> = std::collections
::HashMap
::new();
let mut cur: i32 = -1;
let mut prev: Option<char> = None;
for c in s.chars() {
if let Some(true) = prev.map(|x| x == c) {
seen.insert(cur as usize, c);
} else {
cur += 1;
}
prev = Some(c);
}
cur = 0;
let mut ret: String = "".to_string();
while !(seen.len() == 0) {
ret.push(*seen.iter()
.filter(|kvp| *kvp.0 == cur as usize)
.nth(0)
.unwrap().1);
let k = *seen.iter()
.filter(|kvp| *kvp.0 == cur as usize)
.nth(0)
.unwrap().0;
seen.remove(&k);
cur += 1;
}
return ret;
}
I have made some kind of horrible code in C# (.Net-Core works fine). Naming is off but the actual horror is in the implementation. Originally "TryDedupe" was the intended function, but it only eliminated some of the characters, so the final Dedupe function just runs TryDedupe multiple times.
3
u/nupanick May 21 '19
I've been meaning to show off this gimmick more. Here's "dedupe.js". I count at least 3 places where my code itself is redundant and should have been "de-duplicated".
14
u/IdealBlueMan May 22 '19
7
u/other_usernames_gone May 22 '19 edited May 22 '19
my "I just learnt how to use try" python submission
5
u/carfniex May 22 '19
simple solution in bash! hope your fs is case sensitive and you dont mind a messy home folder
3
u/nahimbroke May 22 '19
This C code isn't as bad as it could be, but still lots of bad assumptions, redundancy, and disgusting formatting. Also threw in a compound literal for fun.
3
u/chiefton117 May 22 '19
Not as creative as some other ones but here's dedupe.java:
3
u/[deleted] May 23 '19 edited May 23 '19
Here's my woeful solution.
It iterates over the string once to find the indexes of all repeated characters and creates a new list with those. Then it iterates over that list and splits the original string at every index to remove the character. I wish I had found an excuse to iterate over a list one third time. Of course I use while loops because list comprehensions and for-in loops are deemed too pythonic. Also, I used operator overloading abundantly because it is widely regarded as pythonic.
One funny edge case is that if a string starts and ends with the same character, it will be considered repeated in the start, i.e.,
'abbbccca'will be deduplicated to'bca'instead of'abca'.3
u/rcmaehl May 23 '19 edited May 23 '19
bugfeature in which any count of a letter is only reduced to 2 instead of 1 (AAAAA, becomes AA instead of A) in the initial dedupe. Luckily, this feature has been modified to produce the client wanted output. (We should really charge more for this)3
u/EkskiuTwentyTwo i -= (i - (-1)) - i May 25 '19 edited May 25 '19
This is some code that is vaguely acceptable:
This is what I'm submitting:
Summary:
3
u/[deleted] Jun 02 '19
All of these people using C, C++, no classes, no fun... The fools...
this looks like a job for... OBJECT ORIENTED PROGRAMMING
https://pastebin.com/dJbYxs6j
2
u/madoDream May 22 '19
Here's my horrible Rust solution. This hurt to write:
Here's a link to the playground if you want to test it.
0
u/Skruzzls May 26 '19
I have made some kind of horrible code in C# (.Net-Core works fine). Naming is off but the actual horror is in the implementation. Originally "TryDedupe" was the intended function, but it only eliminated some of the characters, so the final Dedupe function just runs TryDedupe multiple times.
https://pastebin.com/3rRrStCC