Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 06-12-2017, 02:16 PM   #1726
just_grindin
Pooh-Bah
 
Join Date: Dec 2007
Posts: 4,216
Re: Programming homework and newbie help thread

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 View Post
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.
just_grindin is offline   Reply With Quote
Old 06-12-2017, 02:17 PM   #1727
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 22,985
Re: Programming homework and newbie help thread

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)?
RustyBrooks is offline   Reply With Quote
Old 06-12-2017, 02:21 PM   #1728
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 22,985
Re: Programming homework and newbie help thread

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.
RustyBrooks is offline   Reply With Quote
Old 06-12-2017, 08:25 PM   #1729
Mossberg
Pooh-Bah
 
Mossberg's Avatar
 
Join Date: Dec 2005
Posts: 5,822
Re: Programming homework and newbie help thread

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.
Mossberg is offline   Reply With Quote
Old 06-12-2017, 08:51 PM   #1730
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 22,985
Re: Programming homework and newbie help thread

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.
RustyBrooks is offline   Reply With Quote
Old 06-12-2017, 09:05 PM   #1731
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

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.
ChrisV is offline   Reply With Quote
Old 06-12-2017, 09:06 PM   #1732
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

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.
ChrisV is offline   Reply With Quote
Old 06-12-2017, 09:41 PM   #1733
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 22,985
Re: Programming homework and newbie help thread

I wouldn't say it's impossible but I'd say it's probably pretty torturous
RustyBrooks is offline   Reply With Quote
Old 06-12-2017, 10:33 PM   #1734
Mossberg
Pooh-Bah
 
Mossberg's Avatar
 
Join Date: Dec 2005
Posts: 5,822
Re: Programming homework and newbie help thread

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.
Mossberg is offline   Reply With Quote
Old 06-12-2017, 10:55 PM   #1735
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

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.
ChrisV is offline   Reply With Quote
Old 06-12-2017, 11:05 PM   #1736
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 22,985
Re: Programming homework and newbie help thread

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)
RustyBrooks is offline   Reply With Quote
Old 06-12-2017, 11:16 PM   #1737
_dave_
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK (or what remains of it)
Posts: 12,204
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Mossberg View Post
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.
_dave_ is online now   Reply With Quote
Old 06-12-2017, 11:17 PM   #1738
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

Quote:
Originally Posted by RustyBrooks View Post
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.
ChrisV is offline   Reply With Quote
Old 06-12-2017, 11:20 PM   #1739
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

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.
ChrisV is offline   Reply With Quote
Old 06-12-2017, 11:53 PM   #1740
_dave_
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK (or what remains of it)
Posts: 12,204
Re: Programming homework and newbie help thread

Pretty sure postgresql does WITH, don't think I've ever used it tho so not sure if same as MSSQL
_dave_ is online now   Reply With Quote
Old 06-13-2017, 12:07 AM   #1741
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

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.
ChrisV is offline   Reply With Quote
Old 06-13-2017, 03:04 PM   #1742
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 21,470
Re: Programming homework and newbie help thread

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.
daveT is offline   Reply With Quote
Old 06-13-2017, 06:59 PM   #1743
lostmypw
newbie
 
Join Date: Mar 2017
Posts: 34
Re: Programming homework and newbie help thread

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.
lostmypw is offline   Reply With Quote
Old 06-13-2017, 11:59 PM   #1744
gaming_mouse
Carpal \'Tunnel
 
gaming_mouse's Avatar
 
Join Date: Oct 2004
Location: taking notes on u (see profile)
Posts: 13,704
Re: Programming homework and newbie help thread

it looks like a correct implementation of fisher-yates:

https://bost.ocks.org/mike/shuffle/
gaming_mouse is offline   Reply With Quote
Old 07-03-2017, 07:07 AM   #1745
leavesofliberty
Pooh-Bah
 
leavesofliberty's Avatar
 
Join Date: Jul 2010
Location: on forum tilt
Posts: 5,613
Re: Programming homework and newbie help thread

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.
leavesofliberty is online now   Reply With Quote
Old 07-10-2017, 02:01 AM   #1746
Jeff W
Carpal \'Tunnel
 
Join Date: May 2004
Posts: 9,479
Re: Programming homework and newbie help thread

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.
Jeff W is offline   Reply With Quote
Old 07-10-2017, 04:35 AM   #1747
plexiq
old hand
 
Join Date: Apr 2007
Location: Vienna
Posts: 1,626
Re: Programming homework and newbie help thread

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.
plexiq is offline   Reply With Quote
Old 07-10-2017, 05:03 AM   #1748
Jeff W
Carpal \'Tunnel
 
Join Date: May 2004
Posts: 9,479
Re: Programming homework and newbie help thread

Quote:
Originally Posted by plexiq View Post
(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.
Jeff W is offline   Reply With Quote
Old 07-14-2017, 08:50 PM   #1749
meekrab
veteran
 
Join Date: Mar 2010
Posts: 2,647
Re: Programming homework and newbie help thread

There are also infinite precision integer and float libraries out there, if you don't care about performance.
meekrab is offline   Reply With Quote
Old 07-26-2017, 11:20 AM   #1750
Noodle Wazlib
just about tolerable
 
Noodle Wazlib's Avatar
 
Join Date: Nov 2015
Location: Drowning in robot chocolate
Posts: 10,174
Re: Programming homework and newbie help thread

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
Noodle Wazlib is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 04:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ę 2008-2010, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online