/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

pastebin.com/BWHgk4ai
stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c
pastebin.com/wapPTt9Y
rosettacode.org/wiki/Parametric_polymorphism#C
github.com/torvalds/linux/blob/master/kernel/fork.c
github.com/torvalds/linux/blob/master/drivers/memory/tegra/tegra114.c
twitter.com/SFWRedditImages

daily reminder that managed language is the future.

Daily reminder that managed language is the present.
It would be pretty depressing for it to also be the future because that would mean no progress was made.

it is the future for the 80s crowd

Not on strings but I'm already changing to stringstream.

r8 my b8
pastebin.com/BWHgk4ai
it's a C algorithm for non-greedy wildcard-based string matching that ignores consecutive whitespace

it's also very bad because i started writing it at 3AM this morning and continued until now which is 7AM and encountered several problems which i kind of just patched up with duct tape instead of actually fixing at their core

i got them all and now it works perfectly but it's still ugly

strcat(rstring, " "); crashes .exe
I'm about to end myself

What is rstring? and does it end with a '\0'?

...

hello jaime

hi and ho

holy fuck

wow i just realized i didn't document this at all
okay to clarify how it works if anyone wants to use it for some reason:

char const *instance is the text you want to match against some pattern
e.g. something like "pixies tickle my anus"

char const *pattern is the wildcard pattern you want to match against
e.g. something like "* * my *"

the return value is a null terminated array of null terminated strings
e.g. in the example cases above it would be an array containing the strings "pixies", "tickle", and "anus", followed by a null pointer to indicate the end of the array

if the match was successful but there were no wildcards, an empty array will be returned, i.e. one containing only a null pointer

if the match was unsuccessful altogether, the array itself will be a null pointer

When is scanf safe to use? Is there something like nscanf?

The format string should be null terminated and none of the destination pointers should be null.

Ideally you should also check the return value of scanf, which tells you how many destination pointers were successfully populated. That way you can recover from an incomplete match rather than simply blindly using uninitialized values.

From buffer overflows when handling untrusted data, I mean.

I don't know but apparently these people do
stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c

is I want to send string in to function for edit and back do just
void function(char *string)
{
.....
}
and call
function(&string)

or do I need
void function(const char *string, char *string2)
{
...
}
and call
function(string, &string2)

The first one if you want to change the string, the second one if you want to make a new one.

>is I want to send string in to function for edit and back do just

confused_anime_girl.jpg

Neither.
If you want to send one string into a function to modify it, you want:
void function(char *string) {
// ...
}

And you'd call it with:
function(string);
NOT with:
function(&string);
In C, strings can't be passed by value. A string, by default, is already a pointer to the beginning of the block of memory it uses. Taking its address would give you the address of that pointer, not the address of the text itself.

You could also do
void function(char const *string, char *string2) {
// ...
}

and call
function(string, string2);
// again, no &

However, this is not just for modifying one string. More specifically, this is for modifying a string (string2) in a way that needs to make use of the data in another string (the one that's just called "string"), but such that modifying that other string is unnecessary (hence the "const").

Still creating graphics to replace the solid lines, as well as figuring out different dry humping positions. Not sure what user controls to add beside speed, but that's down the road a bit regardless

Programming Challenge:

Write a function to simulate a Chinese restaurant process. CRPs are a model of a restaurant with an infinite number of tables that can each seat an infinite number of guests. Basically you start with a single guest at the first table and then the next guest comes in and decides whether to sit with the first guest or sit at a new table. In general guest number n will sit at a new table with probability 1/n or sit at one of the already occupied tables with probability |t_i|/(n - 1) where |t_i| is the number of people at table at table number i.

Example outputs:
>>> crp(1)
[[1]]

>>> crp(10)
[[1, 4, 8], [2], [3], [5, 6, 7, 9, 10]]

>>> crp(20)
[[1, 4, 8, 9, 11, 14, 18], [2, 5, 6, 7, 10, 12, 13, 15, 16, 20], [3, 17], [19]]

>Programming Challenge:
More like
>Do My Homework:

Given an ASP.NET Web App, how hard is it to re-write its functionality into a Web API?

i'm gonna be a jerk and say this:
depend on how deep in the hole your asp web app is.
usually a complete rewrite your logic into web api handler

Ah yes the obligatory "this is homework" post. Here's crp(1000) to show that I've already done this: pastebin.com/wapPTt9Y

What's the easiest way to do parametric polymorphism in C without GCC? Macros or ternary operator?

I hate my job. It keeps me busy 70 hours/week away from home 120 hours/week. They little bit of time i have, I'm paying bills, restocking for another week away, and other errands. I get maybe 2 hours/week to focus on teaching myself programming (to escape the current job and have a job in programming). I've gone through a few Lynda courses, Derek Banas, the new Boston, head first books, for dummies books, etc... I probably know enough to cobble together a couple basic apps. But I don't have time.

TL;DR is there any way to get a paid, one the job training job in programming?

What should I do? (Other than kys)

Use C++.

That's not what I want to do, I'm mostly interested in it for optimization purposes.

rosettacode.org/wiki/Parametric_polymorphism#C

Macros.
But really if you're using C the better and more idiomatic way to do generics is to throw type safety out the window and use void *

Then I need to pass the variable type. I can avoid this by using sizeof() like my original idea.
But say I want to do different things for an int and a char*, then void* is useless.

Then you wouldn't be able to do those different things with the same code anyway, because C has no function overloading -- so I fail to see the purpose of parameterizing homogenous code on type if the fact that it's homogenous will just rule out any functional distinction between the parameterizations.

You could try using functions that take other functions that take void *. It would achieve the same affect of a composite algorithm exhibiting different behaviors depending on its components, without relying on parametric polymorphism or overloading.

>I can avoid this by using sizeof() like my original idea.
The problem with this is that there are many more types than there are sizes. All data pointers, for example, are the same size regardless of the type pointed to.

>All data pointers, for example, are the same size regardless of the type pointed to.
Not necessarily. It's platform dependent - the standard only guarantees that struct pointers are equal, union pointers are equal, and void pointers are the size of the greatest of any of them.

Show your code faggot, otherwise kill yourself

print (f'username: {username}; password: {password}')

Can anyone tell me why this works on Windows but not Linux?

I get a syntax error pointing at the last ' character

I'm using Python 3. How do I alter this for running on Linux ?

What version of python3 is running on the linux box? f-strings are only 3.6+

When in psycharm, I get a syntax red error highlighting at the f

install Haskell

Sorry for phone posting, I went out to clear my head. Im learning java and cant wrap my head around how I should print different length ascii art depending on input. Lets say inputs are o, x and 5. Then output should look something like xooox
oxoxo
ooxoo
oxoxo
xooox

This is simple example to show general idea.

I meant function overloading, sorry.
It is possible though.
int a (char* x);
int b (char x);
int c (int x);
int (*fpa[sizeof(intmax_t)]) (void* x);
fpa[sizeof(char*)] = func_charp
fpa[sizeof(char)] = func_char
fpa[sizeof(int)] = func_int
#define FUNCW(x) ((*fpa[sizeof(x)])(x))

It can be further improved with macros.
We can dereference for all equally large to a given pointer type and then use sizeof and a SIGSEGV signal handler, or misuse the ternary operator. The former is probably not something a compiler will optimize out for us, but it is portable.

use a for loop or two

I'm writing last C assignment for uni.
It's pretty big, for me at least.
I have main, header and secondary .c file for function. My functions.c is over 100 lines long now.
Is that fine for non main .c files? Are there any undocumented rules on how long these things should be?

100 is a good size, don't worry about it.

Guest 1 sits at table 1

Guest 2 will sit at a new table with probability 1/2, or at table 1 with probability (t_i = 1, n - 1 = 1) 1. Sum of probabilities = 1.5. Formula doesn't work.

>he thinks 100 lines is a lot for a non-main .c file
Here's a random source file from the Linux kernel:
github.com/torvalds/linux/blob/master/kernel/fork.c

Here's another:
github.com/torvalds/linux/blob/master/drivers/memory/tegra/tegra114.c

Files can get as large as they need to. As long as all of the functions in the file are logically related, it's fine.

It's a two step process. Guest n flips an biased coin, if it's heads he sits at a new table, if it's tails he rolls a weighted die.

To detect int32_t, you can do (*(((&x)*0)+malloc(sizeof(int32_t))) = -1) == -1. Then you only have to worry about uint32_t. Avoid this. Then just dereference it and check sizeof. If it's void* you're fucked though, so don't do that.

>r8 my b8
Is this what this general has come to?

>blatantly leaking memory
ree

~gcceso!

...

can someone redpill me on why all pointers in c++ dont just delete themselves when they go out of scope with zero references?

>with zero references?
where are you keeping track of these?

Parsing HTML with regex because I'm too lazy to learn how to use the API

Smart pointers can do this

An API for what?

Reddit for muh porn

Fuck, forgot to free(). You can use a temp variable. But this is long past "muh optimizing compiler" territory, so it's probably no good idea.

why not use html parser library?

Python has an API binding. It's quite easy to use, just copypaste from the examples.

>out of scope
Consider the scenario where you need to dynamically allocate that is the return value for a function

>reference count is 0
Unneeded bloat when you can preform manual memory management at a much greater speed

What should I know about ActionScript 3

That you should never use it.

It needn't be a two step process though. If the There is an (n-1) / n probability that he does not sit at a new table. Multiply this by each of your other probabilities and you can check everything in one roll. And you know what? The nice thing is that the n-1 term will cancel out.

(t_i / (n-1)) * ((n-1) / n) = t_i / n

Now we try this again...

Guest 1 sits at table 1
Guest 2 can sit at table 2 for 1/2 probability or table 1 for 1/2 probability. He sits at table 1.
Guest 3 can sit at table 2 for 1/3 probability or table 1 for 2/3 probability. He sits at table 2.
Guest 4 can sit at table 3 for 1/4 probability, table 2 for 1/4 probability, or table 1 for 2/4 (or 1/2) probability...

What if pay is good

this.
flash is dying already.

It creates a "cobol situation" though

but flash doesn't power banking system like cobol

That's not right.

Guest 1 sits at table 1
Guest 2 sits at a new table with probability 1/2 or sits at table 1 with probability (1 - 1/2)(1)
Guest 3 sits at a new table with probability 1/3 or, if guest 2 sat at table 1, sits at table one with probability (1 - 1/3)(1), and if guest 2 sat at a new table, sits at table 1 with probability (1 - 1/3)(1/2) and at table 2 with probability (1 - 1/3)(1/2)
etc.

Let an integer x represent the input. You can use java for this program, but it'd be easier to do in Python in my opinion.
Auto-generate the strings up through x/2 and put them in an array(of size x/2 obviously)
Then, simply iterate through the array, printing each value, and then iterate backwards doing the same thing, but skip the last index in the array.

user, the 1 - x you kept putting in front of those is equivalent to (n-1) / n. Crunch the fucking numbers.

I wrote them like that so that it'd be clear what's going on. I know you can write it as a single roll, but conceptually it's easier to think about and to implement as two.

_Generic or void pointers

in c, but you should be able to transfer it pretty easily
void x(int s, char x, char o) {
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
if (i == j || j == s-i-1)
printf("%c", x);
else
printf("%c", o);
}
printf("\n");
}
}

