Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 06-20-2011, 07:53 PM   #106
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

This code does exactly what I want it to do, but I wonder if there is a simpler solution to this problem. I'm also wondering if there is a different solution to this problem that I didn't catch.

The idea is to find all the values of n via n = 6a + 9b + 20c. I created an ascending list with no repeating values. The program runs pretty slow, but I guess that isn't too surprising since I am running 3 simultaneous loops, checking vs an if, creating a list, sorting the list, then minimizing the list.

Considering the problem set, I could use a smaller value than 150. I ran it with 50 as well, but it is still slow.

Code:
nList = []
for a in range(0,150):
    for b in range(0,150):
        for c in range(0,150):
            n = 6*a + 9*b + 20*c
            if n < 150:
                nList.append(n)
                nList.sort()
                nList = list(set(nList))
print nList
edit to add:

ldo, math ftl?

Code:
nList = []
for a in range(0,10):
    for b in range(0,6):
        for c in range(0,3):
            n = 6*a + 9*b + 20*c
            if n < 50:
                nList.append(n)
                nList.sort()
                nList = list(set(nList))
print nList

Last edited by daveT; 06-20-2011 at 08:05 PM. Reason: now, it's lightening fast.
daveT is offline   Reply With Quote
Old 06-20-2011, 08:11 PM   #107
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,918
Re: ** Python Support Thread **

why are you sorting and uniq'ing during every iteration?
tyler_cracker is offline   Reply With Quote
Old 06-20-2011, 08:40 PM   #108
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

Quote:
Originally Posted by tyler_cracker View Post
why are you sorting and uniq'ing during every iteration?
I have to check if n, n + 1,... n+5 is possible. According to the theorem, once I have that, then all n + (inf) is possible. I know that the highest possible number n that can't work is 37.

[0, 6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]

The plan (for now) is to check all numbers in the list against n, n + 1,... n+5 (which probably creates another for loop somewhere else), then print out 37.
daveT is offline   Reply With Quote
Old 06-20-2011, 08:57 PM   #109
ɹǝʍoʇpunoɹ
 
RoundTower's Avatar
 
Join Date: Feb 2005
Location: soah made my profile
Posts: 13,926
Re: ** Python Support Thread **

that's all gobbledegook to me but even if you really, really need to call
Code:
nList = list(set(nList))
on every iteration, which I'm sure you don't, the expensive sort() operation in the previous line changes absolutely nothing because set() unsorts the list. But really what you should do is make nList a set from the start and call nlist.add(n) instead of your 3 lines.
RoundTower is offline   Reply With Quote
Old 06-20-2011, 09:31 PM   #110
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

here is the problem set that explains what I am doing.

http://ocw.mit.edu/courses/electrica...ents/pset2.pdf
daveT is offline   Reply With Quote
Old 06-20-2011, 09:32 PM   #111
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

Quote:
Originally Posted by RoundTower View Post
that's all gobbledegook to me but even if you really, really need to call
Code:
nList = list(set(nList))
on every iteration, which I'm sure you don't, the expensive sort() operation in the previous line changes absolutely nothing because set() unsorts the list. But really what you should do is make nList a set from the start and call nlist.add(n) instead of your 3 lines.
How hard is it for me to highlight and format >> comment to see what happens without sort()?

But, yes, this is good. Thanks.
daveT is offline   Reply With Quote
Old 06-20-2011, 11:23 PM   #112
centurion
 
Giant Tortoise's Avatar
 
Join Date: Apr 2008
Posts: 171
Re: ** Python Support Thread **

Quote:
Originally Posted by daveT View Post
I have to check if n, n + 1,... n+5 is possible. According to the theorem, once I have that, then all n + (inf) is possible. I know that the highest possible number n that can't work is 37.

[0, 6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]

The plan (for now) is to check all numbers in the list against n, n + 1,... n+5 (which probably creates another for loop somewhere else), then print out 37.
I'm currently working through the OCW Intro to Programming course too. I just did the McNugget problem last week. I'm obviously a Python n00b as well, so take my advice with a grain of salt, but what you're trying to do with lists seems needlessly complicated.

