Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
sorry :smith:

it's worth noting that the explanation in my last post is still wrong, I... think. the conditionals are definitely being evaluated top-to-bottom, but something is traveling bottom-to-top; otherwise how did (WATER, ZEBRA) get into the innermost loop?

functional programming is hard

Adbot
ADBOT LOVES YOU

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY
if i were forced to reformat that code, i'd probably go with

code:
# gently caress your 80 characters

return next((WATER, ZEBRA)
              	for (red, green, ivory, yellow, blue)			  	in c(orderings) 	if imright(green, ivory)
                for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) 	in c(orderings) 	if (Englishman is red) 	and (Norwegian is first) 	and nextto(Norwegian, blue)
                for (coffee, tea, milk, oj, WATER) 				in c(orderings) 	if (coffee is green) 	and (Ukranian is tea) 		and (milk is middle)
                for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) 	in c(orderings) 	if (Kools is yellow) 	and (LuckyStrike is oj) 	and (Japanese is Parliaments)
                for (dog, snails, fox, horse, ZEBRA) 				in c(orderings)		if (Spaniard is dog) 	and (OldGold is snails) 	and nextto(Chesterfields, fox) 		and nextto(Kools, horse)
                )
but my god there has to be a better way, probably involving pulling each of those lines out into their own function so you could see how they interact

coffeetable fucked around with this message at 06:02 on Jul 24, 2013

qntm
Jun 17, 2009
:ssh: the most readable form doesn't use list comprehensions

qntm
Jun 17, 2009
Python code:
for (red, green, ivory, yellow, blue) in c(orderings):
  if not imright(green, ivory): continue
  for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in c(orderings):
    if not Englishman is red       : continue
    if not Norwegian is first      : continue
    if not nextto(Norwegian, blue) : continue
    for (coffee, tea, milk, oj, WATER) in c(orderings):
      if not coffee is green : continue
      if not Ukranian is tea : continue
      if not milk is middle  : continue
      for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in c(orderings):
        if not Kools is yellow         : continue
        if not LuckyStrike is oj       : continue
        if not Japanese is Parliaments : continue
        for (dog, snails, fox, horse, ZEBRA) in c(orderings):
          if not Spaniard is dog            : continue
          if not OldGold is snails          : continue
          if not nextto(Chesterfields, fox) : continue
          if not nextto(Kools, horse)       : continue
          return (WATER, ZEBRA)
hope this makes things clear

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
anyway here's the entire code:

Python code:
import itertools
import time

def imright(h1, h2):
    "House h1 is immediately right of h2 if h1-h2 == 1."
    return h1-h2 == 1

def nextto(h1, h2):
    "Two houses are next to each other if they differ by 1."
    return abs(h1-h2) == 1

def zebra_puzzle():
    "Return a tuple (WATER, ZEBRA indicating their house numbers."
    houses = first, _, middle, _, _ = [1, 2, 3, 4, 5]
    orderings = list(itertools.permutations(houses)) # 1
    return next((WATER, ZEBRA)
                for (red, green, ivory, yellow, blue) in c(orderings)
                if imright(green, ivory)
                for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in c(orderings)
                if Englishman is red
                if Norwegian is first
                if nextto(Norwegian, blue)
                for (coffee, tea, milk, oj, WATER) in c(orderings)
                if coffee is green
                if Ukranian is tea
                if milk is middle
                for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in c(orderings)
                if Kools is yellow
                if LuckyStrike is oj
                if Japanese is Parliaments
                for (dog, snails, fox, horse, ZEBRA) in c(orderings)
                if Spaniard is dog
                if OldGold is snails
                if nextto(Chesterfields, fox)
                if nextto(Kools, horse)
                )

def c(sequence):
    c.starts += 1
    for item in sequence:
        c.items += 1
        yield item

def instrument_fn(fn, *args):
    c.starts, c.items = 0, 0
    result = fn(*args)
    print('%s got %s with %5d iters over %7d items'%(
        fn.__name__, result, c.starts, c.items))
c and instrument_fn are just some temporary debug functions for counting the number of calls and doesn't really matter (it was just an excuse for him to introduce some new stuff like the unpacking notation)

here's one way to rewrite the function but it's pretty much the same thing qntm did and I didn't see his post before. :rolleyes:

