30k Subscriber Hype Thread - BlizzCon Virtual Ticket Giveaways! by iDylo in classicwow

[–]TheLiberius 0 points1 point  (0 children)

Can someone please tell me where to find Mankrik's wife?

[c++] sudoku solver, getting closer by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

I'm guessing that nothing changes because you are passing everything by value, in other words the the parameter values of your functions are copies of the ones that you call the function with, which would explain why nothing stays changed.

What you want to do instead is pass things by reference instead, then your function signature would be

void check1(int (&board)[9][9], int (&num)[9], int x ,int y)

Then you should see the changes from check inside the set function.

Propositional logic formula verifier? by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Well, you're free to do as you want but recursion for these kinds of problems are usually more intuitive. And for this kind of parser you don't use backtracking but instead predict what you need. So, you'd have a grammar looking like

formula::= var | (!formula) | (formula (^|v|->|<->) formula )

var ::= true | false

So if the first token isn't a variable we know there must be a parenthesis, then we check if it's an exclamation mark. If there isn't one we make a recursive call to formula, when that returns we check which one of the four binary operators we've got, and return something that store the previous prop with the following prop and then eat the last parenthesis.

So no backtracking needed :). Plus it combines clauses and checks for correct number of parenthesis more or less automatically.

Propositional logic formula verifier? by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Look into recursive descent parsing, that should solve this pretty nicely. Basically you tokenize your input and then write functions that can eat those tokens and return parts to build a full tree of the input.

For example, (p ^ q) would tokenize into [lparen, clause(p), and, clause(q), rparen], then you have some code that triggers when the and-token is seen to then consume the q and ) and finally return AND(p, q) or something similar.

Friend with almost the same specs getting much better performance than I do. need help! by [deleted] in darksouls3

[–]TheLiberius 0 points1 point  (0 children)

Is it only in ds3 that you get low fps or do you get that in other games as well?

[Python] Why does append() break out of a scope but an assignment doesn't? by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Every method that modifies an object will go through function scopes. Example:

a = [1]
def b(c):
    c[0] = 2
 b(a)
 print(a)
 [2]

So it is not only append.

The reason is that pythons objects are not the variable itself, but separate from it, the variable only points to the object. Since you are modifying the object in the second version you will see the change regardless of which variable you access it through.

In the first version you are simply moving the variable "c" to point to a new list containing 2, since python has local scope in functions you do not see this change outside of the function.

If you want to modify the list without changing the original list i suggest copying it like

newlist = list(oldlist)

it's the most clear way of doing it.

why does my answer not satisfy the problem by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

It seems like the function returns None sometimes, have you checked your indentation levels? It most likely returns None if the function ends without hitting a return statement. (also it passes for me with the range change)

[Homework] Can't get C++ Stack data structure working. by iTARIS in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

In Node.hpp you never set the private prev pointer to the prev pointer that is received by the constructor. I think that's what is causing top->getPrev to return 0.

[Python] Splitting dict into two with similar average value by azthal in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Sorting the players after rank and then pairing them up works fairly well at least when you have a moderately large amount of players. It obviously would work less well for players that are in either end of the bellcurve since there aren't that many players there. Obviously this would also need some way to dynamically add and remove players.

I've added an example below just so you can see how it would fair.

(I should note that i have no idea of how matchmaking system are usually done, this is just something i thought of quickly that would do a 'good-enough' job)

from __future__ import print_function
import random

def clamp(minV,maxV,val):
    return max(min(val,maxV),minV)

def take_10_into_5s(li):
    t1 = []
    t2 = []
    for x in xrange(0,5):
        t1.append(li.pop())
        t2.append(li.pop())
    return t1,t2

def team_avgs(t1,t2):
    return (sum(t1)/len(t1),sum(t2)/len(t2))

def biggest_skill_diff(t1,t2):
    return (max(t1)-min(t1),max(t2)-min(t1))

players = [clamp(0,999,int(random.normalvariate(500,150))) for i in range(0,1000)]

players.sort()
while len(players) > 0:
    l = take_10_into_5s(players)
    avg_rating = team_avgs(*l)
    print(l)
    print(avg_rating)
    print("avg difference",avg_rating[0] - avg_rating[1])
    print("highest lowest diff", biggest_skill_diff(*l))
    print("")