The key is checking if you're on the main diagonal (i == j) or if you're on the opposite of the main diagonal (j == width - i)

use a struct with bools (bitfield) dynamically generated via macros
...fuck, i should have stayed away from c++

oh, that's rough. probably doable though

You could just use a 2d char array on second thought

I have a table like this
service1 comp1
service1 comp2
service2 comp3
service3 comp4

how do I count that how many companies offer one service (in sql)

>printf("%c", x);
putchar(x);

Another solution: treat each line as one string, then do string[linenum] = x; string[-linenum] = x.
SELECT COUNT(company) FROM name WHERE SERVICE = 'service1'
I think, not sure

complete dingus beginner here,
could someone help me understand how to write this in scala:
i want do do this function:
def lookupIndex(xs: Vector[(String, String)])(key: String): Int =

using indexWhere, which works fine if i do
xs.indexWhere(._._1 == key)
but how would I write it without using dots if possible? I don't get how to access the first string in the pair, I tried xs(_)._1 but that's obviously wrong

fuck VHDL though

Just for giggles, I drafted up a shitty-code version
private void patternGen(int x) {
String[] result;
if (x % 2 == 0) { result = new String[x/2]; }
else { result = new String[x/2 + 1]; }
for (int i = 0; i < result.length; i++) {
result[i] = "";
char[] str = new char[x];
str[i] = 'X';
str[str.length-(i+1)] = 'X';
for (int j = 0; j < str.length; j++) {
if (!(str[j] == 'X')) str[j] = 'O';
}
for (int k = 0; k < str.length; k++)
result[i] += Character.toString(str[k]);
}
for (int i = 0; i < result.length; i++) {
build.println(result[i]);
}
for (int i = (result.length -2); i >= 0; i--) {
build.println(result[i]);
}
}

