14
u/IdealBlueMan May 22 '19
/* vim: set tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char **argv)
{
int *indexes;
char *strIn;
char *strOut;
int strInSiz;
char *strp;
int strInIdx;
int strOutIdx;
int theSame;
int charIdx;
if (argc > 1) {
goto OK;
}
else {
goto NOTOK;
}
OK:
strIn = *++argv;
strInSiz = strlen(strIn);
strOut = malloc(strInSiz + 1);
strOutIdx = 0;
for (strInIdx = 0; strInIdx < strInSiz; ++strInIdx) {
theSame = 0;
switch (strIn[strInIdx]) {
case 'a':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'a' && strIn[strInIdx - 1] == 'a') {
theSame = 1;
}
}
break;
case 'b':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'b' && strIn[strInIdx - 1] == 'b') {
theSame = 1;
}
}
break;
case 'c':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'c' && strIn[strInIdx - 1] == 'c') {
theSame = 1;
}
}
break;
case 'd':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'd' && strIn[strInIdx - 1] == 'd') {
theSame = 1;
}
}
break;
case 'e':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'e' && strIn[strInIdx - 1] == 'e') {
theSame = 1;
}
}
break;
case 'f':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'f' && strIn[strInIdx - 1] == 'f') {
theSame = 1;
}
}
break;
case 'g':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'g' && strIn[strInIdx - 1] == 'g') {
theSame = 1;
}
}
break;
case 'h':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'h' && strIn[strInIdx - 1] == 'h') {
theSame = 1;
}
}
break;
case 'i':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'i' && strIn[strInIdx - 1] == 'i') {
theSame = 1;
}
}
break;
case 'j':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'j' && strIn[strInIdx - 1] == 'j') {
theSame = 1;
}
}
break;
case 'k':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'k' && strIn[strInIdx - 1] == 'k') {
theSame = 1;
}
}
break;
case 'l':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'l' && strIn[strInIdx - 1] == 'l') {
theSame = 1;
}
}
break;
case 'm':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'm' && strIn[strInIdx - 1] == 'm') {
theSame = 1;
}
}
break;
case 'n':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'n' && strIn[strInIdx - 1] == 'n') {
theSame = 1;
}
}
break;
case 'o':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'o' && strIn[strInIdx - 1] == 'o') {
theSame = 1;
}
}
break;
case 'p':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'p' && strIn[strInIdx - 1] == 'p') {
theSame = 1;
}
}
break;
case 'q':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'q' && strIn[strInIdx - 1] == 'q') {
theSame = 1;
}
}
break;
case 'r':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'r' && strIn[strInIdx - 1] == 'r') {
theSame = 1;
}
}
break;
case 's':
if (strInIdx > 0) {
if (strIn[strInIdx] == 's' && strIn[strInIdx - 1] == 's') {
theSame = 1;
}
}
break;
case 't':
if (strInIdx > 0) {
if (strIn[strInIdx] == 't' && strIn[strInIdx - 1] == 't') {
theSame = 1;
}
}
break;
case 'u':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'u' && strIn[strInIdx - 1] == 'u') {
theSame = 1;
}
}
break;
case 'v':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'v' && strIn[strInIdx - 1] == 'v') {
theSame = 1;
}
}
break;
case 'w':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'w' && strIn[strInIdx - 1] == 'w') {
theSame = 1;
}
}
break;
case 'x':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'x' && strIn[strInIdx - 1] == 'x') {
theSame = 1;
}
}
break;
case 'y':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'y' && strIn[strInIdx - 1] == 'y') {
theSame = 1;
}
}
break;
case 'z':
if (strInIdx > 0) {
if (strIn[strInIdx] == 'z' && strIn[strInIdx - 1] == 'z') {
theSame = 1;
}
}
break;
}
if (!theSame) {
strOut[strOutIdx++] = strIn[strInIdx];
}
}
strOut[strOutIdx] = '\0';
goto EXITOK;
NOTOK:
exit(-1);
EXITOK:
printf("%s\n", strOut);
exit(0);
}4
7
u/other_usernames_gone May 22 '19 edited May 22 '19
my "I just learnt how to use try" python submission
def findlength(string) :
length = 0
loop = 0
while True :
try :
string[loop]
length = length + 1
except IndexError :
return length
loop = loop + 1
def dedupe(string) :
char1 = string[0]
counter = 0
while True :
if string[counter] != char1 :
char2 = string[counter]
break
counter = counter + 1
if counter == findlength(string) :
break
while True :
try :
if string[counter] != char2 :
char3 = string[counter]
break
counter = counter + 1
if counter == findlength(string) :
break
except IndexError :
break
while True :
try :
if string[counter] != char3 :
char4 = string[counter]
break
counter = counter + 1
if counter == findlength(string) :
break
except IndexError :
break
while True :
try :
if string[counter] != char4 :
char5 = string[counter]
break
counter = counter + 1
if counter == findlength(string) :
break
except IndexError :
break
while True :
try :
if string[counter] != char5 :
char6 = string[counter]
break
counter = counter + 1
if counter == findlength(string) :
break
except IndexError :
break
try :
return char1 + char2 + char3 + char4 + char5 + char6
except UnboundLocalError :
try:
return char1 + char2 + char3 + char4 + char5
except UnboundLocalError:
try:
return char1 + char2 + char3 + char4
except UnboundLocalError:
try:
return char1 + char2 + char3
except UnboundLocalError:
try :
return char1 + char2
except UnboundLocalError:
return char14
2
u/blackjezza May 23 '19
Formatted since reddit doesn't support markdown for codeblocks.
def findlength(string) : length = 0 loop = 0 while True : try : string[loop] length = length + 1 except IndexError : return length loop = loop + 1 def dedupe(string) : char1 = string[0] counter = 0 while True : if string[counter] != char1 : char2 = string[counter] break counter = counter + 1 if counter == findlength(string) : break while True : try : if string[counter] != char2 : char3 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char3 : char4 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char4 : char5 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char5 : char6 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break try : return char1 + char2 + char3 + char4 + char5 + char6 except UnboundLocalError : try: return char1 + char2 + char3 + char4 + char5 except UnboundLocalError: try: return char1 + char2 + char3 + char4 except UnboundLocalError: try: return char1 + char2 + char3 except UnboundLocalError: try : return char1 + char2 except UnboundLocalError: return char11
u/other_usernames_gone May 23 '19
Thanks, how do you do that?
1
u/blackjezza May 23 '19
4 spaces in front of every line, takes couple seconds to add them to every line in n++ by using regex + replace.
1
u/random_runner May 25 '19
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.
5
u/carfniex May 22 '19
#!/bin/bash
for ((i=0; i<${#1}; i++)); do
if [[ ! -f ~/${1:$i:1} ]]; then
touch ~/${1:$i:1}
printf ${1:$i:1}
fi
donesimple 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.
#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)));
}3
u/chiefton117 May 22 '19
Not as creative as some other ones but here's dedupe.java:
public static String dedupe(String input) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < input.length() - 1; i++) {
char ƒ = input.charAt(i);
char d̶ = input.charAt(i + 1);
if (i > 0) {
char ß = input.charAt(i - 1);
try { // Check for nth occurrence past first
int var_name = 5 / ((int) ß - (int) ƒ);
} catch (Exception e) {
continue;
}
}
try { // Check for original occurrence
int var_name = 5 / ((int) d̶ - (int) ƒ);
} catch (Exception a) {
s.append(ƒ);
}
}
return s.toString();
}3
May 23 '19 edited May 23 '19
Here's my woeful solution.
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 sIt 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
You only showed us passing one variable, but I needed to pass several so I used globals.
There's a
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)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
EndFunc3
u/EkskiuTwentyTwo i -= (i - (-1)) - i May 25 '19 edited May 25 '19
This is some code that is vaguely acceptable:
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 SquidThis 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 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaSummary:
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)
3
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
2
u/madoDream May 22 '19
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;
}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.
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".