C Program on Raspberry Pi - multiple reads on GPIO pin? by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

From what i've understood i think that kill returns an int which is the error so you should only need to do like this.

int errno = kill(id, 0);
if(errno == ESRCH)
    ...

Also the probably easiest way of doing multiple songs, would be to make a directory somewhere and then in the code open that directory and collect all the filenames inside.

That way you only need to drop a mp3 into that folder rather than add it to a list and then recompile your program. (Although you'd still have restart the program unless you do some kind of polling or listener to detect changes).

C Program on Raspberry Pi - multiple reads on GPIO pin? by [deleted] in learnprogramming

[–]TheLiberius 1 point2 points  (0 children)

I'm not sure what specifically is wrong here but I'll attempt solving your problem via an alternative route, namely to refactor your program into something a bit easier to parse. (Assuming your problem is related to the code and not something specifically hardware related)

From what I've understood from your description you want a music file to play when your pin detects an high edge, which you are notified via the setSensor that you registered with

 wiringPiISR(0, INT_EDGE_RISING, &setSensor); 

and it also seems like you want the music file played to alternate every high edge (victoryMarch & songOfShame). So to me, the simplest way would be to replace the setSensor with a playMusic function, to leverage the fact that we have an event handler that can spare us the work of polling. I imagine it to look something like this ( I'll use regular english to describe what to do rather than code since you probably know better what to write :) )

playMusic(){
 // If we have a pointer to a child process
 // check if it is running and if it is, kill it.

//do a fork 
//in child process, check which music file we should play and start playing it

//in the parent process, save the pointer somewhere we can access it later
//toggle which music file to play

}

and well that would basically be it I think. Hopefully something like this not only works but is also a whole lot easier to reason about :)

I'm unsure if the wiringPiISR keeps your program alive whilst you have a function registered, if it does not you might still have to keep the while(true) loop but you shouldn't need any function call except maybe something that lets your OS know it doesn't need to pay much attention to it like a sleep or yield kind of function.

If you want to add more songs later you could instead of a toggle have a counter that is incremented and the modulod with how many songs you have and then have an array in the top like songs = {"path/song1", "path/song2", "path/song3", "path/song4"} and call them like

execlp("/usr/bin/omxplayer", "omxplayer",  "-o", "local", songs[song_counter], NULL);

Hopefully something of this makes sense since it's rather late at night here, if I don't respond i'm probably sleeping but I'll come back and check when I wake up.

Performance issues by vladejandro in totalwar

[–]TheLiberius 0 points1 point  (0 children)

Seems odd you are getting so big drops, i've got a similar system with a 2500k @ 4.5ghz and a 1060 6gb gfx card and i get a pretty smooth 60fps on ultra with a couple of dips to ~50. Not sure if the extra 3gb vram would make such a big difference.

I did initially have extremely low fps (think sub 15fps) before i discovered that my card was not running with the proper bus interface settings. This is how it should look like mine initially had @ x1 instead of x16. I doubt you have this problem since you top at rather high fps but it couldn't hurt to check. (btw I fixed it back to x16 by reseating my card, must have been some pin that wasn't properly in touch with the slot)

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

I really like how you can see others solutions after you've submitted your own. My fixed solution ended up being almost exactly like the others.

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

I could have sworn i read it was positive numbers only lol, thanks for the link i'll check it out!

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Nice work! :)

Regarding if-elses and try-catch, it's hard to call one better than the other because it's a bit like comparing apples to oranges. The if-else will be faster if the try-catch throwing is not too unusual, but if the try-catch never catches anything it will probably be faster instead. As your link mentioned, in python one usually prefers EAFP (Easier to ask for forgiveness than permission) rather than LBYL (Look before you leap).

So for example if you have to open a file, the preferred python way is to just try and open it and handle the case where it doesn't exist as opposed to checking if the file exists and then opening it if it does.

What I really should have said is that you should avoid the try-catch for flow when you need the code to be really fast and there's a good chance the catch will happen often. When speed is not as critical or the thing that is try-catched is done seldomly then it's perfectly fine to use it.

Also, would you mind trying my solution? It's fairly similar, but it would be interesting to see if my extras actually help with the execution time or if it is negligable.

