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


Groups > comp.lang.python > #66044 > unrolled thread

Combination Function Help

Started bykjakupak@gmail.com
First post2014-02-12 07:20 -0800
Last post2014-02-12 18:22 -0500
Articles 10 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#66044 — Combination Function Help

Fromkjakupak@gmail.com
Date2014-02-12 07:20 -0800
SubjectCombination Function Help
Message-ID<bd88bd17-76d0-43de-9649-daa9ef86155e@googlegroups.com>
So I need to write a function based off of nCr, which I have here:

def choices(n, k):
    if n == k:
        return 1
    if k == 1:
        return n
    if k == 0:
        return 1
    return choices(n - 1, k) + choices(n - 1, k - 1)

It works fine, but then I need to add in so that the user can have an input box for n and k.

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)

n_input = int(input("Number of courses you like: "))
k_input = int(input("Number of courses you can register for: "))

The above doesn't return any value at all. And then I need it to print the total number of ways of choosing k out of n courses, which I only have:

print ("Total number of ways of choosing %d out of %d courses: " % (n_input, k_input))


So basically I need help figuring out why it won't return any value and how to print that final value.

[toc] | [next] | [standalone]


#66047

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-12 15:40 +0000
Message-ID<mailman.6760.1392219671.18130.python-list@python.org>
In reply to#66044
On 12/02/2014 15:20, kjakupak@gmail.com wrote:
> So I need to write a function based off of nCr, which I have here:
>
> def choices(n, k):
>      if n == k:
>          return 1
>      if k == 1:
>          return n
>      if k == 0:
>          return 1
>      return choices(n - 1, k) + choices(n - 1, k - 1)
>
> It works fine, but then I need to add in so that the user can have an input box for n and k.
>
> 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)
>
> n_input = int(input("Number of courses you like: "))
> k_input = int(input("Number of courses you can register for: "))
>
> The above doesn't return any value at all. And then I need it to print the total number of ways of choosing k out of n courses, which I only have:
>
> print ("Total number of ways of choosing %d out of %d courses: " % (n_input, k_input))
>
>
> So basically I need help figuring out why it won't return any value and how to print that final value.
>

Actually calling choices with n_input and k_input as inputs might help 
your cause.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#66049

Fromkjakupak@gmail.com
Date2014-02-12 07:56 -0800
Message-ID<11e9f551-f920-428c-895d-a5d19a4d7734@googlegroups.com>
In reply to#66047
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

[toc] | [prev] | [next] | [standalone]


#66051

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-02-12 11:21 -0500
Message-ID<mailman.6762.1392222089.18130.python-list@python.org>
In reply to#66049

[Multipart message — attachments visible in raw view] — view raw

Y
On Feb 12, 2014 11:00 AM, <kjakupak@gmail.com> wrote:
>
> 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)
Following line never runs
>     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
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#66053

FromJohn Ladasky <john_ladasky@sbcglobal.net>
Date2014-02-12 08:24 -0800
Message-ID<f108f447-9e92-42cd-b8f8-830a4ee0c4f0@googlegroups.com>
In reply to#66049
On Wednesday, February 12, 2014 7:56:05 AM UTC-8, kjak...@gmail.com wrote:
[snip]
> choices(n, k)
>
> Changed it like you said, didn't work

What are you doing with the value returned by the function, choices()?  Right now, you aren't doing anything with it.  You are throwing it away.  That's the beginning of your problem.

In your own program, you have two other working examples of functions which return and use values.  You should study these.  

The first, and easier function call for you to understand is the call to int().  You call it twice.  The first time, you ASSIGN the name "n" to the value returned by int().  The second time, you assign the name "k" to the value returned by another run of int().

Assigning the names to the values returned by the function calls is what allows you to work with those values later, on other lines of your program.  Without assigning n and k, attempting to call choices(n,k) would generate an error.

If int() was written in Python (it probably isn't; most of the Python core is written in C) and you looked at int()'s source code, it would have a "return" statement in it, just like your choices() does.

Does that help you to see how you should modify the last line of your program, and what you might do after that?

There's a second function call in your program as well: you call input().  This function call would be harder for a beginner to understand, because it happens to be nested inside the int() function call.  Let's take the simpler one first.

[toc] | [prev] | [next] | [standalone]


#66056

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-12 16:55 +0000
Message-ID<mailman.6765.1392224130.18130.python-list@python.org>
In reply to#66049
On 12/02/2014 15:56, kjakupak@gmail.com wrote:
> 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
>

Changed it like who said?  I'm assuming myself, but with no context you 
can't always tell.  Also, stating "didn't work" is often useless to us. 
  What didn't work?  Why didn't it work?  Here it's obvious, you're 
throwing away the return value from your function call.  Either save the 
return value and print it or add the function call directly to your 
original print call.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#66070

FromDave Angel <davea@davea.name>
Date2014-02-12 13:38 -0500
Message-ID<mailman.6775.1392230093.18130.python-list@python.org>
In reply to#66049
 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

[toc] | [prev] | [next] | [standalone]


#66114

Fromkjakupak@gmail.com
Date2014-02-12 14:59 -0800
Message-ID<c12f9609-1206-4741-8af3-ebbe7e20fbe2@googlegroups.com>
In reply to#66070
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)

comb = choices(n, k)
print comb

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: "))

Still isn't working though..

[toc] | [prev] | [next] | [standalone]


#66117

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-12 23:15 +0000
Message-ID<mailman.6805.1392246924.18130.python-list@python.org>
In reply to#66114
On 12/02/2014 22:59, kjakupak@gmail.com wrote:
> 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)
>
> comb = choices(n, k)
> print comb
>
> 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: "))
>
> Still isn't working though..
>

Please be precise, "still isn't working" tells us nothing.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#66118

FromDave Angel <davea@davea.name>
Date2014-02-12 18:22 -0500
Message-ID<mailman.6806.1392247128.18130.python-list@python.org>
In reply to#66114
 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)
> 
> comb = choices(n, k)
> print comb
> 
> 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: "))
> 
> Still isn't working though..
> 

Still haven't figured how to post a traceback? 

The two lines defining and referencing comb need to be AFTER n and
 k are defined and given values. 

Also, what version of Python are you running?  Version 3.x
 requires parentheses around the arguments to the print function.
 

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web