Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

06-12-2017 , 02:16 PM
Edit: Just wanted to add the below is my opinion and I'm learning as well so wanted to put my thoughts out there for comment.

Quote:
Originally Posted by Mossberg
I've ran into another little snag. I have an EqualWins view for my Tournament Database which lists rows where player1 has the same number of wins as player 2. Now, I want to pair players with equal number of wins for the next round of the tournament, but I only want to select each player once.

EqualWins:

id1 | id2
-----+-----
50 | 49
56 | 50
56 | 49
56 | 54
54 | 50
54 | 49
52 | 51
55 | 51
55 | 52
55 | 53
53 | 51
53 | 52

I have messed around with LIMIT, GROUP BY, and DISTINCT but can't quite get the job done. Any tips?

EDIT: this is what I currently have:

SELECT DISTINCT a.id as id1, b.id as id2
FROM playerstandings AS a, playerstandings AS b
WHERE a.won=b.won AND a.id>b.id;
Distinct works to return unique records across all of your return values and so it will return all values in your case because record 55|53 is different than record 55|56.

Link below with the same answer in case I stated something incorrectly.

https://stackoverflow.com/questions/...rns-duplicates.

It would seem to me that the pairing part should be done in an application logic outside of SQL. I would just return IDs where wins are equal to your program and then have it do the pairing.

That being said I'm fairly certain most DBMS systems come with regular programming facilities that you could use if you wanted to keep it all within the same procedure but I don't know if using database manipulation techniques is what you want to do.

Last edited by just_grindin; 06-12-2017 at 02:25 PM.
Programming homework and newbie help thread Quote
06-12-2017 , 02:17 PM
So you want all the cases where 2 players have the same number of wins?

What's wrong with your current query (and I don't think the distinct is necessary)?
Programming homework and newbie help thread Quote
06-12-2017 , 02:21 PM
Oh, I see what you mean. Hmm. How about

Code:
select a.id, min(b.id)
from playerstandings a, playerstandings b
where a.won = b.won and a.id != b.id
group by a.id
This rather arbitrarily picks the lowest playerid for a player with the same number of wins.
Programming homework and newbie help thread Quote
06-12-2017 , 08:25 PM
Thanks guys.

Rusty, your suggestion definitely gets me closer, but still not quite what I'm looking for:

id1 | id2
-----+-----
56 | 49
54 | 49
53 | 51
52 | 51
55 | 51
49 | 50
50 | 49
51 | 52

id2 column should only contain unique entries, as id1 column does.
Programming homework and newbie help thread Quote
06-12-2017 , 08:51 PM
I don't think there's really a way to do that with sql. For one thing, it might not be possible to even make such matchups. Consider what happens if there are only 3 players and all 3 have the same number of wins. A plays B and C plays who?

I'd do it outside of SQL personally. I'd get a list of all the matchups like you had in your original query (minus the distinct) and then picks matchups, eliminating rows that were no longer possible.
Programming homework and newbie help thread Quote
06-12-2017 , 09:05 PM
Do you want very strictly players with equal wins, or do you want to pair all players with the most appropriate opponent? For instance, if there are three players with 5 wins, should one of them be paired with someone with 6 wins or 4 wins?

Also, what version of SQL are you using? My solution would involve using SQL Server's OVER (PARTITION BY) clause, which I think works differently in other SQL versions.
Programming homework and newbie help thread Quote
06-12-2017 , 09:06 PM
I do agree with Rusty that SQL isn't really the most appropriate platform to do this on, because it's not really a set-based operation. It can probably be done, though.
Programming homework and newbie help thread Quote
06-12-2017 , 09:41 PM
I wouldn't say it's impossible but I'd say it's probably pretty torturous
Programming homework and newbie help thread Quote
06-12-2017 , 10:33 PM
That helps. I'm doing this project through Python's DB-API so we aren't necessarily limited to only SQL statements. I'll see what I can do with Python to get the desired records.

