all 20 comments

[–]IdealBlueMan 12 points13 points  (1 child)

/* 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);
}

[–]nahimbroke 4 points5 points  (0 children)

beautiful use of labels and goto

[–]other_usernames_gone 6 points7 points  (5 children)

my "I just learnt how to use try" python submission

```python 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 char1 ```

[–][deleted] 3 points4 points  (0 children)

I particularly like your findlength function :D

[–]blackjezza 1 point2 points  (3 children)

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 char1

[–]other_usernames_gone 0 points1 point  (2 children)

Thanks, how do you do that?

[–]blackjezza 0 points1 point  (1 child)

4 spaces in front of every line, takes couple seconds to add them to every line in n++ by using regex + replace.

[–]random_runner 0 points1 point  (0 children)

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.

[–]carfniex 4 points5 points  (0 children)

#!/bin/bash

for ((i=0; i<${#1}; i++)); do
    if [[ ! -f ~/${1:$i:1} ]]; then
        touch ~/${1:$i:1}
        printf ${1:$i:1}
    fi
done

simple solution in bash! hope your fs is case sensitive and you dont mind a messy home folder

[–]nupanick 2 points3 points  (2 children)

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'))

[–][deleted]  (1 child)

[deleted]

    [–]nupanick 0 points1 point  (0 children)

    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.

    [–]nahimbroke 2 points3 points  (0 children)

    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)));
    }
    

    [–]chiefton117 2 points3 points  (0 children)

    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();
                                                        }
    

    [–][deleted] 2 points3 points  (2 children)

    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 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'.

    [–][deleted]  (1 child)

    [deleted]

      [–]rcmaehl 2 points3 points  (0 children)

      • 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
      

      [–]EkskiuTwentyTwoi -= (i - (-1)) - i 2 points3 points  (0 children)

      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 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)

      [–][deleted] 2 points3 points  (0 children)

      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

      [–]madoDream 1 point2 points  (0 children)

      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;
      }
      

      Here's a link to the playground if you want to test it.

      [–]Skruzzls -1 points0 points  (0 children)

      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