Two Plus Two Poker Forums Programming homework and newbie help thread
 Register FAQ Search Today's Posts Mark Forums Read Video Directory TwoPlusTwo.com

 Notices

12-03-2014, 08:25 AM   #126
ChrisV
Carpal \'Tunnel

Join Date: Jul 2004
Posts: 34,156
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 cubes = new List(); List[] lists = new List[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.

 12-03-2014, 09:37 AM #127 iosys old hand     Join Date: Sep 2011 Posts: 1,754 Re: Programming homework and newbie help thread cool stuff ^^
12-03-2014, 11:41 AM   #128
NiSash1337
Carpal \'Tunnel

Join Date: Dec 2010
Location: Germany
Posts: 9,189
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by iosys 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.

12-03-2014, 12:14 PM   #129
iosys
old hand

Join Date: Sep 2011
Posts: 1,754
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by NiSash1337 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.

 12-03-2014, 01:37 PM #130 NiSash1337 Carpal \'Tunnel     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.
 12-05-2014, 02:01 AM #131 catsec centurion     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]
 12-05-2014, 03:09 AM #132 Xhad Carpal \'Tunnel     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])```
 12-05-2014, 04:05 AM #133 catsec centurion     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.
12-09-2014, 02:08 AM   #134
daveT
S.A.G.E. Master

Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 21,636
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by Xhad 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.

 12-09-2014, 02:47 AM #135 Xhad Carpal \'Tunnel     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)
 12-09-2014, 09:07 AM #136 catsec centurion     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?
 12-09-2014, 07:07 PM #137 NxtWrldChamp Carpal \'Tunnel     Join Date: Feb 2008 Location: LEVEL 10 Posts: 8,990 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.
 12-09-2014, 07:38 PM #138 NxtWrldChamp Carpal \'Tunnel     Join Date: Feb 2008 Location: LEVEL 10 Posts: 8,990 Re: Programming homework and newbie help thread Figured out how to store user inputs, by appending to a list. So that is fun.
 12-10-2014, 01:08 PM #139 KatoKrazy veteran     Join Date: Jan 2009 Location: Roch, MN Posts: 3,082 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.
 12-10-2014, 03:56 PM #140 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread does this not answer your question?
 12-10-2014, 04:26 PM #141 KatoKrazy veteran     Join Date: Jan 2009 Location: Roch, MN Posts: 3,082 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.
 12-10-2014, 04:54 PM #142 Anais Carpal \'Tunnel     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
 12-11-2014, 11:29 AM #143 Acemanhattan veteran     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.
 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.
12-11-2014, 02:28 PM   #145
Acemanhattan
veteran

Join Date: Jan 2007
Location: PDX Oregon
Posts: 2,978
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by Allen C 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.

 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?
 12-11-2014, 05:12 PM #147 Acemanhattan veteran     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.
 12-11-2014, 06:22 PM #148 Anais Carpal \'Tunnel     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.
 12-11-2014, 06:36 PM #149 Mr.mmmKay Drugs are bad...     Join Date: May 2011 Posts: 13,232 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
 12-11-2014, 07:03 PM #150 ChrisV Carpal \'Tunnel     Join Date: Jul 2004 Location: Adelaide, Australia Posts: 34,156 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.

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Two Plus Two     Two Plus Two Magazine Forum     The Best of Two Plus Two     The Two Plus Two Bonus Program     Two Plus Two Pokercast     Two Plus Two Videos     Marketplace         General Marketplace         Staking - Offering Stakes         Staking - Seeking Stakes         Staking - Selling Shares - Online         Staking - Selling Shares - Live         Staking Rails         Transaction Feedback & Disputes     Commercial Marketplace     Staking - Offering Stakes     About the Forums Fantasy Sports     Fantasy Sports         Sporting Events General Poker Discussion     Beginners Questions     Live Casino Poker         Poker Venues         Regional Communities     Poker Goals & Challenges     Books and Publications     Poker Theory     Poker Tells/Behavior, hosted by: Zachary Elwood     News, Views, and Gossip     Twitch - Watch and Discuss Live Online Poker     Televised Poker     Home Poker     Poker Legislation & PPA Discussion hosted by Rich Muny     That's What She Said!     Poker Beats, Brags, and Variance Coaching/Training     Coaching Advice     Cash Game Poker Coach Listings     Tournament/SNG Poker Coach Listings International Forums     Deutsch         BBV [German]     Français     Two Plus Two en Espańol No Limit Hold'em     Medium-High Stakes PL/NL     Micro-Small Stakes PL/NL     Medium-High Stakes Full Ring     Micro-Small Stakes Full Ring     Heads Up NL     Live Low-stakes NL Limit Texas Hold'em     Mid-High Stakes Limit     Micro-Small Stakes Limit Tournament Poker     STT Strategy     Heads Up SNG and Spin and Gos     Mid-High Stakes MTT     Small Stakes MTT     MTT Community     MTTc - Live         WPT.com Other Poker     High Stakes PL Omaha     Small Stakes PL Omaha     Omaha/8     Stud     Draw and Other Poker General Gambling     Backgammon Forum hosted by Bill Robertie.     Probability     Psychology     Sports Betting     Other Gambling Games Internet Poker     Internet Poker         Winning Poker Network         nj.partypoker.com         Global Poker     Commercial Software     Software         Commercial Software         Free Software     nj.partypoker.com         WPT.com 2+2 Communities     Other Other Topics         OOTV         Game of Thrones     The Lounge: Discussion+Review     EDF     Las Vegas Lifestyle     BBV4Life         omg omg omg     House of Blogs Sports and Games     Sporting Events         Single-Team Season Threads         Fantasy Sports     Wrestling     Golf     Pool, Snooker, and Billiards     Chess and Other Board Games     Video Games         League of Legends         Hearthstone     Puzzles and Other Games Other Topics     Politics         Economics     Business, Finance, and Investing     Travel     Science, Math, and Philosophy     History     Religion, God, and Theology     Health and Fitness     Student Life     The Studio     Laughs or Links!     Computer Technical Help     Programming

All times are GMT -4. The time now is 04:50 AM.

 Contact Us - Two Plus Two Publishing LLC - Privacy Statement - Top