ChrisV, I'm looking for pairings with the most appropriate opponent. As for which version of SQL I'm using--I'm not sure. The course has us using postgresql through a virtual machine.
Programming homework and newbie help thread Quote
06-12-2017 , 10:55 PM
SELECT a.id as id1, b.id as id2
FROM (SELECT ROW_NUMBER() OVER (ORDER BY won DESC, id) rnum, * FROM playerstandings) a
JOIN (SELECT ROW_NUMBER() OVER (ORDER BY won DESC, id) rnum, * FROM playerstandings) b ON b.rnum = a.rnum + 1
WHERE a.rnum % 2 = 1

I think that should work, and I think that syntax is right in postgres. Not completely sure on both counts

Just to explain briefly: ROW_NUMBER() OVER gives each row a numbering based on the ordering in the brackets, I've then aliased the resulting row number to "rnum". So it joins the players by row numbers that are one apart, 1v2, 2v3, 3v4, 4v5 etc. Then the WHERE clause makes a's rnum be odd, so we end up with 1v2, 3v4, 5v6, etc.
Programming homework and newbie help thread Quote
06-12-2017 , 11:05 PM
If I understand that right, it's going to sometimes pair a guy with a person who has less wins than him, in cases where there aren't even numbers of wins. It's a little bit like if you ordered all the players by # of wins and then said, OK, #1 plays #2, #3 plays #4, #4 plays #5 and so on. Is that right?

It's kinda neat to see that implemented in SQL, but, this would be like the simplest SQL query ever, munged into shape by like 1 simple line of python. I would probably never use a query like this, unless I was using some kind of 3rd party system where my only inputs were SQL queries (I have dealt with stuff like that before, like places where you do mail merges by supplying a SQL query and no other option)
Programming homework and newbie help thread Quote
06-12-2017 , 11:16 PM
Quote:
Originally Posted by Mossberg
As for which version of SQL I'm using--I'm not sure. The course has us using postgresql through a virtual machine.
That's what he's asking. Postgresql after something like (not certain) 9.0+ supports "windowing" (OVER x [PARTITION BY y])

Also without thining about it too much, prob agree with Rusty and this would be a problem for "SQL to pull the player/winrate data, application logic to sort it out" for me usually.

Last edited by _dave_; 06-12-2017 at 11:22 PM.
Programming homework and newbie help thread Quote
06-12-2017 , 11:17 PM
Quote:
Originally Posted by RustyBrooks
It's a little bit like if you ordered all the players by # of wins and then said, OK, #1 plays #2, #3 plays #4, #4 plays #5 and so on. Is that right?
It's exactly that.

Quote:
It's kinda neat to see that implemented in SQL, but, this would be like the simplest SQL query ever, munged into shape by like 1 simple line of python. I would probably never use a query like this, unless I was using some kind of 3rd party system where my only inputs were SQL queries (I have dealt with stuff like that before, like places where you do mail merges by supplying a SQL query and no other option)
Agreed. The query as it stands isn't horrible, but then the next thing is going to be "hey I notice that when there's an odd number of players, it just leaves the last one off, can we fix that?" and so forth. And before you know it you'll end up with an abomination, because SQL was designed to manipulate sets, not ordered lists.

Last edited by ChrisV; 06-12-2017 at 11:22 PM.
Programming homework and newbie help thread Quote
06-12-2017 , 11:20 PM
btw, normally I would alias the repeated subquery:

WITH orderedplayers AS (
SELECT ROW_NUMBER() OVER (ORDER BY won DESC, id) rnum, * FROM playerstandings
)
SELECT a.id as id1, b.id as id2
FROM orderedplayers a
JOIN orderedplayers b ON b.rnum = a.rnum + 1
WHERE a.rnum % 2 = 1

