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 02-20-2015, 12:39 AM   #301
Allen C
journeyman
 
Join Date: Oct 2004
Posts: 379
Re: Programming homework and newbie help thread

I think a prerequisite for a c++ class should be a class on vanilla c or at least k&r or something.
Allen C is offline   Reply With Quote
Old 02-20-2015, 09:29 AM   #302
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 21,570
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Allen C View Post
I think a prerequisite for a c++ class should be a class on vanilla c or at least k&r or something.
Yes, I agree. A migration from C to C++ is what I would recommend. I would also advocate the course in C cover the full blown current C standard which I believe is C11. There really isn't nearly as much to cover in C compared to C++, you absolutely need a good understanding of the C scope rules, C types, and C pointers to move on to C++ in my view.
adios is offline   Reply With Quote
Old 02-20-2015, 11:47 AM   #303
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Shoulda gone Java : /
Anais is offline   Reply With Quote
Old 02-20-2015, 12:21 PM   #304
econophile
Carpal \'Tunnel
 
econophile's Avatar
 
Join Date: Jul 2005
Location: (X'X)^(-1)X'Y
Posts: 11,319
Re: Programming homework and newbie help thread

Quote:
Originally Posted by CallMeIshmael View Post
Some of this might be personal preference, but the entire use of the hardcode rank variable seems overly confusing, with no real upside that I can see. (super fast hand evaluators that ive seen have some confusing ass dictionaries but that is for performance reasons)


I think if you convert the straight and flush variables to bool, you can get code that looks like:

if (straight and flush):
return "straight flush"

if (flush):
return "flush"

if (straight):
return "straight"


How you want to handle the counts variable could go a couple ways, but, again imo, you should always have a reason to do something other than the most readable thing in a function, and the readable thing is to sort the counts variable, and say

if 1,1,1,1,1, return high card
if 1,1,1,2, return 1 pair etc


to me, this lines up better with the actual logic of the problem we are solving
I agree that the handcode calculation is confusing. It turns out that I don't need it, and I can instead use the count variable plus the straight and flush indicators as dictionary keys for the hand ranks.

Quote:
Originally Posted by daveT View Post
Code:
flush = False
if len(set(s)) == 1:
    flush = True
I don't like that either, because you are assuming 5 cards and this breaks with 7 cards, but if you are strictly dealing with 5 cards, you only have to do:

Code:
len(s) == 5
This is going to be much clearer. I'm not entirely sure on this, but I think that len() is also a referenced number, so that you don't do an iteration over the list each time you ask for len() as the len() value is carried around. Minor speed optimization if this is true, but something else to consider.

I also think you should put this in a function, like so:

Code:
def is_flush (s):
    do some test:
    return True

flush = is_flush(c)
The rank function is only going to be used with 5-card hands. My plan is to add other functions that break 7-card hands into all possible 5 card hands to determine the best 5-card hand.

The way I have set it up, the hand_suits variable will take values like ['s', 'c', 's', 'h', 'd'] and will always have a length of 5. So I could test for a flush by asking something like
Code:
hand_suits.count(hand_suits[0]) == 5
or by asking
Code:
len(set(hand_suits)) == 1
Not sure if there is a reason to prefer one over the other.

Does it only make sense to define flush as a separate function if I expect other functions to call it, or is it good practice to break it out even if it only used in the rank function? What is the cutoff for breaking out sub-programs?

Anyway, here is the latest version of my code, now with a testing function.

Code:
VALUES = map(str, range(2, 10)) + ['T', 'J', 'Q', 'K', 'A']
CARD_RANK = dict(zip(VALUES, range(0, 13)))

HAND_RANK = {
    (1,1,1,1,1,'s','f'): ('Straight flush', 9), (4,1): ('Four of a kind', 8),
    (3,2): ('Full house', 7), (1,1,1,1,1,'f'): ('Flush', 6), 
    (1,1,1,1,1,'s'): ('Straight', 5), (3,1,1):('Three of a kind', 4),
    (2,2,1): ('Two pair', 3), (2,1,1,1): ('One pair', 2),
    (1,1,1,1,1):('High card', 1) 
}

def rank(hand, card_rank, hand_rank):
    """Assumes hand is a list of format ['Tc', '2h', 'As', '2c', 'Qd']
       Returns tuple with a string describing the hand's rank and a number 
       corresponding to the rank's relative strength, e.g., ('One pair', 2)"""
    hand_vals, hand_suits, counts, i, j = [], [], [], 0, 0
    for v, s in hand:
        hand_vals.append(card_rank[v])
        hand_suits.append(s)
    hand_vals.sort()
    while i < len(hand_vals):
        counts.append(hand_vals.count(hand_vals[i]))
        i += counts[j]
        j += 1
    counts.sort(reverse = True)
    if ((hand_vals[4] - hand_vals[0] == 4) or (hand_vals == [0, 1, 2, 3, 12])) \
        and counts[0] == 1:
        counts.append('s')
    if len(set(hand_suits)) == 1:
        counts.append('f')
    return hand_rank[tuple(counts)]

def test_rank(hand, card_rank, hand_rank):
    answer = rank(hand, card_rank, hand_rank)
    print 'Test hand is:', hand
    print 'Test hand rank is:', answer

h1 = ['5s', '4s', '8s', '6s', '7s']
h2 = ['3h', '3s', '4c', '3c', '3d']
h3 = ['7h', '7d', 'Ac', 'Ac', '7c']
h4 = ['2c', '5c', 'Jc', '7c', 'Tc']
h5 = ['Ac', '5c', '3s', '4h', '2h']
h6 = ['8h', '7s', 'Jh', '9d', 'Tc']
h7 = ['6c', '3h', 'As', '6d', '6h']
h8 = ['Ac', '8d', '8h', '2c', 'Ah']
h9 = ['7s', '7h', '9d', 'Td', 'Jh']
h10 = ['2c', '4c', '9s', 'Jh', '7h']
test_hands = [h1, h2, h3, h4, h5, h6, h7, h8, h9, h10]

for h in test_hands:
    test_rank(h, CARD_RANK, HAND_RANK)
econophile is offline   Reply With Quote
Old 02-20-2015, 01:42 PM   #305
Allen C
journeyman
 
Join Date: Oct 2004
Posts: 379
Re: Programming homework and newbie help thread

Quote:
Originally Posted by econophile View Post

Does it only make sense to define flush as a separate function if I expect other functions to call it, or is it good practice to break it out even if it only used in the rank function? What is the cutoff for breaking out sub-programs?
In my opinion, if it has a good name, put it in a separate function. If you do it more than once, definitely put it in a separate function.
Allen C is offline   Reply With Quote
Old 02-21-2015, 12:59 PM   #306
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

trying to re-create the Google homepage (just basic html/css, not any interactive parts). i'm having trouble figuring out how to do the top-righthand apps thing:



(the 9 grey squares at top right)

for some reason i can't seem to add any background color to an individual <li> element.

html code:

<code>
<!DOCTYPE html>
<html>
<head>
<title>Google Homepage</title>
</head>
<link rel="stylesheet" type="text/css" href="googleCSS.css">
<body>
<div>
<ul class="topnav">
<li>
<a href="https://plus.google.com/u/0/getstarted/getstarted?fww=1">+You</a></li>
<li>
<a href="https://mail.google.com/mail/">Gmail</a></li>
<li>
<a href="https://www.google.com/imghp?hl=en&ei=j1DmVJa8F4K7ggSrmIGQCA&ved=0CAMQqi4 oAg">Images</a></li>
<li id="apps"></li>
</ul>


<div class="maindiv"><img id="logo" src="http://cdn.ientry.com/sites/webpronews/pictures/new-google-logo.jpg" />
<div id="search"><form action="http://www.google.com" method="GET">
<input type="text" placeholder=" Search Google or type URL" class="text" name="search" id="search">
<br>
<input type="submit" class="submit" value="Google Search">
<input type="submit" class="submit" value="I'm Feeling Lucky">
</form>
</div></div>
</body>
</html></code>

css code: (problem is that #apps CSS isn't showing any difference on my page)

<code>body {
text-align: center;
}

ul {
text-align: right;
margin: 1px;
}

li {
padding: 4px;
display: inline;
}

#apps {
height: 30px;
width: 30px;
background: black;
}

div.maindiv {
position: absolute;
top: 200px;
bottom: 200px;
left: 200px;
right: 200px;
}

#logo {
margin-top: 130px;
}

#search {
position: relative;
margin-top: -30px;
}

input.text {
height: 25px;
width: 700px;
}

input.submit {
height: 28px;
width: 130px;
margin-top: 25px;
margin-right: 30px;
}</code>
cookies4u is offline   Reply With Quote
Old 02-21-2015, 01:00 PM   #307
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

also, can someone tell me how to format code on these forums
cookies4u is offline   Reply With Quote
Old 02-21-2015, 01:21 PM   #308
KatoKrazy
Pooh-Bah
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Orange County, CA
Posts: 4,061
Re: Programming homework and newbie help thread

[ code ]
Hello world!
[/ code ]

Lose the whitespace.

Code:
Hello world!
KatoKrazy is offline   Reply With Quote
Old 02-21-2015, 01:37 PM   #309
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Does your code validate?

Maybe I'm not remembering css well enough, but # is ID, . is class, no? And IDs are unique to one instance?
Anais is offline   Reply With Quote
Old 02-21-2015, 01:38 PM   #310
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

hah too much staring at html tags, thanks.

html code:

Code:
<!DOCTYPE html>
<html>
<head>
<title>Google Homepage</title>
</head>
<link rel="stylesheet" type="text/css" href="googleCSS.css">
<body>
	<div>
		<ul class="topnav">
			<li>
				<a href="https://plus.google.com/u/0/getstarted/getstarted?fww=1">+You</a></li>
			<li>
				<a href="https://mail.google.com/mail/">Gmail</a></li>
			<li>
				<a href="https://www.google.com/imghp?hl=en&ei=j1DmVJa8F4K7ggSrmIGQCA&ved=0CAMQqi4oAg">Images</a></li>
			<li id="apps"></li>
		</ul>


	<div class="maindiv"><img id="logo" src="http://cdn.ientry.com/sites/webpronews/pictures/new-google-logo.jpg" />
		<div id="search"><form action="http://www.google.com" method="GET">
			<input type="text" placeholder=" Search Google or type URL" class="text" name="search" id="search">
			<br>
			<input type="submit" class="submit" value="Google Search">
			<input type="submit" class="submit" value="I'm Feeling Lucky">
	</form>
	</div></div>
	</body>
	</html>
css code:

Code:
body {
	text-align: center;
}

ul {
	text-align: right;
	margin: 1px;
}

li {
	padding: 4px;
	display: inline;
}

#apps { /*These are not effecting the page*/
	height: 30px;
	width: 30px;
	background: black;
}

div.maindiv {
	position: absolute;
	top: 200px;
	bottom: 200px;
	left: 200px;
	right: 200px;
}

#logo {
	margin-top: 130px;
}

#search {
	position: relative;
	margin-top: -30px;
}

input.text {
	height: 25px;
	width: 700px;
}

input.submit {
	height: 28px;
	width: 130px;
	margin-top: 25px;
	margin-right: 30px;
}
cookies4u is offline   Reply With Quote
Old 02-21-2015, 01:43 PM   #311
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
Does your code validate?

Maybe I'm not remembering css well enough, but # is ID, . is class, no? And IDs are unique to one instance?
yeah # is ID and . is class. i'm just trying to style the #apps <li> element, not sure what i'm missing. so with my current code i'm expecting to see a 30x30 black square next to the other list items (not correct for what i ultimately want to do, but i'm just trying to get it to show something).

i'm not even sure what "validate" means tbh, i'm just typing the code in sublime text and loading the page.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 01:43 PM   #312
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Quote:
<li id="apps"></li>
What's this supposed to be doing?

Also, why not use semantic tags? What's with all the divs?
Anais is offline   Reply With Quote
Old 02-21-2015, 01:45 PM   #313
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Google html validator and put your html code in

Then Google css validator and put your css code in

That'll let you know if your code validates
Anais is offline   Reply With Quote
Old 02-21-2015, 02:06 PM   #314
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
What's this supposed to be doing?

Also, why not use semantic tags? What's with all the divs?
it's just a list item where i want to put my "design"

for ex. if i put
Code:
<div id="square"></div>
and

Code:
#square {
	height: 30px;
	width: 30px;
	background: black;
}
it shows what i'm trying to get...but it's not working for some reason inside the list.

ok, checked the validators: CSS validates fine, HTML has 2 errors and 3 warnings, apparently my <link> tag is supposed to be inside the head. 2nd error just said it can't recover from first error and any more errors will be ignored.

thanks for the semantics reccomendation, i will fix that too.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 02:21 PM   #315
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Can you upload a pic to imgur of what your site looks like when you try to load it? Would like to see the errors
Anais is offline   Reply With Quote
Old 02-21-2015, 02:33 PM   #316
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

alright, i forgot to close the first div, did that and it brought up 4 more errors.

2 of them are "... & did not start a character reference. (& probably should have been escaped as &amp;.)", which is something i've never heard of but am googling.

1 says my img must have an alt. attribute which i've never heard of but will add it.

the other was because i used the "search" id for two elements, oops.

also for a reason that is beyond me, moving the link tag into the header made my "maindiv" class lose its vertical alignment (fell to bottom of page), it still kept its horizontal alignment tho.

hrmmmmmm

Last edited by cookies4u; 02-21-2015 at 02:42 PM.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 02:38 PM   #317
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

sorry i just installed Linux Mint and the prnt scrn isn't working like i'm used to in windows
cookies4u is offline   Reply With Quote
Old 02-21-2015, 02:50 PM   #318
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

How you gonna go Linux but not dual boot if you're new?
Anais is offline   Reply With Quote
Old 02-21-2015, 02:57 PM   #319
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

hmm i still have windows installed.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 02:59 PM   #320
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

ok fixed the errors, this html validates:

Code:
<!DOCTYPE html>
<html>
<head>
<title>Google Homepage</title>
<link rel="stylesheet" type="text/css" href="googleCSS.css">
</head>
<body>
	<div>
		<ul class="topnav">
			<li>
				<a href="https://plus.google.com/u/0/getstarted/getstarted?fww=1">+You</a></li>
			<li>
				<a href="https://mail.google.com/mail/">Gmail</a></li>
			<li>
				<a href="https://www.google.com/imghp?hl=en&amp;ei=j1DmVJa8F4K7ggSrmIGQCA&amp;ved=0CAMQqi4oAg">Images</a></li>
			<li id="apps"></li>
		</ul></div>


	<div class="maindiv"><img id="logo" src="http://cdn.ientry.com/sites/webpronews/pictures/new-google-logo.jpg" alt="Google Logo" />
		<div id="search"><form action="http://www.google.com" method="GET">
			<input type="text" placeholder=" Search Google or type URL" class="text" name="search" id="searchbox">
			<br>
			<input type="submit" class="submit" value="Google Search">
			<input type="submit" class="submit" value="I'm Feeling Lucky">
	</form>
	</div></div>
	</body>
	</html>
now i get a black quadrilateral where i expected but i can't adjust its dimensions, even if i put width or height = 0 it stays the same.

after fiddling around i got the maindiv (google logo/searchbox) to re-center by removing "position: absolute" from the CSS, but i have no clue why it worked lol.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 03:39 PM   #321
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

Two things:

Even though it's huge, have you examined google's source code?

Also, wherever you're learning from seems to be pushing some outdated methods. Might consider looking around for something a bit more up to date
Anais is offline   Reply With Quote
Old 02-21-2015, 04:30 PM   #322
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

i'm using the odin project. is there anything else besides not using semantic tags that's outdated?

i took a glance at google's source and quickly saw a bunch of stuff that wasn't remotely familiar to me. the current course i'm doing is basically a brief overview of everything (html/css, javascript, ruby, databases, git, etc.), and later in the curriculum they delve into each one more thoroughly. so i figured the stuff that doesn't make sense now would be covered later on.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 05:22 PM   #323
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

For google's source, do a ctrl+f and look for the stuff you're interested in, like "gmail"
Anais is offline   Reply With Quote
Old 02-21-2015, 06:33 PM   #324
cookies4u
grinder
 
Join Date: Aug 2014
Posts: 467
Re: Programming homework and newbie help thread

alright will do, thanks for the suggestions.
cookies4u is offline   Reply With Quote
Old 02-21-2015, 09:23 PM   #325
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: La La Land
Posts: 23,211
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Anais View Post
Does your code validate?
http://validator.w3.org/check?uri=ww...Inline&group=0

daveT 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 01:28 PM.


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