Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66070

Re: Combination Function Help

From Dave Angel <davea@davea.name>
Subject Re: Combination Function Help
Date 2014-02-12 13:38 -0500
Organization news.gmane.org
References <bd88bd17-76d0-43de-9649-daa9ef86155e@googlegroups.com> <mailman.6760.1392219671.18130.python-list@python.org> <11e9f551-f920-428c-895d-a5d19a4d7734@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6775.1392230093.18130.python-list@python.org> (permalink)

Show all headers | View raw


 kjakupak@gmail.com Wrote in message:
> def choices(n, k):
>     if k == 1:
>         return n
>     if n == k:
>         return 1
>     if k == 0:
>         return 1
>     return choices(n - 1, k) + choices(n - 1, k - 1)
>     print ("Total number of ways of choosing %d out of %d courses: " % (n, k))
> 
> n = int(input("Number of courses you like: "))
> k = int(input("Number of courses you can register for: "))
> choices(n, k)
> 
> Changed it like you said, didn't work
> 

I see at least two problems with that code
:

The line with the print function will never get called, since it
 follows an unconditional return statement.  You shouldn't print
 there anyway,  just move it to top level,  after the two calls to
 input.  Don't forget to dedent it.

You don't use or save the return value of choices.  You should
 probably assign it to a name like combinations,  then print it on
 the following line.

The recursive function choices doesn't look right to me, but I'm
 not sure either way.  I have not tested it.

-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Combination Function Help kjakupak@gmail.com - 2014-02-12 07:20 -0800
  Re: Combination Function Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-12 15:40 +0000
    Re: Combination Function Help kjakupak@gmail.com - 2014-02-12 07:56 -0800
      Re: Combination Function Help Joel Goldstick <joel.goldstick@gmail.com> - 2014-02-12 11:21 -0500
      Re: Combination Function Help John Ladasky <john_ladasky@sbcglobal.net> - 2014-02-12 08:24 -0800
      Re: Combination Function Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-12 16:55 +0000
      Re: Combination Function Help Dave Angel <davea@davea.name> - 2014-02-12 13:38 -0500
        Re: Combination Function Help kjakupak@gmail.com - 2014-02-12 14:59 -0800
          Re: Combination Function Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-12 23:15 +0000
          Re: Combination Function Help Dave Angel <davea@davea.name> - 2014-02-12 18:22 -0500

csiph-web