ignore the "build.println" statements, I don't like using the console so I have a gui I run things through

Holy shit nigger what are you doing

def pattern(a, b, n):
for i in range(n):
for j in range(n):
if i == j or i == n - j - 1:
print(a, end='')
else:
print(b, end='')
print()


>>> pattern('x', 'o', 11)
xooooooooox
oxoooooooxo
ooxoooooxoo
oooxoooxooo
ooooxoxoooo
oooooxooooo
ooooxoxoooo
oooxoooxooo
ooxoooooxoo
oxoooooooxo
xooooooooox

Why?

Too verbose.
has lot of annoying quirks like if the type is bit you have to use single quotes like '1' but if it's vector of bits you have to use double quotes like "101".
end if;
end if;
end process;
end your_life;

The way entities and libraries have to organized is retarded.
Almost makes you want to write some common lisp reader macros to translate Lisp into vhdl but making it so that debugging is easy would be too much work.
No good open source tools.

huh
your solution is (obviously) a lot better
I am a little drunk, sorry

brain is like a l-value with a place in physical reality and functioning by its laws and consciousness is like a r-value something evaluated from the physical interactions in the brain and is temporary in nature

Let's say you have an open source implementation of something, you yourself don't know anything about the original implementation except what the public knows, you host the project and someone else makes a contribution utilizing insider knowledge but you don't know this at the time, later you find out that they had known private information, what do you do? What can be done to your project if someone else finds out that the person who contributed knew something?

sepples has given you brain damage

Does anyone here know how to make a morph animation in android? I want a drawable circle to morph into a rectangle but I've had no luck animating it, either it dissapears and reappears as a rectangle or I have a rectangle overlay over the circle.

Found the web dev brainlet.

If I implemented the Game Of Life properly and start every cell as alive, according to the rules, the next turn, every single cell should have died, right?

(print "0\n1\n2\n4\n5\n")

Yes.

Is your grid double buffered?
That is, do you have two grids, one to read current cell status from and one to write new cell status to, which you then switch at the end of the cycle?

If not, you're accidentally making the cell update order matter; cells updated later are reacting to the new statuses of neighboring cells updated prior.

>But say I want to do different things for an int and a char*, then void* is useless.
You pass in a function pointer that takes a void* but is defined for the actual type. Anything else would not be parametric polymorphism.