/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

wiki.libsdl.org/SDL_CreateRGBSurface
twitter.com/SFWRedditGifs

First for Lisp is the most powerful programming language

BRAINLET LANGUAGE CHECK: What's your language's solution to Letter Combinations?

Let's make it fair, no using builtin cross product functions from standard libraries.

Just use a recursive function in any language

Some languages have better ways of making the solution nice and declarative though.

For example, the Java solutions on leetcode are all very long.

23 would just be "ad" though.
That's a dumb example.

6th for fortran 77

cross [] = [[]]
cross (l:ls) = [x:xs | xs

telephone = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno',
'7': 'pqrs', '8': 'tuv', '9': 'wxyz'}

def comboGenerator(ds):
if ds == '': yield ''
else:
for l in telephone[ds[0]]:
for g in comboGenerator(ds[1:]):
yield l + g

>turning lain into moeshit

do you love lain?

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter a number n: ");
int n = input.nextInt();

int f1 = 1;
int f2 = 1;
int fnMinusOne = 1;
int fnMinusTwo = 1;
int fn = 1;

if ((n == 0) || (n < 0)) {
System.out.println("Your number n has to be bigger than 0");
System.out.println("Please try again: ");
n = input.nextInt();
} else if (n == 1) {
System.out.println(f1);

} else if (n == 2) {
System.out.println(f2);

} else {

int i = 3;

while (i

...

>if ((n == 0) || (n < 0))
You mean if (n

> Java

It's like you want to be bullied.

Holy shit
yes kek you see I am a brainlet
maybe I should kill myself

We need to learn it third week college here

In C if I want to print 5 tabs \t or 5 newlines \n do I have to write them 5 times? I'm surprised I can't do something like this printf("\5n");

Still working on that logic proof program.
I honestly hate my uni for making us do almost only OOP, functional programming is fucking nice.

(* Returns the Skolem form corresponding to expr, or expr itself if it has no quantifier *)
let rec skolem expr =
match expr with
| Atom(x) -> expr (* s(Φ) ---> Φ *)
| Or(x,y) -> Or( (skolem x), (skolem y) ) (* s(Φ | Φ') ---> s(Φ) | s(Φ') *)
| And(x,y) -> And( (skolem x), (skolem y) ) (* s(Φ & Φ') ---> s(Φ) & s(Φ') *)
| Not(x) -> Not(herbrand x) (* s(~Φ) ---> ~h(Φ) *)
| Implies(x,y) -> Implies( (herbrand x), (skolem y)) (* s(Φ => Φ') ---> h(Φ) => s(Φ') *)
| Forall(x,y) -> (skolem y) (* s(∀x.Φ) ---> s(Φ) *)
| Exists(x,y) -> skolem (assign expr x) (* s(∃x.Φ) ---> s(Φ)[f (x1, ... , xn)/x] *)
| _ -> expr

(* Returns the Herbrand form corresponding to expr, or expr itself if it has no quantifier *)
and herbrand expr =
match expr with
| Atom(x) -> expr (* h(Φ) ---> Φ *)
| Or(x,y) -> Or( (herbrand x), (herbrand y) ) (* h(Φ | Φ') ---> h(Φ) | h(Φ') *)
| And(x,y) -> And( (herbrand x), (herbrand y) ) (* h(Φ & Φ') ---> h(Φ) & h(Φ') *)
| Not(x) -> Not(skolem x) (* h(¬Φ) ---> ¬s(Φ) *)
| Implies(x,y) -> Implies( (skolem x), (herbrand y)) (* h(Φ => Φ') ---> s(Φ) => h(Φ') *)
| Forall(x,y) -> (herbrand (assign expr x)) (* h(∀x.Φ) ---> h(Φ)[f (x1, ... , xn)/x] *)
| Exists(x,y) -> herbrand (y) (* h(∃x.Φ) ---> h(Φ) *)
| _ -> expr

eww

wut
the current stage of Sup Forums
maybe you should consult with the creator of the whole string escape thingy cause this behaviour is standard from embeded to web dev

>consult with the creator of the whole string
Ok, what's his contact info?

Take that back cunt

Is he right?

At what point into learning Python should I start learning algorithms and data structures?
I will move onto c after this.

right now trying to make an API endpoint using aws lambda to store my logs in s3. Lambda is super cheap so thats why and apex.run lets us use golang with lambda, Apex all around is pretty nice and full featured. Kinda wondering why we don't just store directly to S3 now that i think about it, anyone know the pros to setting it up this way?

Do anyone here have experience with SDL? I'm trying to create an empty, white surface but no matter what I do it always renders as black.
Using this for reference: wiki.libsdl.org/SDL_CreateRGBSurface

Have you tried setting the bits the other way around?

Already tried that and confirmed that my machine is little-endian. I tried the 32 bit version of SDL to see if that somehow made it work but it didn't so now I'm kind of stuck

Have you tried taking a step back and literally fucking your own face?

Working on my own MIPS decompiler, part of a personal project, decompilation of n64 roms

Connect the dots, user.

done

CUTE

Holy shit I might be retarded. I didn't realize I had to fill the rect after creating it. I thought it would be set to white by default

FOL?

I'm a fucking retard but I can't find my problem.
I'm building a "dancefloor simulator", and pic related is the relevant code.
Every "dancer", or agent influences and gets influenced by their neighbor's music preference, visualised by red(rock), green(pop) or blue(techno).
However, when I run the simulation red(index 0) always wins.
The simulation starts perfectly random.
Where the fuck am I going wrong?
"tmp" is actually a formula btw, just using 0.1 for testing

The recursive solutions are actually slow and brainlet-tier, especially given that the core of a decent solution is about 10 lines even in C:
#include
#include

char const* chars[] = { " ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int last_char[] = {' ', ' ', 'c', 'f', 'i', 'l', 'o', 's', 'v', 'z'};

int next_combo(char const* number, char* combo, int len) {
int i;

for(i = 0; i < len; ++i) {
if(combo[i] < last_char[number[i] - '0']) {
++combo[i];
return 1;
}else
combo[i] = chars[number[i] - '0'][0];
}

return 0;
}

void first_combo(char* dest, char const* number, int len) {
for(int i = 0; i < len; ++i)
dest[i] = chars[number[i] - '0'][0];
dest[len] = 0;
}

int main() {
char combo[256];
char const* number = "237";
int len = strlen(number);

first_combo(combo, number, len);
printf("%s\n", combo);

while(next_combo(number, combo, len)) {
printf("%s\n", combo);
}

return 0;
}

>10 lines

>i can't read
Like 98% of Sup Forums.

Pretty irrelevant speedup for this type of task desu, especially since you aren't really doing anything different algorithmically, the time-complexity is the same

>The recursive solutions are actually slow and brainlet-tier
no they aren't, they do one iteration per combination

What?

OCaml

wtf is a dancefloor simulator?

No I mean what you're implemmenting

>muh optimizing compiler

>Pretty irrelevant speedup for this type of task desu
That's your subjective opinion. Mine is that if you're going to take on these pointless challenges, you might as well take a more interesting approach than the slow and obvious one.

>especially since you aren't really doing anything different algorithmically
That's actually quite a different algorithm.

>the time-complexity is the same
By this logic, quicksort and merge sort are "algorithmically the same" as well.

Oh kek.
First order logic prover yes.
>take FOL formula
>convert to CNF
>apply DPLL

>slow
What kind of computer do you have where you can even notice a difference between your code and his? Are you running a commodore 64?

IMO python isn't very good for data structures because you don't have any pointers.
Sure, you can implement a complete binary tree using some index arithmetic and an array. You can also make a matrix representation of a graph, but that's as good as it gets. If you want to have fun with balanced trees, adjacency list graphs, linked lists, etc. you will need a language with pointers.
Make the switch right now.

Wait no, not DPLL, Robinson.

i've tried similar, should probably finish it

>i can't read, again
I said "the core" is about 10 lines, the core being the function that gets the next combination. Everything else is boilerplate.
int next_combo(char const* number, char* combo, int len) {
for(int i = 0; i < len; ++i) {
if(combo[i] < last_char[number[i] - '0']) {
++combo[i];
return 1;
}else
combo[i] = chars[number[i] - '0'][0];
}
return 0;
}

Literally 10 lines.

also there's this really good book

Knowledge Representation and Reasoning

What?

>they do one iteration per combination
But they do more work on every iteration, hence they are slower.

very nice, i am currently creating my own type system by using type familys and specifying my rules in first order logic on them for a custom edsl of mine.

higher abstractions do have their advantages and are a fine choice unless you really need the performance. c is a decent workhorse but you notice its age and there are often better alternatives nowadays

Honestly it's not very difficult, the only trouble I'm having is that I'm pretty new to functional programming and OCaml in particular, but somehow even with that it's much easier to implement than with Java or C++.

Quick question: What will make my life easier regarding AVL trees: a height field in a node or a balance field?

The algorithm isn't really very different, it takes the exact same number of steps to produce each output. The primary speedup you would gain would just be the lack of stack pushes, imo not a particularly more interesting solution

so it is actually more than 10 lines?

>higher abstractions do have their advantages
I wasn't arguing against abstractions. Just saying that I don't understand the point of pumping out the obvious and slow solution for a mini-coding-challenge type of thing.

any decent compiler will not care for proper recursive functions

>Just saying that I don't understand the point of pumping out the obvious and slow solution for a mini-coding-challenge type of thing.
you said it yourself, what does speed give you in a mini-coding-challenge?

>The algorithm isn't really very different,
It's actually completely different, which you would notice if you understood what it's doing.

>The primary speedup you would gain would just be the lack of stack pushes
The primary speedup you would gain would be from the fact that it does its work in-place. It's like comparing the Haskell "quicksort" one-liner to a real implementation of quicksort and saying that both are O(n log(n)) while ignoring the copious amounts of extra work the Haskell version is doing which renders it useless.

once you reach functional elitism you should try absolute perfect typed representation
Polymorphic recursion, arbitrary rank types, GADTs, cream of the crop functional autism

>what does speed give you in a mini-coding-challenge?
An incentive to implement something more interesting than the obvious recursive algorithm.

Absolutely can't wait.

uni will teach c in second sem which starts in january. I thought of sticking with python till then.

int log = 0;
int tmp = n;
while (tmp /= base)
log++;

I started reading the little schemer and it says (atom? (quote ())) should return #f, but chicken returns true
whats up with that?

Got bored at work - Teacher of CS at HS.

Created some basic VB macros to generate generic comments to students work, separate into different .docs and name for each class.

Made a bunch of "Quizzes" that students do, then FTP their mark straight to my drive on the school network.

Every other teacher sits for like 2-3 hours marking every day, fuck that shit.

I understand what it is doing perfectly well, it sounds like you don't know what the recursive solution is doing. What do you think the difference is? It is just as 'in place' but with the data on the stack vs in your array

Lambda calculus teaser

type 'v expr =
Var of 'v
| Abs of (('v option) expr)
| App of ('v expr * 'v expr)


Notice how we don't even need a variable name on Abs, it's de-brujin encoded
Type safety stops you making a mistake with the debrujin indices too

My bengi

those concepts are actually very useful depending on what you work, also very funny and interesting to learn

It's hard to reason with the c disease of verbosity, stuck in 1970

When are C functions "too long"?

long long long is too much

Is there still a reason to use Lisp nowadays?

yes

When they don't fit in the program cache

When they could be shorter without impacting readability or efficiency

wrong, this accepts 1 as a valid input

>it sounds like you don't know what the recursive solution is doing
It actually sounds like you don't understand what either algorithm does.
One is based on the idea that all the possible character representations for the entire number can be gotten by concatenating every possible character representation of the first digit with every
possible character representation of the rest of the number (a reasoning which applies recursively to the rest of the number).
The other is based on the idea on incrementing a number where each digit is of a different base and with a different set of associated symbols until it overflows.

>accepts 1 as a valid input
>implying this is somehow a problem
The task doesn't specify how to deal with 1, so I just do whatever I like (which happens to be interpreting it as a space). Prove it "wrong".

>C is so SHIT that it doesn't have a fucking pow operator
why are these memers given any attention again?

This is a coding challenge, you have to deal with all edge cases.

Like how you can enter "asd" and your program Segmentation faults

>what's a standard library function

>This is a coding challenge, you have to deal with all edge cases
Total non-sequitur.

says the literal person advocating for speed in a coding challenge

>coding challenge
>challenge
What exactly is challenging about pumping out the slowest and most obvious solution, or about scanning the input string for invalid characters and throwing an error?

isLessThan(x, y) // first arg is lhs
substract(x, y) // first arg is lhs

gee looks convenient

and what exactly is the challenge from pumping out the bug ridden code?

Those functions are not part of C's standard library.

>gee looks convenient
whyDoesntEveryConceivableOperationHaveADedicatedOperator(x, y);

en.wikipedia.org/wiki/APL_(programming_language)
Alternatively, you can provide your non-subjective reasoning why the operations you care about should have dedicated operators, but not hundreds of other potential operations. Looking forward to your objective derivation for the cutoff point where something isn't common enough to be built in.

Again, trust me, I understand this perfectly well, the two are essentially equivalent

When your solution increments normally (then clause of your if) is analogous to an iteration of the for loop inside the recursive function (the yield will return the control flow to the caller instead of your return). Your else clause is similarly analogous to the inner generator finishing, control flow returning to the previous level, and the generator being recreated. Given these replacements the steps each process takes are exactly the same beyond trivialities

int pow(b, e)
{
while (e) b *= e--;
return b;
}

>i'm a total retard
I know, kid.

Coding an exporter for Prometheus to keep an eye out on this horrid beast called EventStore, because my coworkers decided this horrid peace of shit software is a great way to store events for ever and ever and ever using experimental features.

I don't agree with that guy, but this is a pretty piss poor implementation