Python code:
def zebra_puzzle():
    houses = first, _, middle, _, _ = [1, 2, 3, 4, 5]
    orderings = list(itertools.permutations(houses))
    for (red, green, ivory, yellow, blue) in orderings:
        if imright(green, ivory):
            for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings:
                if Englishman is red and Norwegian is first and nextto(Norwegian, blue):
                    for (coffee, tea, milk, oj, WATER) in orderings:
                        if coffee is green and Ukranian is tea and milk is middle:
                            for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in orderings:
                                if Kools is yellow and LuckyStrike is oj and Japanese is Parliaments:
                                    for (dog, snails, fox, horse, ZEBRA) in orderings:
                                        if Spaniard is dog and OldGold is snails and nextto(Chesterfields, fox) and nextto(Kools, horse):
                                            return (WATER, ZEBRA)
geez, so much indentation.

I don't think in this case the comprehension would be faster because he's only getting the first possible solution but even so it looks better to me.



a similar brute force case where it would matter is in this problem. here's one of my first attempts before figuring the real trick:

Python code:
from itertools import combinations

COUNT_MODULO = 1000000009

def count(seq, bound):
    lseq = len(seq)
    return len({hash(subseq[:i] + (new_element, ) + subseq[i:])
                for subseq in set(combinations(seq, lseq-1))
                for i in xrange(lseq)
                for new_element in xrange(1, bound+1)}) % COUNT_MODULO
using the set comprehension here is considerably faster than going with something like
Python code:
def count(seq, bound):
    lseq = len(seq)
    s = set()
    for subseq in set(combinations(seq, lseq-1)):
        for i in xrange(lseq):
            for new_element in xrange(1, bound+1):
                s.add(hash(subseq[:i] + (new_element,) + subseq[i:]))
    return len(s) % COUNT_MODULO
but I guess from now on I'll stick with not using those "impossible to write in a 79 characters line" comprehensions because it's obfuscating the code and being a kind of premature optimization. it's just that I'm so used to it.

MeruFM
Jul 27, 2010
This is definitely the most complex a single list comprehension should be
code:
for user in [post.user() for post in thread if poo poo(post) is True]:
    user.ban()

MeruFM
Jul 27, 2010

Symbolic Butt posted:

code:
for:
    if:
        for:
            if:
                for:
                    if:
                        for:
                            if:
                                for:
                                    if:

FamDav
Mar 29, 2008
Can't you just use prolog

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
oh hey that for-if ladder actually looks kinda cool when you put it this way

*farts*

I wonder if the zebra puzzle is one of those things that can be done particularly well in prolog from what I hear people say about it

edit: what the gently caress FamDav

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Symbolic Butt posted:

geez, so much indentation.

speaking only for myself, there's no such thing as "too much indentation". i consider it a built-in requirement of the code, even when i'm not using python

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

FamDav posted:

Can't you just use prolog

Symbolic Butt posted:

I wonder if the zebra puzzle is one of those things that can be done particularly well in prolog from what I hear people say about it

edit: what the gently caress FamDav

somebody light the tef-signal; i want to see what prolog would look like :allears:

uG
Apr 23, 2003

by Ralp
http://rosettacode.org/wiki/Zebra_puzzle#Prolog

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

thanks :tipshat:

now if only i can figure out what's actually happening :) (it definitely looks like this is something prolog is good at; much less convoluted than a lot of other languages)

Elder Postsman
Aug 30, 2000


i used hot bot to search for "teens"

PleasingFungus posted:

that is extremely silly, and certainly bad practice, but I can't bring myself to be upset about it. (the teddy in your av probably helps. :3:)

the post you quoted suggests one way to be a little more explicit about what you're doing vis-a-vis string formatting:

Python code:
            qry = "OBJECTID in ({})".format(','.join(oidstrlist))
build a comma-separated list of elements, then drop 'em between parens. it's the same result as the tuple approach, just expresses your intent more clearly in code, rather than indirectly indicating it by switching data structures.

holy gently caress dude

