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 12-03-2014, 08:25 AM   #126
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

Um, I assume a has to be >= 1? Otherwise the search space would be infinite.

The answer to this is that instead of trying to do everything procedurally all at once, you cache the stuff you need beforehand. In this case, from what I can see, you're trying to only check the cubes that have the right final digit such that they might sum to the correct thing. The right way to go about this is to construct 10 lists of cubes divided up by what they are mod 10. Something like this (C#):

Quote:
function FindTriplets(ulong c) {
List<ulong> cubes = new List<ulong>();
List<ulong>[] lists = new List<ulong>[10];
ulong cSquared = c*c;
ulong root = 1;
ulong cube = 1;
while(cube < cSquared) {
cubes.Add(cube);
lists[cube%10].Add(cube)
root++;
cube = root*root*root;
}
[rest of implementation]
}
That way for each value of a, you can calculate the difference between a mod 10 and c mod 10 and just look in the list that has potential matches.

Obviously there are better ways to optimise this, but yeah from a conceptual point of view, if you're finding stuff awkward to do in-place, it might be a sign that you should generate a list of calculated values first and work with that instead.
ChrisV is offline   Reply With Quote
Old 12-03-2014, 09:37 AM   #127
iosys
old hand
 
iosys's Avatar
 
Join Date: Sep 2011
Posts: 1,754
Re: Programming homework and newbie help thread

cool stuff ^^
iosys is offline   Reply With Quote
Old 12-03-2014, 11:41 AM   #128
NiSash1337
Carpal \'Tunnel
 
NiSash1337's Avatar
 
Join Date: Dec 2010
Location: Germany
Posts: 9,189
Re: Programming homework and newbie help thread

Quote:
Originally Posted by iosys View Post
NiSash1337 why don't you approach the problem in steps.

1. Figure out how to code in java a way to check if three variables; a,b, and c are equal for your equation below.

Example using your first example (1.0, 2.0, 3.0),
in java a^3 + b ^ 3 = c^2, replace them and write code to test if this returns true or false... 1^3 + 2^3 = 3^2 by a method like... public boolean checkEquation(a,b,c) {...}

2. Now that you have a method for returning true or false, write 3 for loops with inputing into the method like...


you could also just remove the if true checks and output in the method the system.out.println.

I think that is how to make it work and have the code sort of clean but I'm not a math wizard.
I had a lot of different algorithms, 3 loops is obviously really inefficent. I had one using c^2 and b ^3 and then testing for a. But that was inefficent too. The nex best way was just to let a and b run but I knew it wasn`t efficent as well.Took me a while but then I figured out that the last digit of every squarenumber always ends with 0, 1 ,4 ,5 ,6 or 9 . So the digits of a ^3 + b ^3 have to be one of these numbers to even be a squarenumber.

Um, I assume a has to be >= 1? Otherwise the search space would be infinite.

Quote:
The answer to this is that instead of trying to do everything procedurally all at once, you cache the stuff you need beforehand. In this case, from what I can see, you're trying to only check the cubes that have the right final digit such that they might sum to the correct thing. The right way to go about this is to construct 10 lists of cubes divided up by what they are mod 10. Something like this (C#)
Yes exactly it has to be >=1 and that`s what I`m doing. Now I`m better with Math than I`m with programming and I have idea of C#. But both of you put me on the right track I think, I`m gonna check it out later when I got time.
NiSash1337 is offline   Reply With Quote
Old 12-03-2014, 12:14 PM   #129
iosys
old hand
 
iosys's Avatar
 
Join Date: Sep 2011
Posts: 1,754
Re: Programming homework and newbie help thread

Quote:
Originally Posted by NiSash1337 View Post
I had a lot of different algorithms, 3 loops is obviously really inefficent. I had one using c^2 and b ^3 and then testing for a. But that was inefficent too. The nex best way was just to let a and b run but I knew it wasn`t efficent as well.Took me a while but then I figured out that the last digit of every squarenumber always ends with 0, 1 ,4 ,5 ,6 or 9 . So the digits of a ^3 + b ^3 have to be one of these numbers to even be a squarenumber.

Um, I assume a has to be >= 1? Otherwise the search space would be infinite.



Yes exactly it has to be >=1 and that`s what I`m doing. Now I`m better with Math than I`m with programming and I have idea of C#. But both of you put me on the right track I think, I`m gonna check it out later when I got time.
This is why i wrote i'm not a math wizard after i edited it.

I think recursion would be the answer to making the problem efficient because I did do something like this way back in school.

IDK its a cool problem but i also hate doing these unless i'm forced into it. haha
Definitely think you will love java though because its a great language.
iosys is offline   Reply With Quote
Old 12-03-2014, 01:37 PM   #130
NiSash1337
Carpal \'Tunnel
 
NiSash1337's Avatar
 
Join Date: Dec 2010
Location: Germany
Posts: 9,189
Re: Programming homework and newbie help thread

Your post still helped me tho, doing the checking in a Method is great. I`ve only been learning Java for 4 weeks so everything is quite new to me, but gonna do some reading on Lists/Recursion(We had it briefly in one of our lectures) and I learned that I prob should have more methods instead of having everything in one place.

Last edited by NiSash1337; 12-03-2014 at 01:44 PM.
NiSash1337 is offline   Reply With Quote
Old 12-05-2014, 02:01 AM   #131
catsec
centurion
 
catsec's Avatar
 
Join Date: Apr 2014
Location: hunting shells
Posts: 179
Re: Programming homework and newbie help thread

I'm working through the Python (2.7) lists and dictionaries modules on codecademy.com, and I'm having trouble grasping the formatting operator. I understand a little that with strings, the % format operator keeps something constant, allowing you to add in variables around it, or something like that. I don't know, it's confusing. Anyway, the code is this:


Code:
price = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

for key in price:
    print key
    print "price: %s" % price[key]
    print "stock: %s" % stock[key]
I understand fine that I create a price, and stock dictionary. I understand the key:value pair just fine. I understand the for loop iterates through the price dictionary, and I also understand that it accesses the stock dictionary's keys as well. But I don't understand the bolded.

print "price: %s" % price[key]
print "stock: %s" % stock[key]
catsec is offline   Reply With Quote
Old 12-05-2014, 03:09 AM   #132
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: Programming homework and newbie help thread

The %s is a printf format specifier: http://www.codingunit.com/printf-for...rmatted-output

That page is about C but it doesn't matter; printf is standard in many languages at this point, including Python. Although looking at it I don't know why they use %s as I'd think %d or %f is more appropriate.

The second % is basically making it so that the dictionary calls are the argument to the specifier in the string; so it's telling it to replace the %s with price[key] or stock[key] as appropriate.

There's also a .format method that's a bit more intuitive:

Code:
print "price: {}".format(price[key])
print "stock: {}".format(stock[key])
Xhad is offline   Reply With Quote
Old 12-05-2014, 04:05 AM   #133
catsec
centurion
 
catsec's Avatar
 
Join Date: Apr 2014
Location: hunting shells
Posts: 179
Re: Programming homework and newbie help thread

Thanks for the post/link mate, I'll read it over.
catsec is offline   Reply With Quote
Old 12-09-2014, 02:08 AM   #134
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

Quote:
Originally Posted by Xhad View Post
The %s is a printf format specifier: http://www.codingunit.com/printf-for...rmatted-output

That page is about C but it doesn't matter; printf is standard in many languages at this point, including Python. Although looking at it I don't know why they use %s as I'd think %d or %f is more appropriate.

The second % is basically making it so that the dictionary calls are the argument to the specifier in the string; so it's telling it to replace the %s with price[key] or stock[key] as appropriate.

There's also a .format method that's a bit more intuitive:

Code:
print "price: {}".format(price[key])
print "stock: {}".format(stock[key])
Call me nitty, but I think .format() should be used at all times. This rule would have saved me considerable headache over the years.
daveT is offline   Reply With Quote
Old 12-09-2014, 02:47 AM   #135
Xhad
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,962
Re: Programming homework and newbie help thread

I suspect most languages have a tradeoff of using printf because every language has it, or using some other thing that's specific to the language but better than printf in a vacuum (the "other thing" should never be worse than printf in a vacuum because then no one would use it; they'd just use printf since it's everywhere)
Xhad is offline   Reply With Quote
Old 12-09-2014, 09:07 AM   #136
catsec
centurion
 
catsec's Avatar
 
Join Date: Apr 2014
Location: hunting shells
Posts: 179
Re: Programming homework and newbie help thread

daveT,

What problems have you ran into when it wasn't used?
catsec is offline   Reply With Quote
Old 12-09-2014, 07:07 PM   #137
NxtWrldChamp
Carpal \'Tunnel
 
NxtWrldChamp's Avatar
 
Join Date: Feb 2008
Location: LEVEL 10
Posts: 8,985
Re: Programming homework and newbie help thread

Hello all,

After about my 5th attempt to get interested in learning coding I've finally made the smallest amount of progress that has created some momentum in my learning. I'm interested in creating a simple(not really) golf app and have started with python.

So far what I have is python reading a CSV file that contains 2 columns of data. It creates a list for each column. Doing this I can ask for user input, look up the position of that user input in list 1, and then return the value in the corresponding position of list 2.

With that in mind, I've gotten as far as asking the user how long the hole he is about to play is, then asked him where the shot he hit finished. Using those inputs I can then calculate how good of a shot he hit.

Code:
import csv
#puts starting locations into a list
f = open('SGdata.csv')

csv_f = csv.reader(f)

shots = []

for row in csv_f:
	shots.append(row[0])
	
f.close()
#puts strokes gained from certain locations into a list
f = open('SGdata.csv')

csv_f = csv.reader(f)

strokesgained = []

for row in csv_f:
	strokesgained.append(row[1])
	
f.close()

#prints menu options and prompts for choice
def menu():
	print "Welcome to the Strokes Gained Calculator"
	print "your options are:"
	print " "
	print "1) Calculate Strokes Gained"
	print "2)Exit the calculator"
	print " "
	return input ("Choose your option:")
	
#finish location menu
def finish_locations():
	print "Where did your shot finish?"
	print " " 
	print "'F' for Fairway"
	print "'R' for Rough"
	print "'S' for Sand"
	print "'G' for Green"
	print "'R' for Recovery"
	print "'H' for In The Hole"
	print " " 
	

#calculates strokes gained for your tee shot
def StrokesGained():
	hole_length = raw_input('Enter the length of the hole you are playing:')
	t = 'T'
	starting_shot = '%s-%s' %(t,hole_length)
	Start = shots.index(starting_shot)
	SGTHO = float(strokesgained[Start])
	print "Average strokes to complete hole measuring " + str(hole_length) + " yards = " + str(SGTHO)
	print " "
	finish_locations()
	finish_location = raw_input('Enter finish surface: ')
	finish_distance = raw_input('How far from the hole did your shot finish? ')
	finish_shot = '%s-%s' %(finish_location,finish_distance)
	Finish = shots.index(finish_shot)
	SG = float(strokesgained[Finish])
	print "You gained ",SGTHO - SG - 1," shots"
	print " "

loop = 1
choice = 0
while loop == 1:
	choice = menu()
	if choice == 1:
		StrokesGained()
	elif choice == 2:
		loop = 0
		
print "Thank you for using calculator"
Quick example:
If you are playing a 400 yard par 4, it takes on average 3.99 shots to get your ball in the hole.

You hit a good shot, 300 yards down the fairway, so you are now 100 yards away in the fairway. From that location/distance it takes 2.8 shots to get your ball in the hole.

So to calculate how good the shot was you would do 3.99 - 2.8 - 1(for the shot it took you to get from point A to point B) = .19. This is currently what the code returns.

Where I'm getting stuck is how to do more than 1 shot at a time. How could I calculate it for an entire hole. I would need to keep asking the user where each of his shots finished, and then calculating that finish location with the previous starting location. And then of course it has to be dynamic(?) bc sometimes you only take 3 shots on a hole and sometimes you take 8. Anyone following? Kind of complicated I guess. Maybe how can I store each location/distance combo they enter?

Any help would be greatly appreciated.
NxtWrldChamp is offline   Reply With Quote
Old 12-09-2014, 07:38 PM   #138
NxtWrldChamp
Carpal \'Tunnel
 
NxtWrldChamp's Avatar
 
Join Date: Feb 2008
Location: LEVEL 10
Posts: 8,985
Re: Programming homework and newbie help thread

Figured out how to store user inputs, by appending to a list. So that is fun.
NxtWrldChamp is offline   Reply With Quote
Old 12-10-2014, 01:08 PM   #139
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,079
Re: Programming homework and newbie help thread

What exactly does import in Java do? I am supposed to compare a bunch of stuff between C++ and Java, and this is the only question I am kind of stuck on.

Code:
Compare #include and import
I know that #include just tells the pre-processor to include the contents of another source. I have never really given much thought to what exactly import does, and Google hasn't been much help.
KatoKrazy is offline   Reply With Quote
Old 12-10-2014, 03:56 PM   #140
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

does this not answer your question?
Anais is offline   Reply With Quote
Old 12-10-2014, 04:26 PM   #141
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,079
Re: Programming homework and newbie help thread

Holy crap! Yes, it does! I even looked at that briefly at one point I think. Complete fail on my part.
KatoKrazy is offline   Reply With Quote
Old 12-10-2014, 04:54 PM   #142
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

pretty sure you probably did, since it was the second google result
Anais is offline   Reply With Quote
Old 12-11-2014, 11:29 AM   #143
Acemanhattan
veteran
 
Acemanhattan's Avatar
 
Join Date: Jan 2007
Location: PDX Oregon
Posts: 2,978
Re: Programming homework and newbie help thread

I just want to get a sense of what sort of skills are required to do a personal project like the following, and what sort of things might make it computationally impossible/difficult. The motivation here is wanting to find the winning opening moves in 3x3, 4x4, and 5x5 games of hex.

Let's say we make a program that generates an n x n hex board with n of our choosing and then fills it by placing moves alternatively at random for player 1 and player 2 respectively. For any n x n board the program would repeat this process millions (tens, hundreds, thousands, billions) of times and then tell us how many wins resulted from any given opening move. If a move lead to a win 100% of the time, then we'd know (though not to a degree of mathematical certainty) that that opening move is a winning opening move.

I believe this might be a Monte Carlo simulation of sorts, but I'm not actually sure outside of PokerStove what constitutes an actual Monte Carlo simulation.
Acemanhattan is offline   Reply With Quote
Old 12-11-2014, 01:44 PM   #144
Allen C
journeyman
 
Join Date: Oct 2004
Posts: 379
Re: Programming homework and newbie help thread

The small games should yield completely to the minimax algorithm. What will make it computationally difficult/impossible is the number of possible games. Figure out how many there are for a given board, and if it is well over some number of billions its getting too big.

Picking random moves probably won't help much in a game of perfect information and no move will "win 100% of the time" will it? Because sometimes you will play dumb future moves that give away the win. If you use the minimax idea with no pruning you might be able to discover stuff like how many ways are there for the winning side to blow the win.

Oh, and what kind of skills do you need? Probably about 4-5 months of general programming practice and study from beginnerhood.

disclaimer: My only knowledge of hex comes from the 60 seconds I spent on Google before writing this answer.
Allen C is offline   Reply With Quote
Old 12-11-2014, 02:28 PM   #145
Acemanhattan
veteran
 
Acemanhattan's Avatar
 
Join Date: Jan 2007
Location: PDX Oregon
Posts: 2,978
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Allen C View Post
The small games should yield completely to the minimax algorithm. What will make it computationally difficult/impossible is the number of possible games. Figure out how many there are for a given board, and if it is well over some number of billions its getting too big.

Picking random moves probably won't help much in a game of perfect information and no move will "win 100% of the time" will it? Because sometimes you will play dumb future moves that give away the win. If you use the minimax idea with no pruning you might be able to discover stuff like how many ways are there for the winning side to blow the win.

Oh, and what kind of skills do you need? Probably about 4-5 months of general programming practice and study from beginnerhood.

disclaimer: My only knowledge of hex comes from the 60 seconds I spent on Google before writing this answer.
Thanks for the response. It makes sense that the # of possible games once we get bigger than some n would become so large that we couldn't execute this "algorithm" anymore.

Though regarding no move winning 100% of the time: I'm fairly certain that in 3x3 hex if player 1 plays in the middle on their first move, there aren't enough hexagons left for them to play bad enough on the ensuing moves to lose. I also think that for games up to 5x5 any move on the main diagonal will have the same sort of outcome. I think that once we get beyond that though, there is enough room on the board that player 2 could exploit sub optimal second,third,etc moves.
Acemanhattan is offline   Reply With Quote
Old 12-11-2014, 04:12 PM   #146
Allen C
journeyman
 
Join Date: Oct 2004
Posts: 379
Re: Programming homework and newbie help thread

Maybe I don't understand the rules or what you are saying. Isn't this a possible loss after going in the center?
Allen C is offline   Reply With Quote
Old 12-11-2014, 05:12 PM   #147
Acemanhattan
veteran
 
Acemanhattan's Avatar
 
Join Date: Jan 2007
Location: PDX Oregon
Posts: 2,978
Re: Programming homework and newbie help thread

Yeah, you're totally right. I don't know what I did to make myself believe that but I think I drew my board wrong to do it.
Acemanhattan is offline   Reply With Quote
Old 12-11-2014, 06:22 PM   #148
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Wondering what level of programming one would need to obtain to be able to write a simple, text-based poker game. Like, what all concepts would one need to know?

I've got structs and classes, c string arrays, dynamic & two dimensional arrays, a hint of recursion, pointers... I feel like that should just about cover the very basics of a poker game, say 1 player vs the computer or something, god awful-basic AI if any at all.

Just want a project to work on during the break to keep my skills sharpened, as I'm very prone to forgetting things I'm not using and a month off is a scary thing to me.
Anais is offline   Reply With Quote
Old 12-11-2014, 06:36 PM   #149
Mr.mmmKay
Drugs are bad...
 
Mr.mmmKay's Avatar
 
Join Date: May 2011
Posts: 13,085
Re: Programming homework and newbie help thread

i somewhat learned c++ a while ago by reading the course notes and doing the exercises
of this course (already knew c)
http://ocw.mit.edu/courses/electrica...uary-iap-2011/

and then afterwards, as a project, i did a text based NLHE 9 max cash game. But i kinda gave up as keeping track of the side pots was a bit of a pita, as was finding a decent algorithm to determine the winner, will probably finish it sometime though. But it's definitely doable with basic c++ stuff and some stuff from the standard library
Mr.mmmKay is online now   Reply With Quote
Old 12-11-2014, 07:03 PM   #150
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 33,900
Re: Programming homework and newbie help thread

Acemanhattan:

I could knock you up a Hex-solving program that "worked" in an hour or two, but the sun might burn out before it finished traversing the 5x5 game tree. On the other end of the spectrum, here's a 2007 Ph. D. thesis on building a more efficient Hex solver. So the answer to "how hard is it to write a program that solves Hex?" is "How long would you like to wait for it to execute?".

Also, Monte Carlo simulations are not a thing in game-solving. Monte Carlo is trying out possibilities at random to give an approximate answer. The problem with this is, to use chess as an example, suppose a program is evaluating capturing an opponent's protected queen. If you assume random moves from the opponent, the evaluation will be super favourable because anything move from the opponent other than recapturing will be a huge advantage for you.
ChrisV 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:08 PM.


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