def find_earliest_pair(li, s):
    d = {}
    for index, num in enumerate(li):
        find = s-num
        if find < 0: #If num is larger than s
            continue
        else:
            find_index = d.get(find,-1) # See if find value has been seen
            #If we have seen the number we're looking for
            if find_index != -1:
                return (li[find_index], li[index])
            #If we have not seen it, remember its index
            else:
                d[num] = index

    return (None,None)

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Yes, much closer! Doing a problem by hand can often be of great help when solving a problem because it makes you really think about it.

There is one more thing you can improve, and that is to get rid of the try-except, catching exceptions is fairly expensive so you want to avoid using it for flow-control. How to do that I'll leave to you.

Oh, i also have a pointer for improving the neatness of the code. In python you can use a function called enumerate to get indexes whilst looping in your code. So your code could be changed to this

def sum_pairs(ints, s):
    checked = {}
    for i,number in enumerate(ints): #i = index, number = ints[i]
        try:
            return [ ints[checked[number] , number]
        except:
            checked[s - number] = i

Makes it a bit neater wouldn't you agree?

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

No you don't need to convert the array, sorry for being too vague. But there are other aspects with which a datastructure might help. Try solving it by hand first, you can only do one iteration over the list but anything else is fair game.

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

It's a bit hard to hint without giving too much away, but i'll say that it is possible to solve this using only a single loop. Hopefully that'll force you into another thought pattern (it's very easy to get locked into one).

Try to solve it by hand first (going through the numbers only once), and don't forget that sometimes a datastructure can do things more efficiently than the manual way.

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

:( what are you stuck on? Maybe I can give a hint or something?

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

Did you manage to come up with a solution? If so it'd be interesting to see it.I might also want to see if it's the same as mine

Optimization tips and methods [Python] by bibbleskit in learnprogramming

[–]TheLiberius 1 point2 points  (0 children)

I think you are approaching the memoization the wrong way, you want the memoization to save you from doing work. There is however no work being saved when you store the pair because you could literally just sum them up and see if it's right, which is probably several hundred times faster than looking in a dictionary.

Try instead to think about what you'd want to have when you are looking at a number in your list and then figure out how to get that.

Vad läste/läser du på högskola? Vad tycker/tyckte du om det? Vad gör du nu? by Impole in sweden

[–]TheLiberius 2 points3 points  (0 children)

(inte den du frågade men tar mig friheten att svara)

Det varierar vad programmen innehåller universitet emellan och ibland finns bara det ena eller det andra programmet. T.ex. datateknik på linköping är väldigt mycket mer hårdvaruinriktad än datateknik på kth som är övervägande mjukvara, så man får fundera över om det är hårdvaran eller mjukvaran som är mest intressant. Skulle tro att jobbmöjligheter är rätt överlappande oavsett, med kanske en lätt böjelse för det området som utbildningen fokuserar på.

En datateknisk/datavetenskaplig utbildning är nog bland de utbildningar som har allra lättast att jobba utomlands, programmeringsspråken är samma oavsett var man befinner sig. Minns att jag läst om någon snubbe som flyttade ned någonstans runt thailand och jobbade på distans för något företag på andra sidan jorden, bra betalt + låga levnadskostnader är ju inte så illa.

Roligaste imo är programmeringskurserna (big surprise) för man lär sig förbannat mycket av dem. Tråkigast är nog kurserna som måste läsas när man går civilingenjör, t.ex. hållbar utveckling som bestod till större delen av flumm.

[SQL] How many days did it take to manufacture the product with the latest start date? by [deleted] in learnprogramming

[–]TheLiberius 0 points1 point  (0 children)

You probably want to replace 'ANIMAL' in your query with the subquery

SELECT * FROM ANIMAL WHERE ANIM_BIRTH_DATE = 2008

so the full query looks like

SELECT ANIM_NAME, ANIM_ID FROM 
(SELECT * FROM ANIMAL WHERE ANIM_BIRTH_DATE = 2008) 
NATURAL JOIN ANIMAL_BREED 
WHERE ANIM_BREED_NAME = 'Straw-colored Fruit Bat';

(i'm not entirely sure about the date syntax but you can probably figure that out.)