i changed it to work this way (though it needs to be "OBJECTID in ({0})" because python 2.6 (that's what arc10 supports :( )) and it now takes 4 seconds to select 371000 features, instead of the hour and a half it took the original way

this owns, thanks!

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

dur posted:

holy gently caress dude

i changed it to work this way (though it needs to be "OBJECTID in ({0})" because python 2.6 (that's what arc10 supports :( )) and it now takes 4 seconds to select 371000 features, instead of the hour and a half it took the original way

this owns, thanks!

and here I thought I was just sperging about Proper Usage! super glad I could help. :)

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

MeruFM posted:

This is definitely the most complex a single list comprehension should be
code:
for user in [post.user() for post in thread if poo poo(post) is True]:
    user.ban()

I don't understand. why include the conditional check if it's always going to return True?

double sulk
Jul 2, 2010

PleasingFungus posted:

I don't understand. why include the conditional check if it's always going to return True?

:v:

dragon enthusiast
Jan 1, 2010
yospos a few months ago there was chatte about natural language unit test generators where you could go like

add(2,4) should equal 6
butts("piss") should throw PoopException

I don't remember the names of any of those generators and now I want to look at some of them and Google isn't helping

Nomnom Cookie
Aug 30, 2009



unit tests is requirements with code inside

-- a thing dumb babby p-langers say

JewKiller 3000
Nov 28, 2006

by Lowtax
but if your unit tests have code inside, who's to say that code does what you meant? you need unit tests for it

uG
Apr 23, 2003

by Ralp
my unit tests are just generated boilerplate files, no code

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
some day I will work in a company with a rich testing environment

that day is not today

JewKiller 3000
Nov 28, 2006

by Lowtax

PleasingFungus posted:

some day I will work in a company with a rich testing environment

NOPE

Bloody
Mar 3, 2013

uG posted:

my unit tests are just generated boilerplate files, no code

this except pretty much my whole project

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

wait, you're the guy who claims that java/python/etc aren't "real programming languages" (as opposed to esoteric academic functional languages, aka, all functional languages)

your opinions are a very strange mix.

JewKiller 3000
Nov 28, 2006

by Lowtax
java is a real programming language although its subtyping is probably undecidable and it needs better support for first-class functions. algebraic datatypes would be nice too. so basically scala

python is a joke

Nomnom Cookie
Aug 30, 2009



The actual most important thing about a language is available libraries. This makes java/scala the best language for any purpose permitting a jvm

Shaggar
Apr 26, 2006

PleasingFungus posted:

some day I will work in a company with a rich testing environment

that day is not today

Shaggar
Apr 26, 2006

Nomnom Cookie posted:

The actual most important thing about a language is available libraries. This makes java/scala the best language for any purpose permitting a jvm

Most of the popular java libs have c# versions or standard library equivalents these days.

JewKiller 3000
Nov 28, 2006

by Lowtax
c# is better than java but not better than f# :v:

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
ruby's active record/active support/active model libraries are so good i cant imagine ever needing anything else

Nomnom Cookie
Aug 30, 2009



Shaggar posted:

Most of the popular java libs have c# versions or standard library equivalents these days.

Lord prevent me from encountering a problem the C# stdlib can solve

Nomnom Cookie
Aug 30, 2009



chumpchous posted:

ruby's active record/active support/active model libraries are so good i cant imagine ever needing anything else

Wow rails has static typing? People really aren't kidding when they say rails programmers don't know ruby

uG
Apr 23, 2003

by Ralp

chumpchous posted:

ruby's active record/active support/active model libraries are so good i cant imagine ever needing anything else

you still need C to do all the actual work for your ruby bindings if you want things to finish running in a timely fashion (probably not as you are using ruby)

Bloody
Mar 3, 2013

Nomnom Cookie posted:

Lord prevent me from encountering a problem the C# stdlib can solve

im struggling to think of a problem the c# stdlib can't solve

Bloody
Mar 3, 2013

uG posted:

you still need C to do all the actual work

this is true of everything

JewKiller 3000
Nov 28, 2006

by Lowtax
ruby is a joke

uG
Apr 23, 2003

by Ralp
fun fact: all submissions for a cpan username get approved by a human, but only because nobody wants to update the centuries old code

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

JewKiller 3000 posted:

ruby is a joke

sayin' that around here sure takes some monads

Adbot
ADBOT LOVES YOU

Nomnom Cookie
Aug 30, 2009



Bloody posted:

im struggling to think of a problem the c# stdlib can't solve

maybe i just hate programming then

  • Locked thread