but I have a hunch WITH might be a SQL Server thing and was too lazy to look up whether it works in postgres.
Programming homework and newbie help thread Quote
06-12-2017 , 11:53 PM
Pretty sure postgresql does WITH, don't think I've ever used it tho so not sure if same as MSSQL
Programming homework and newbie help thread Quote
06-13-2017 , 12:07 AM
I've actually frequently encountered performance problems when using WITH instead of a CTE, but I use it anyway for non performance critical queries because it's easy.
Programming homework and newbie help thread Quote
06-13-2017 , 03:04 PM
WITH in PostgreSQL is a CTE. IRT optimization fences, that's a known problem, but the Postgres team also refuses to add query hinting.

For anyone who hasn't tried out windowing, it's so amazing once you get used to it. It's really hard to use MySQL knowing it doesn't exist, to be honest.

Of course, the syntax in Postgres is different than what ChrisV shows here. There is a way to simply grab the max or min of each window in the partition over section, using (I think) the DISTINCT ON clause.

And finally, you probably shouldn't do views on views, or rather, don't do functional queries over views, like windowing, aggregations, limit 1 clauses, etc. It's a lot of work for the database and often confuses the query planner. I would just write an entirely new view for what Mossberg is attempting.
Programming homework and newbie help thread Quote
06-13-2017 , 06:59 PM
Code:
function shuffle(cards) {
  for (let i = 0; i < cards.length; i++) {
    let ran = Math.floor(Math.random() * (cards.length - i)) + i;
    [cards[i], cards[ran]] = [cards[ran], cards[i]]
  }
}
is this actually a random shuffle? I remember there being some gotchas with shuffling.
Programming homework and newbie help thread Quote
06-13-2017 , 11:59 PM
it looks like a correct implementation of fisher-yates:

https://bost.ocks.org/mike/shuffle/
Programming homework and newbie help thread Quote
07-03-2017 , 07:07 AM
You can use the STL's shuffle since C++11

http://en.cppreference.com/w/cpp/alg...random_shuffle

Also, next_permutation and its sister prev_permutation are interesting if you want to exhaust a deck of cards.

random_shuffle is deprecated. Boost has options as well.
Programming homework and newbie help thread Quote
07-10-2017 , 02:01 AM
I am learning C++ and ran into an issue with Taylor Series Approximation of e^x to n terms. Here is code for the relevant functions:

Taylor.cpp
factorial.cpp

The Taylor function starts diverging from the correct answer as x and n increase. I believe the problem is in the floating point division. Do I need different types? I think I can at least prevent the function from returning nonsensical with some error handling:

Code:
if |term n - term n-1| > |term n-1 - term n-2|
	return approximation at term (n-1)

Last edited by Jeff W; 07-10-2017 at 02:25 AM.
Programming homework and newbie help thread Quote
07-10-2017 , 04:35 AM
This is probably just your factorial overflowing. This would happen at n=21 (64bit) or n=13 (32bit), not sure what integer width is used by default in your factorial method. (Your factorial is returning uint64, but using a regular int during calculation.)

Last edited by plexiq; 07-10-2017 at 04:42 AM.
Programming homework and newbie help thread Quote
07-10-2017 , 05:03 AM
Quote:
Originally Posted by plexiq
(Your factorial is returning uint64, but using a regular int during calculation.)
Bingo! This was the problem. Forgot to switch factorial to uint64 I can at least get accuracy for bigger n now. Thanks.
Programming homework and newbie help thread Quote
07-14-2017 , 08:50 PM
There are also infinite precision integer and float libraries out there, if you don't care about performance.
Programming homework and newbie help thread Quote
07-26-2017 , 11:20 AM
I've never written programs that run as their own thing on windows, and I'm curious how to implement this:

Quote:
if you don't trust online apps, write your own program: The windows API to do this is: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

Code:
while(1) { mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, NULL); Sleep(60000);}
It's from a thread on Reddit about programs for moving the mouse slightly every x minutes.

Just curious what implementation would be like, what library I'd have to use, also if I have bash for windows if I could write and compile it there without installing visual studio
Programming homework and newbie help thread Quote

      
m