Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'element': 0.07; '__name__': 0.09; 'skip:# 30': 0.09; 'unmodified': 0.09; 'runs': 0.10; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; '2.7': 0.14; "'__main__':": 0.16; '(other': 0.16; '8bit%:32': 0.16; 'c):': 0.16; 'count.': 0.16; 'subject:broken': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'example': 0.22; 'email addr:gmail.com>': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'towards': 0.31; '13,': 0.31; 'assert': 0.31; 'anyone': 0.31; 'url:python': 0.33; 'problem': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'example,': 0.37; 'pm,': 0.38; 'does': 0.39; '12,': 0.39; 'url:mail': 0.40; 'skip:\xc2 10': 0.60; 'break': 0.61; 'show': 0.63; 'sum': 0.64; 'to:addr:gmail.com': 0.65; '8bit%:100': 0.72; 'jul': 0.74; '13)': 0.84; '13:': 0.84; 'sum.': 0.84; '-\xc2\xa0': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=v/y2exK8/fC74SLeLgG853t7842LEqPQSMBOtrVQPfw=; b=VQE54qvmBGtRR6mNDizaxm5SOR0V5ULvwSNjFvBxe4yCYpN7QbWGX/UBQyGSPjCSad 2c5K/9DgVMZfjFpWGQCwfawcbLa4YAl/x1StSjVTBKNKq1ZKVWrrDZTW51BfKK1bHwfx +1Jo7MN29O5YLIk9i9aBa9X2gQUMHanNe4Y+WPtMK8u4tgF7wr/J+1vnGNJdH3IJOj9k 47JJRKV9q2//TpLLzdgBlPh23sdWRDQ+zPpoR/WTjzdbjjnG/ajfXgLblgAHW+CZG5/1 jxc6tW32yPbsVPhCOuFH5xsPGrvTL2ACjUmw46urTTtEwqMJw/uM1Ye7tfZoVl5qUQEe kMBQ== MIME-Version: 1.0 X-Received: by 10.52.252.226 with SMTP id zv2mr7078429vdc.19.1405225305518; Sat, 12 Jul 2014 21:21:45 -0700 (PDT) In-Reply-To: References: Date: Sat, 12 Jul 2014 21:21:45 -0700 Subject: Re: codingbat question broken? From: Dan Stromberg To: Rodrick Brown Content-Type: multipart/alternative; boundary=001a1135ed222a368404fe0b8370 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 185 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405225308 news.xs4all.nl 2853 [2001:888:2000:d::a6]:52833 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74395 --001a1135ed222a368404fe0b8370 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable This runs on 2.7 or 3.4, unmodified (other than the #!): #!/usr/local/cpython-2.7/bin/python #!/usr/local/cpython-3.4/bin/python def lucky_sum(*list_): lucky_total =3D 0 for element in list_: if element =3D=3D 13: break lucky_total +=3D element return lucky_total if __name__ =3D=3D '__main__': print('starting tests') assert lucky_sum(1, 2, 3) =3D=3D 6 assert lucky_sum(1, 2, 13) =3D=3D 3 assert lucky_sum(1, 13, 3) =3D=3D 1 print('ending tests') On Sat, Jul 12, 2014 at 7:05 PM, Rodrick Brown wrote: > I'm working on the following problem set from codingbat.com > > http://codingbat.com/prob/p107863 > > Given 3 int values, a b c, return their sum. However, if one of the value= s > is 13 then it does not count towards the sum and values to its right do n= ot > count. So for example, if b is 13, then both b and c do not count. > > lucky_sum(1, 2, 3) =E2=86=92 6 > lucky_sum(1, 2, 13) =E2=86=92 3 > lucky_sum(1, 13, 3) =E2=86=92 1 > > The solution I came up with was - > > def lucky_sum(a, b, c): > t =3D 0 > for ints in (a, b, c): > if a =3D=3D 13: > t =3D b + c > elif b =3D=3D 13: > t =3D a > elif c =3D=3D 13: > t =3D a + b > else: > t =3D a + b + c > return t > > However the following tests fail > > lucky_sum(13, 2, 3) =E2=86=92 05X lucky_sum(13, 2, 13) =E2=86=92 015X= lucky_sum(13, > 13, 2) =E2=86=92 015X > > Can anyone show me an example where all test are success? > > > -- > https://mail.python.org/mailman/listinfo/python-list > > --001a1135ed222a368404fe0b8370 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

This runs on 2.7 or 3.4, unmodified (other than the #!= ):

#!/usr/local/cpython-2.7/bin/python
#!/usr/local/cpython-3.4/b= in/python

def lucky_sum(*list_):
=C2=A0=C2=A0=C2=A0 lucky_total = =3D 0
=C2=A0=C2=A0=C2=A0 for element in list_:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if element =3D=3D 13:
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 break
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 lucky_total +=3D element
=C2=A0= =C2=A0=C2=A0 return lucky_total

if __name__ =3D=3D '__main__'= ;:
=C2=A0=C2=A0=C2=A0 print('starting tests')
=C2=A0=C2=A0=C2= =A0 assert lucky_sum(1, 2, 3) =3D=3D 6
=C2=A0=C2=A0=C2=A0 assert lucky_sum(1, 2, 13) =3D=3D 3
=C2=A0=C2=A0=C2= =A0 assert lucky_sum(1, 13, 3) =3D=3D 1
=C2=A0=C2=A0=C2=A0 print('en= ding tests')

On Sat, Jul 12, 2014 at 7:05 PM, Rodrick Brown <rodrick.brown@gma= il.com> wrote:
I'm working on the following problem set from codingbat.com=C2=A0


Given 3 int values, a b c, return their sum. However, if = one of the values is 13 then it does not count towards the sum and values t= o its right do not count. So for example, if b is 13, then both b and c do = not count.=C2=A0

lucky_sum(1, 2, 3) =E2=86=92 6
lucky_sum(1, 2, 13) =E2=86=92 3
lucky_sum(1, 13, 3) =E2= =86=92 1

The solution I came up with was -=C2= =A0

def lucky_sum(a, b, c):
=C2= =A0 t =3D 0
=C2=A0 for ints in (a, b, c):
=C2=A0 = =C2=A0 if a =3D=3D 13:
=C2=A0 =C2=A0 =C2=A0 t =3D b + c
=C2=A0 =C2=A0 elif b =3D=3D = 13:
=C2= =A0 =C2=A0 =C2=A0 t =3D a=C2=A0
=C2=A0 =C2=A0 elif c =3D=3D 13= :=C2=A0
=C2=A0 =C2=A0 =C2=A0 t =3D a + b
=C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 t =3D a + b + c
=C2= =A0 return t=C2=A0

However the fol= lowing tests fail=C2=A0

lucky_sum(13, 2, 3) = =E2=86=92 05X=C2=A0=C2=A0=C2=A0= =C2=A0
lucky_sum(13, 2, 13) =E2=86=92 015X=C2=A0=C2=A0=C2=A0=C2=A0
lucky_sum(13, 13, 2) =E2= =86=92 015X=C2=A0 =C2=A0

Can anyone show me an example where al= l test are success?=C2=A0


--
https://mail.python.org/mailman/listinfo/python-list


--001a1135ed222a368404fe0b8370--