I solved this one pretty easily just brute-forcing the possible combinations and using if/else statements to set the appropriate flags and keep track of a counting variable until I found 6 valid combos in a row.

The code is in a spoiler below in case you don't want to see before you solve it for yourself :

Spoiler:
Giant Tortoise is offline   Reply With Quote
Old 06-21-2011, 02:19 AM   #113
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

I have a solution, though it probably isn't as correct as yours. This is something I threw together over about 4 hours (which is extremely fast for me). I got a little to married to the list idea, but I'll work on a different solution at a later date.

Spoiler:
daveT is offline   Reply With Quote
Old 06-21-2011, 08:23 AM   #114
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,090
Re: ** Python Support Thread **

What's this for:
Quote:
list(set(nList))
You should very, very rarely need to construct two data structures like this. You definitely shouldn't do it multiple times. In your case, why wouldn't you just make nList and noValue set's to start?

In fact: I think you can just leave them as sets and then call max(yourSet) to get the largest number.

Edit: The reason I'm pointing that out is that learning to use the right data structure and avoiding lots of unnecessary conversions between data structures is pretty important. Much more important than trying to get code down to a small size or to figure out the very best algorithm to use.
jjshabado is offline   Reply With Quote
Old 06-21-2011, 08:56 AM   #115
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 334
Re: ** Python Support Thread **

Been trying to learn python for a few months on and off in the free time that I have (very little). This week it finally paid off as I could write a script that eases some of the work I was doing.
Even though it is probably a horrible piece of code from a programmers' view, I was still kinda proud. It's 8 times a similar thing after each other, but each one is dependent on the step before it, so I split it into 8 codeblocks. Is there a way to do that easier?
Rough example: I have x, out of that comes x1 x2 and x3, then I do roughly the same with those and get y11 y12 y13, y21 y22 and y23, and y31/y32/y33.
Is that possible to do in one codeblock without using so many for and if blocks that I tab out of the right side of my screen?

Attached the script btw, some variable names might be weird but they make sense if you know what the subject is about. It basically gets the relevant information out of (in this case) 8 documents and pastes it exactly in the format I want in a new file.

Also, first time dabbling in regexes so it took me a while to figure out all those to get exactly what I wanted.

Spoiler:
Slick Strawberry is offline   Reply With Quote
Old 06-22-2011, 07:40 AM   #116
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,821
Re: ** Python Support Thread **

Quote:
Originally Posted by jjshabado View Post
What's this for:


You should very, very rarely need to construct two data structures like this. You definitely shouldn't do it multiple times. In your case, why wouldn't you just make nList and noValue set's to start?

In fact: I think you can just leave them as sets and then call max(yourSet) to get the largest number.

Edit: The reason I'm pointing that out is that learning to use the right data structure and avoiding lots of unnecessary conversions between data structures is pretty important. Much more important than trying to get code down to a small size or to figure out the very best algorithm to use.
Okay, I'll look all this up.

Slick Strawberry: The reason we were using spoilers is so we don't show off the answers to the homework we are doing.
daveT is offline   Reply With Quote
Old 06-22-2011, 09:16 AM   #117
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 334
Re: ** Python Support Thread **

I'm using a spoiler because a spoiler gets a scrollbar if the script is too long, don't want my posts to be extremely long and I don't really know a good other way to show off my code.
Slick Strawberry is offline   Reply With Quote
Old 06-22-2011, 12:01 PM   #118
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,918
Re: ** Python Support Thread **

Use the [ code ] tag.

Last edited by tyler_cracker; 06-22-2011 at 12:01 PM. Reason: Or continue to use spoilers. Just know there's an option.
tyler_cracker is offline   Reply With Quote
Old 06-22-2011, 12:15 PM   #119
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 334
Re: ** Python Support Thread **

Didn't know the code tag also added scrollbars to the code, now I have a code tag in a spoiler tag...
Slick Strawberry is offline   Reply With Quote
Old 06-22-2011, 12:39 PM   #120
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,213
Re: ** Python Support Thread **

You can also use the php tag to get some syntax highlighting. It isn't perfect because it as it is geared towards php, but it does a decent job with any c-style language.
TheIrishThug 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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 08:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive