Jow Forums Interview

So you think you have what it takes to browse Jow Forums huh?

You have some data in a list, for example

[1, 2, 2, 3, 3, 3, 3, 3, 57, "hello world"]

Write a program that removes all entries that repeat 3 times or more in a row. Use any programming language you want.


Examples:

[1, 1, 1, 1, 2, 2, 3] -> [2, 2, 3]

[1, 1, 2, 1, 2, 1, 2] -> [1, 1, 2, 1, 2, 1, 2]


Brainlets need not apply.

Attached: 1551043926008.gif (487x560, 898K)

do your own homework, this is some cs100 shit

def niggerchallenge(lst)
return lst.reject { |x| lst.count(x) > 3 }
end

Fuck off Jow Forums you brainlet. OP said 3 times or more IN A ROW.

r e c u r s i o n

>t. has never been in an interview

loop through list, based on the current index look at the hardcoded elements in the i+1/i+2 index and update the list if it meets the condition since deletion is O(n) this isnt that bad

theres nothing wrong with the naive approach

def o(m):
if len(m) == 0:
return []

n = []

last = m[1]
count = 1

for i in m[1:]:
if i == last:
count += 1
else:
n.append((last, count))
last = i
count = 1
n.append((last, count))

p = []
for v, c in n:
if c >= 3:
continue
p += [v] * c
return p

niggerlicious but werks

def faggot(ls)
head = nil
i = 0
ret = []

while !ls.empty?
head = ls.pop

if head == ret.last
i += 1
else
i = 0
end

if i == 3
2.times { ret.pop }
i = 0
else
ret

oh this also fails now that i look at it i'll just kms

>updating data
ISHYGDDT

# xs is the input list
kill = set()
for i in range(len(xs) - 2):
a,b,c = xs[i:i+3]
if a == b == c:
kill.add(a)
print([x for x in xs if x not in kill])

Requires all elements of xs to be hashable. OP is a little vague, but I took it to mean that you should remove all instances of x if x,x,x appears anywhere in the list. So [1,1,1,2,1] goes to [2], not [2,1].

certain tools for certain jobs, if ur one of those autists who think le arrays are not supposed to be dynamic!!11one!1 then baka

If you delete elements from an array while iterating through it you don't belong on this board.

>last = m[1]
Should be m[0], right?
>for i in m[1:]:
Bit confusing to make i the element value instead of the element index, but it works I guess

No data should be mutable, you filthy fuck.

Like just loop through it backwards lmao

DELETE_REPS = 3

def gaychallenge(lst):
for i, k in enumerate(lst):
j = i
mark = 0
while j < len(lst) and lst[j] == k:
mark += 1
j += 1
if mark >= DELETE_REPS:
for _ in range(i, j):
del lst[i]

return lst

print(gaychallenge([1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 8, "meme", "meme2", "k", "k", "k"]))


Done in Python. In-place. Includes test. Set DELETE_REPS to change amount of reps to delete at.

Yeah that should be m[0]. My bad.

lmao doesnt know the difference between array and arraylist

haha epic funCtioNaL haskell master xD

lmao uses java

Did you write this specifically to trigger ? I'm not even him and I find that amount of mutating-while-iterating a little concerning

ebin post, Python master!!! XD

>array and arraylist
Get out filthy javatrash

:) ahh yes the tears of purists who cant figure out to use the tool for the job

MOM! cancel my meetings someone on Jow Forums actually thinks HASKELL isn't the perfect language for EVERY JOB

Nah don't worry it works, let's ship it

>using worse tools is appropriate for some jobs

I guess if the job is doing something poorly, then you're right. Different tools for different things.

OP here

Sorry you're wrong there buddy, I actually mean to remove the X in a row and keep the others.

Example:


[1, 1, 1, 2, 1] -> [2, 1]

Otherwise it would be a trivial problem not worth posting here.

acc = []
cur = []
for x in xs:
if len(cur) == 0 or x == cur[0]:
cur.append(x)
else:
if len(cur) < 3:
acc.extend(cur)
cur = []
if len(cur) < 3:
acc.extend(cur)
print(acc)


>Otherwise it would be a trivial problem not worth posting here.
You've got some weird standards there, my dude

def func(list lst)
if(elem in lst repets 3 times)
erase elem

no

Literally trivial with the best language (ECMAScript)
function removeRepeats(list) {
const isEqual = (i, j, k) => i >= 0 && k < list.length && list[i] === list[j] && list[j] === list[k];
return list.filter((_, i) => {
return !isEqual(i - 2, i - 1, i) && !isEqual(i - 1, i, i + 1) && !isEqual(i, i + 1, i + 2);
});
}

console.log(removeRepeats([1, 1, 1, 1, 2, 1, undefined, undefined, undefined]));

this could definitely be cleaned up a bit, but here you go OP
proc filterRepeats(nums: seq[int], val, cnt: int): iterator: int =
return iterator: int =
if nums.len == 0:
if cnt < 3:
for i in 1..cnt:
yield val
elif nums[0] == val:
let f = filterRepeats(nums[1..nums.high], nums[0], cnt + 1)
for i in f():
yield i
else:
if cnt < 3:
for i in 1..cnt:
yield val
let f = filterRepeats(nums[1..nums.high], nums[0], 1)
for i in f():
yield i

I wrote something in haskell, but it's ugly. It looked good when I removed "only 3", but adding the "3 or more" part made it repeat too much.

remove_triple :: Eq a => [a] -> [a]
remove_triple [] = []
remove_triple (x:y:z:zz:xs)
| x == y && y == z && z == zz = remove_triple (y : z : zz : xs)
| x == y && y == z = remove_triple (zz : xs)
| otherwise = x : remove_triple (y : z : zz : xs)
remove_triple (x:y:z:xs)
| x == y && y == z = remove_triple xs
| otherwise = x : remove_triple (y : z : xs)
remove_triple z = z

