solutions from class yesterday

11 views
Skip to first unread message

Tim Menzies

unread,
Feb 21, 2014, 2:02:13 PM2/21/14
to cs310
def item(x) :
if isinstance(x,(tuple,list)):
for y in x:
for z in item(y): yield z
else: yield x

--
:: there are some who call me... tim.m...@gmail.com
:: prof @ cs.ai.se.csee.wvu.usa.sol.virgo.all.nil
:: +1-304-376-2859
:: http://menzies.us (skype = menzies.tim)

<hubris>
vita= http:// goo.gl/8eNhY
pubs= http:// goo.gl/8KPKA
stats= http:// goo.gl/vggy1
wow = http:// goo.gl/2Wg9A
</hubris>
parse.txt

cschaozhang

unread,
Feb 25, 2014, 9:24:30 PM2/25/14
to cs...@googlegroups.com, tim.m...@gmail.com
def item(x):
   if isinstance(x, (tuple,list)):
       for y in item(x): yield y
   else: yield x

I guess this works.

Tim Menzies

unread,
Feb 25, 2014, 9:43:36 PM2/25/14
to cschaozhang, cs310
er... not quite. your solution suffers from the problem mentioned in class: you are recursing on the passed-in argument with no modification. so you code recurses infinitely till it runs of of stack.

on the other hand, my version recurses on items from all y items within the  passed-in list

def item(x) :
  if isinstance(x,(tuple,list)):
    for y in x:
      for z in item(y): yield z
  else: yield x

t

cschaozhang

unread,
Feb 25, 2014, 10:00:08 PM2/25/14
to cs...@googlegroups.com, tim.m...@gmail.com
def item(x):
    for a in x:
        if isinstance(a, list):
            for b in item(a): yield b
        else:
            yield a

made a mistake: forgot "for a in x"

thanks.


On Friday, February 21, 2014 2:02:13 PM UTC-5, tim.menzies wrote:

Tim Menzies

unread,
Feb 26, 2014, 9:39:27 PM2/26/14
to cschaozhang, cs310
that works.... unless the *first* thing passed-in is not a list

t

Cameron Meador

unread,
Feb 27, 2014, 11:18:08 AM2/27/14
to cs...@googlegroups.com
Hello,
Sorry this is really short notice, but i just walked out to my car a few minutes ago and it wouldn't start so I'm not sure if I'll be able to make it I the midterm in time to finish. Can I please schedule a makeup sometime today or tomorrow. As I'm not sure when I'll be able to get my car running or find a ride. Thanks.

Sent from my iPhone
> --
> You received this message because you are subscribed to the Google Groups "cs310" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cs310+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages