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

02-20-2015 , 12:39 AM
I think a prerequisite for a c++ class should be a class on vanilla c or at least k&r or something.
Programming homework and newbie help thread Quote
02-20-2015 , 09:29 AM
Quote:
Originally Posted by Allen C
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.
Programming homework and newbie help thread Quote
02-20-2015 , 11:47 AM
Shoulda gone Java : /
Programming homework and newbie help thread Quote
02-20-2015 , 12:21 PM
Quote:
Originally Posted by CallMeIshmael
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
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)
Programming homework and newbie help thread Quote
02-20-2015 , 01:42 PM
Quote:
Originally Posted by econophile

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.
Programming homework and newbie help thread Quote
02-21-2015 , 12:59 PM
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>
Programming homework and newbie help thread Quote
02-21-2015 , 01:00 PM
also, can someone tell me how to format code on these forums
Programming homework and newbie help thread Quote
02-21-2015 , 01:21 PM
[ code ]
Hello world!
[/ code ]

Lose the whitespace.

Code:
Hello world!
Programming homework and newbie help thread Quote
02-21-2015 , 01:37 PM
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?
Programming homework and newbie help thread Quote
02-21-2015 , 01:38 PM
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;
}
Programming homework and newbie help thread Quote
02-21-2015 , 01:43 PM
Quote:
Originally Posted by Anais
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.
Programming homework and newbie help thread Quote
02-21-2015 , 01:43 PM
Quote:
<li id="apps"></li>
What's this supposed to be doing?

Also, why not use semantic tags? What's with all the divs?
Programming homework and newbie help thread Quote
02-21-2015 , 01:45 PM
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
Programming homework and newbie help thread Quote
02-21-2015 , 02:06 PM
Quote:
Originally Posted by Anais
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.
Programming homework and newbie help thread Quote
02-21-2015 , 02:21 PM
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
Programming homework and newbie help thread Quote
02-21-2015 , 02:33 PM
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.
Programming homework and newbie help thread Quote
02-21-2015 , 02:38 PM
sorry i just installed Linux Mint and the prnt scrn isn't working like i'm used to in windows
Programming homework and newbie help thread Quote
02-21-2015 , 02:50 PM
How you gonna go Linux but not dual boot if you're new?
Programming homework and newbie help thread Quote
02-21-2015 , 02:57 PM
hmm i still have windows installed.
Programming homework and newbie help thread Quote
02-21-2015 , 02:59 PM
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.
Programming homework and newbie help thread Quote
02-21-2015 , 03:39 PM
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
Programming homework and newbie help thread Quote
02-21-2015 , 04:30 PM
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.
Programming homework and newbie help thread Quote
02-21-2015 , 05:22 PM
For google's source, do a ctrl+f and look for the stuff you're interested in, like "gmail"
Programming homework and newbie help thread Quote
02-21-2015 , 06:33 PM
alright will do, thanks for the suggestions.
Programming homework and newbie help thread Quote
02-21-2015 , 09:23 PM
Quote:
Originally Posted by Anais
Does your code validate?
http://validator.w3.org/check?uri=ww...Inline&group=0

Programming homework and newbie help thread Quote

      
m