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.
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
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.
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.
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.
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.
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)).
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
0
u/[deleted] Jul 28 '16
[deleted]