/dpt/ - Daily Programming Thread

What are you working on, Jow Forums?

Previous thread:

Attached: functional-programming-in-php-43-638.jpg (638x479, 33K)

Other urls found in this thread:

youtube.com/watch?v=8iYdJH1i4rc
youtube.com/watch?v=cy4AUzsGbfE
twitter.com/SFWRedditVideos

Haskell devs are unemployed pedophiles

>in PHP we must explicitly state what we want to bring in from the enclosing scope to achieve a closure
this is why you want to have variable declarations

Given an integer array and number k, output all unique pairs that sum up to k. Example: for input [1, 3, 2, 5, 46, 6, 7, 4] and k = 4, output (1, 3).

this is an interview question not homework
ill post my solution in a while

Attached: 2018-09-13--1536872415_1019x444_scrot.png (1019x444, 30K)

>unemployed
Yes.
>pedophile
No.

How good do I have to be in dynamic memory allocation?

Not at all, unless you're dealing with legacy code.

same in lisp
(define (f) (let ([y 1]) (λ (x) (+ x y))))

(displayln ((f) 2))

A two pass solution (O(n)) would be for the first pass put everything into a set (hash set)

Then on the second pass do
for i in list:
is k-i in set? you found a solution
then REMOVE i from the set (important to avoid duplicates)

Pretty sure that works

Sorting would be bad since its O(nlgn) and you would probably fail the interview

...

from itertools import combinations

def solution(array, k):
lst = list(combinations(array, 2))
return [pair for pair in lst if sum(pair)==k]

if __name__ == '__main__':
array = [1, 3, 2, 5, 46, 6, 7, 4]
k = 4
print (solution(array, k))

programming is too hard but i want to learn python. i have everything set up somehow on my computer but i havent touched it in months...

I have a thinkpad t400 lying around with no usage whatsoever and a newer computer with linux mint on it.
What programming language should i try to work with? Python...im kinda dumb

Attached: 1536099562600.png (300x368, 117K)

why is python so ugly
in c++ this is just
const auto pairs = Range(nums) | Product( Range(nums)) | Filter( tupleSumsToK) | CollectList()


eitherway its n^2 and you failed the interview and will be homeless now

Attached: .png (625x625, 401K)

Alright, if duplicates are allowed (in the list) I would then try and catch them on the first pass.

If there are three 2's in the list, then any sort of pair (x,2) x+2=k should only be output once, so duplicates only matter for the case y+y=k

So you catch them on the first pass.
When you encounter x, check if 2x = k, then check if x is already in the set.

Don't forget to clean shit up. That's it. As long as there is one owner of the data (and 99% there's only one at any time), he should clean shit up. If someone else uses that data, they should either do that temporarily and explicitly, only in one function, or themselves belong to the owner. That way you won't have the problem of deciding what part of the program when should free the memory. It's all about the structure.

>you failed the interview
nah

C# is very easy to learn and a great stepping stone to get into C++ coding. Once you learn a language or two the rest are fairly simple to pick up

this is my join for the str
#include

template
ForwardIt skip(ForwardIt first, ForwardIt last, const T &value)
{
for (; first != last && *first == value; ++first) {}
return first;
}

template
OutputIt join(ForwardIt first, ForwardIt last,
OutputIt o_first, OutputIt o_last,
const T &value)
{
while (first != last)
{
first = skip(first, last, value);
for (; (first != last)
&& (*first != value)
&& (o_first != o_last); ++first, ++o_first)
{
*o_first = *first;
}
if (o_first == o_last) break;
if (first != last) *o_first++ = value;
}
return o_first;
}

int main()
{
const char str[] = " a set of strings ";
char ostr[10];
auto n = join(str, str + sizeof(str), ostr, ostr + sizeof(ostr), ' ');
for (char *c = ostr; c != n; ++c) putchar(*c);
}


nice to see you again libanon

ok thats a nice soloution
it does make it a bit more uglier and less expressive though

yikes

In these types of questions they expect you to produce a fast solution.

O(n^2) is so unbelievably worse than O(n), and it just shows you didn't really think about the problem in a clever way, but just brute forced it using some meme solution

>just brute forced it using some meme solution
yes

>wasting time with these problems
nah

>after linking with 46 different libraries and an hour of just debugging the compilation process, I finally have a 3 line program that puts a window on the screen (37 MB runtime btw)

the absolute state of modern computing

in my country, programming jobs don't require you to solve any programming problem.

you just talk about your past project or so, and maybe they send you a quick project for you to do in a week

Attached: 1536864856434s.jpg (187x250, 6K)

>don't specify what O speed is required
>lmao that's the wrong one, sorry nerd
This is why software is such a shitshow.
just learn vulkan

youtube.com/watch?v=8iYdJH1i4rc

A while ago I ground out a bunch of these leetcode type problems.
This sort of two pass, hash set, solution is common for these types of problems.

It all comes down to just learning the types of techniques you apply.
If you encounter something novel then it will depend on how smart you are, and how quickly you can relate the problem to something you are familiar with and adapt a known technique to it.

String join (const String a, const String b)
{
if(!b.length()) return a;
return join( a.push_back(b.peek()), b.pop());
}

Attached: .jpg (1920x1080, 174K)

sorry buddy, but you failed
>proceeds to spaz out like a baby
Its ok. Just practice solving these kinds of problems and learn what techniques the optimal solutions typically use

Attached: frend.jpg (680x344, 27K)

String join (const String a, const String b) {
return if(!b.length()) ? a : join(a.push_back(b.peek()), b.pop());
}

pairsWithSum xs total = nub [(min a b, max a b) | a

oops
String join (const String a, const String b) {
return !b.length() ? a : join(a.push_back(b.peek()), b.pop());
}

literally what's the point of this

that is nice
if only c++ had real pattern matching
maybe in c++35

>why
because i can

very nice, although the name join that I originally wrote is crap... is more like, 'trim_by' something

can you just say what the expected input and output is because this style is a chore to read and think about

anyone good at reverse engineering shit?
seems like it would be fun to know how, but also a lot of time put in for very little result

Attached: 1530086419898.jpg (400x400, 24K)

>is k-i in set?
Depending on set implementation, this could be a O(n) or (log(n)) worst case operation.

in my country, programming jobs don't require any skill whatsoever.

you just show your github avatar and they hire you (or not) right away.

>put everything into a set
arrays allow duplicates so you cant really do that

>C is perfect for writing kernels

Attached: 1504355056731.jpg (1024x1024, 124K)

...

C is the only language out there that can be used for that niche
Any language that tried to replace it would just be exactly like C

Java HashSet has constant time access (ie contains(x))

Only in a degenerate case of the hash dispersing the elements poorly would it start to become bad

I addressed the duplicates in a follow up post, so my original solution was wrong for the edge case of the list containing multiple elements y where 2y = k.
See

Pascal compiler targeting LLVM

>C is the only language out there that can be used for that niche
Even fucking Rust, which I hate more than the plague, works better for this.

Attached: 1504599921432.jpg (1605x655, 313K)

Attached: 1522002278486.jpg (645x968, 153K)

wrote an interpreter for pascal once. god I hate this shitlang

What do you dislike about it?

[(a, b) for i, a in enumerate(arr) for b in arr[i + 1:] if a + b == k]

I don't think a competent interviewer would fail you for answering with something like as long as it was in a language/framework that the company used, since it's so fast to write. If they were looking for better asymptotic behaviour, they would then ask for a more efficient solution. If you were able to provide one given the prompt, I think you would look pretty good - you just showed that you can quickly write something that works and then optimize it if necessary.

Paedophile is normal and natural nothing wrong about it.

youtube.com/watch?v=cy4AUzsGbfE

If it also checks a

Given an unsorted array, find the nth smallest element in the array

hint: there is an O(n) solution

Attached: 2018-09-13--1536875906_732x300_scrot.png (732x300, 15K)

>nth smallest element
when you say O(n)...

Attached: 1477076938213 (1).png (549x560, 258K)

no i mean the length of the list lol

Hash sets are constant time in the average case. In the worst case, you're still looking at either O(log(n)) or O(n) time. I'm not gonna say it's not a good algorithm regardless, just that it's not linear in the worst case, more like log-linear (which is close enough, to be honest).

Well, it was literally designed for this exact purpose.

C++, Rust, and Ada are all capable of being used to write kernels. You don't need C specifically, just a specific feature set. Namely, you need the ability to perform pointer arithmetic, dereference raw pointers, and the ability to be used without any runtime dependencies (no VM, no standard library that does I/O or allocates/deallocates memory). The language should also be able to call into functions written in assembly, either through linking to them or inline assembly.

>The language should also be able to call into functions written in assembly, either through linking to them or inline assembly.
Not even. Theoretically you only need a sufficient set of primitives that correspond to the necessary set of CPU intrinsics.

this is so easy, it's embarassing

Are you saying that a sequence of N accesses to a hashset is going to be something like NlgN?

Its amortized O(1) per access assuming a good hash, so N accesses will be O(N)

ok lets see your soloution and if its not O(n) you're fired

I guess dragging along an n sized array would be O(n^2)

yes
at that point just sort it
which is n + logn
so you're gonna want to find the other secret solution which beats both

good ol' ollydbg tutorials *sip*

Attached: 1526841439829.jpg (990x881, 187K)

n as in the nth smallest number or n as in the length of the input? n as in the nth smallest number is considered constant in relation to the input.

Can we first call it them mth smallest number to avoid such confusion?

its the same because the maximum n is m so its a pointless distinction for compelxity anylsis

It's pretty hard. You have to be very strategic about how you go about doing it.

public int getNthSmallestElementInTheArray(IEnumerable array, int n)
{
return array.OrderByDescending().Reverse().ToArray()[n-1];
}

list.sort()[n];

¿¿

n + logn you're FIRED

I looked up the solution (not gonna post it here, no spoilers) but I don't get why it works

>OrderByDescending().Reverse()
retard

>writing a function that's a single line of returning a different function

Attached: 1531502498005.jpg (731x840, 154K)

Rate my Algol 68:

list = struct (node val, ref list next);

>above

mode node = union (real, int, compl, string);

node n := "1234";

case n in
(real r): print(("real:", r)),
(int i): print(("int:", i)),
(compl c): print(("compl:", c)),
(string s): print(("string:", s))
out print(("?:", n))
esac

GOTTEM

Algol even knows strings? Why don't they use it anymore then?

The only good idea after Algol & Simula was parametric polymorphism imho. In every other way, modern languages are inferior knockoffs.

How many of you would take a job at Google if offered?

The feature I wanted to show off was tagged unions, i.e. like ADT's in Haskell.

As far as I know, most interviewers expect you to come up with the naive brute-force solution first and then refine that down or make optimizations to get a better runtime.

No. I don't want to work for large corporations.

What's the best website to grind out algorithm & data structures practice? There's like a billion of them.

Why would google hire me? I have a dick

2 years and then work as contractor or some comfy java shop job.

>comfy
>java

Attached: 1536726863974.jpg (680x383, 35K)

He meant serving java in a coffee shop

Thinking about it, would you even need anything besides interrupt related stuff?

In Matlab, I'm trying to add values to a vector A if a certain condition is met. It almost works, but if the condition is NOT met, it pushes a 0 to the end of the vector. I don't want that, I only want it to push the value if the condition IS met. Help?
My code looks something like:
>x = 0:1:100
>if f(x) > 0: A(end + 1) = x

Radix sort (linear time, in-place).
Do binary search for k/2 and k.
Yield all combinations of numbers below k/2.
Then iterate upwards for e in array between k/2 and k, yield everything below k - e starting from the front.

The total cost is O(n) + O(number of solutions)

java is unironically comfy desu

e.g. separated signatures and implementations in adhoc polymorphic functions - if I remember correctly there is no clear definition in the standard that specifies what happens if there are multiple functions with the same name and arity if they all have separated signatures.

So I noticed that IntelliJ will just import java.util.* if you have it automatically handle imports in a class where you use 6 or more classes from java.util

Not sure how I feel about this.

this would fail the sample input. It would give 2,2 which is not correct.

pairsWithSum xs total = nub [(min (xs !! i) (xs !! j), max (xs !! i) (xs !! j)) | i

Gnome Builder is literally broken out of the box. Running a simple hello world python script is impossible.
What the fuck.

It's not just java.util. but any common package with multiple classes in one.
I think you can hover and undo that though, or turn it off in an IDE setting.

I less and less would because of their politics.