my solution:
import Data.List
removedups x = concat [if (length i) >= 3 then [] else i | i

nice!

What happens if it repeats 5 times in a row?

But anyways, what do you guys think about skipping every other element, say you got
[a,a,b,b,b] you check a, then check b since it change you continue from b, check the 3rd b and since it matches you check the one you skipped, if it matches you remove them if they don't you don't.

Technically this could save some comparisons, but you would have to store the current index and manage that, also you would have to have indexed lists in the first place, but I think that's a given.

Attached: 5e2.jpg (554x439, 38K)

remove_triple still works if it repeats 5 times in a row. if it detects 4 or more repeats, it only takes off the first one and recurses.

shitty half drunk python
def count(lst):
cur_el = None
cur_el_len = 0
res = []
for x in lst:
if x == cur_el:
cur_el_len += 1
else:
if cur_el_len > 0:
res.append([cur_el] * cur_el_len)
cur_el_len = 1
cur_el = x

if cur_el_len > 0:
res.append([cur_el] * cur_el_len)

return res

def solve(lst):
return [el for lst in count(lst) if len(lst) < 3 for el in lst]

if __name__ == '__main__':
print solve([1, 1, 2, 1, 2, 1, 2])
print solve([1, 1, 1, 1, 2, 2, 3])


[1, 1, 2, 1, 2, 1, 2]
[2, 2, 3]

TFW no walker variable

Kill yourself

Perform RLE on the list
Filter all the result with ≥3 in the RLE field (via a list comprehension, maybe)
Decode the list

Not super efficient but werks

I solved it this way. I agree about it not being very efficient but I learned about RLE so I got that going for me.

def encode(lst):
count = 1
prev = lst[0]
ret = []
for i in range(1, len(lst)):
if lst[i] == prev:
count += 1
else:
to_append = (prev, count)
ret.append(to_append)
count = 1
prev = lst[i]
ret.append((prev, count))
return ret

def decode(lst):
return [item for (item, count) in lst for _ in range(count)]

def solve(lst):
filt_lst = [(item, count) for item, count in encode(lst) if count < 3]
return decode(filt_lst)

test = [1, 1, 2, 1, 2, 1, 2]
print(solve(test)) # [1, 1, 2, 1, 2, 1, 2]
test = [1, 1, 1, 1, 2, 2, 3]
print(solve(test)) # [2, 2, 3]
test = [1, 1, 1, 2, 1]
print(solve(test)) # [2, 1]

doYourOwnHomework [] = []
doHomework (x:xs)
| trips 0 x xs = doHomework $ next xs
| otherwise = x : doHomework xs
where trips n a (y:ys)
| n == 2 = true
| y /= a = false
| otherwise = trips (n + 1) a ys
next (b:c:d)
| b == c = next (c:d)
| otherwise = (c:d)

I’m on a phone and this is probably nowhere near the most elegant solution, but whatever man it probably works
(also fuck you for putting a string in a list of ints)

public class NewClass {
public static ArrayList sortList(int[] theList){
int[] newList = new int[theList.length];
ArrayList shortList = new ArrayList();
newList[0] = theList[0];
newList[1] = theList[1];
for (int i = 2; i < theList.length; i++) {
if (!(theList[i] == newList[i-1] && theList[i] == newList[i-2])){
shortList.add(theList[i]);
}
}
return shortList;

}

public static void main(String[] args){
int[] a = {1,1,1,1,3,4,5,5,3};
ArrayList myArrayList = sortList(a);
for (Object o : myArrayList) {
System.out.println(o);
}
}
}

doesn't work for lists with 3 elements or less and just prints the elements in an ArrayList lmao im a trash programmer rn

forgot to put edge cases on the baby functions, you get the idea though

public static ArrayList findRepeats(ArrayList fin, int[] list, int prev, int count) {
if (list.length > 0) {
if (prev == list[0] && prev != -1) {
count++;
if (count >= 3 && !fin.contains(prev))
fin.add(prev);
} else {
count = 1;
}
findRepeats(fin, Arrays.copyOfRange(list, 1, list.length), list[0], count);
}
return fin;
}

public static void main(String[] args) {
int[] a = {1, 1, 1, 1, 3, 4, 5, 5, 5, 3};
ArrayList myArrayList = findRepeats(new ArrayList(), a, -1, 1);
log(Arrays.toString(myArrayList.toArray()));
}
fixed it up to use recursion

So (1 1 2 2 3 3 3 2 1) should become (1 1 2 2 2 1) and not ()? Alright OP, whatever you say.
-module(homework).
-export([do/1]).

eat(H, [H|T]) -> eat(H, T);
eat(_, T) -> T.

do([]) -> [];
do([H, H, H|T]) -> do(eat(H, T));
do([H|T]) -> [H|do(T)].

It's not tail recursive but I couldn't be bothered to make it tail recursive so go fuck yourself. It's a meme question with no real specific requirements and I'm pretty sure OP is a faggot.
But some autists are still going to come to me complaining that it's "not performant" or some shit. Go fuck yourself.

uhm

prolog
challenge([], []) :- !.
challenge([X], [X]) :- !.
challenge([X|[Y|[]]], [X|[Y|[]]]) :- !.
challenge([X|[Y|R]], [X|S]) :- \+ X is Y, challenge([Y|R], S), !.
challenge([X|[X|[Y|R]]], [X|[X|S]]) :- \+ X is Y, challenge([Y|R], S), !.
challenge([X|L], S) :- scroll(X, L, R), challenge(R, S).

scroll(X, [Y|L], [Y|L]) :- \+ X is Y.
scroll(X, [X|L], M) :- scroll(X, L, M).

rm3

Are you allowed to create a new list, or do the entries with 3 or more occurrences have to be ejected from the list?

either way works but you're a faggot if you mutate data

fn a_faggots_homework(list: &Vec) -> Vec
where T: std::cmp::PartialEq + std::clone::Clone {
list.iter().enumerate().filter(
|(i, val)| {
let it = list.iter().enumerate()
.skip_while(|(j, e)| j + 2 < *i || e != val)
.take_while(|(_, e)| e == val);
it.count() < 3
}
).map(|(_, val)| val).cloned().collect()
}

fn main() {
println!("{:?}", a_faggots_homework::(&vec![1, 1, 1, 1, 2, 2, 3]));
println!("{:?}", a_faggots_homework::(&vec![1, 1, 2, 1, 2, 1, 2]));
}

Attached: smug wojak rust.png (767x639, 109K)