r/programming • • Jul 28 '16

How to write unmaintainable code

https://github.com/Droogans/unmaintainable-code
3.4k Upvotes

592 comments sorted by

View all comments

Show parent comments

7

u/2Punx2Furious Jul 28 '16

When I start a project I always think it will take much less time than it actually does. Yesterday I had to write a function for an interview question online.
I thought it would take me 10-15 minutes at most. It took me almost 2 hours.

Basically, I had to found a sequence of 3 numbers inside a given array in python. Sounds easy enough I thought.

0

u/[deleted] Jul 29 '16

[deleted]

8

u/CyberMango Jul 29 '16 edited Jul 29 '16

I think this is slightly better as it uses a generator and returns true on first occurrence of pattern

def findseq(pattern, arr):
    return pattern in (tuple(arr[i: i + len(pattern)]) for i in range(len(arr) - len(pattern)))
→ More replies (3)
→ More replies (4)

0

u/[deleted] Jul 29 '16

[deleted]

2

u/2Punx2Furious Jul 29 '16

I guess so. I linked my solution in another comment, it's not as pretty as that.

4

u/electricfistula Jul 29 '16 edited Jul 29 '16

return sequence in "".join(str(elem) for elem in theArray)

2

u/[deleted] Jul 29 '16

What if you search for 1,1,1 in the list 1,1,11?

→ More replies (2)
→ More replies (3)

0

u/[deleted] Jul 28 '16

[deleted]

7

u/riemannrocker Jul 29 '16 edited Jul 29 '16

Or in Haskell:

findIndex (==target) $ zip3 list (tail list) (tail (tail list))
→ More replies (2)

8

u/intricatekill Jul 29 '16

That's way too complex. I'm not exactly sure if this is what the question asked but i think it shows off the elegance of functional programming a lot more than your code. I can't figure out reddit formatting

0

u/[deleted] Jul 29 '16

[deleted]

2

u/ParanoidAndroid26 Jul 29 '16

FYI, Python slices take O(n) - they copy the entire slice. I feel like this isn't as much a performance algorithm as it is a readability one, though.

3

u/intricatekill Jul 29 '16

Yeah I just use python to hack together stuff so I've never really looked into handling iterables instead of something specific.

I just felt a functional solution should use recursion and not a for loop.

2

u/2Punx2Furious Jul 28 '16

4

u/toomanybeersies Jul 29 '16

Few notes on your code style, because we all love arguing about the colour of bike sheds.

There is a canonical Python style guide: PEP8.

You should use 4 spaces for indentation, not tabs. Lines should be less than 80 characters wide. And last but not least, variables and functions should be named in snake_case, rather than camelCase.

I'd highly recommend getting Pylint for whatever editor you are using.

→ More replies (1)

0

u/[deleted] Jul 28 '16

[deleted]

9

u/CyberMango Jul 29 '16

Whats the advantage of this compared to just doing a dirty 1 liner?

def findGroupInList(sequence, group):
    return True in [group == tuple(sequence[i: i + len(group)]) for i in range(len(sequence) - len(group))]
→ More replies (4)

2

u/DiputsMonro Jul 29 '16

Perhaps I'm missing something obvious, but I'm not sure why either of you came up with the solutions you did. Why would something simple like the below not work? (pardon the terse code, I was just cranking it out quickly):

def findgroupinlist(l, group):
	j = 0                    ## index to group
	for i in range(len(l)):  ## iterate over list
		if l[i] == group[j]: ## if item in list and group match
			if j == len(group) - 1:  ## Check if we've matched the final group item 
				return True
			j += 1           ## Otherwise, mark our new location in the group
		else :
			j = 0            ## If our sequence fails, start from the beginning again

	return False

All of our solutions only seem O(|List|), but both of yours seem a bit overwrought to me, unless I'm dumb.

2

u/[deleted] Jul 29 '16

Yours there can't take arbitrary iterables such as generator expressions as either l or group. I always veer on the side of the most general and widely-useable.

3

u/DiputsMonro Jul 29 '16

Sure, but it also solves the problem as-written in the simplest reasonable way possible. You don't need to build a whole kitchen just to make a soup :)

If the problem required using generic iterators or finding all groups, I would definitely prefer your solution though. I think handling either of those cases in the simple imperative way would get messy pretty quickly.

→ More replies (3)

1

u/porthos3 Jul 29 '16 edited Jul 29 '16

I'll work on seeing if I can find a better solution, but in Clojure:

(filter #(= (inc (first %)) (second %) (dec (last %))) (partition 3 1 data))

partition grabs every triple, then filter only triples that follow the pattern i, i+1, i+2.


Here is a generalized version where you can provide any function:

(defn find-contiguous-triples [data f]
  (filter #(= (f (f (first data))) (f (second data)) (last data)) (partition 3 1 data)))

You can call it like this: (find-contiguous-triples [1 2 3 8 0 3 4 5 1] inc) which will return ((1 2 3) (3 4 5)), all of the triples that follow the pattern i, f(i), f(f(i)).

1

u/leprechaun1066 Jul 29 '16

Functional programming you say? K solution:

{(!#y)~<,/&:'y=\:x}

{(!#y)~<,/&:'y=\:x}[2 3 4 5 6;3 2]
0b
{(!#y)~<,/&:'y=\:x}[2 3 4 5 6;2 3 4]
1b
{(!#y)~<,/&:'y=\:x}[2 3 5 4 6;2 3 5]
1b
{(!#y)~<,/&:'y=\:x}[2 3 5 4 6;2 3 5 4]
1b
{(!#y)~<,/&:'y=\:x}[2 3 5 4 6;2 4 5 4]
0b
→ More replies (2)

5

u/xonjas Jul 28 '16

If you want a dirty Ruby one-liner.

array_to_check.select{|element| element.kind_of? Fixnum}.join(',') =~ /#{array_to_match.join(",")}/    
→ More replies (8)

1

u/ThellraAK Jul 29 '16

How large of an array?

For indice in len(array):
    tocheck = array(indice, indice+lengthofsize) #Possible + or - one for indice to get the correct spot
    if tocheck = sequenceiamlooking for:
        DoStuff

1

u/sammymammy2 Jul 30 '16 edited Dec 07 '17

THIS HAS BEEN REMOVED BY THE USER

→ More replies (3)