Homework help

Discussion in 'Python' started by bobby.connor@gmail.com, Apr 1, 2008.

  1. Guest

    Hey guys
    I haev this homework assignment due today
    I don't necessarily want the answers, but need help on how to approach
    it/the steps i need to solve the problems
    Thanks

    # (2 Points) Write a python function howMany(item,lst) which accepts
    an item and a lst of items and returns the number of times item occurs
    in lst. For example, howMany(3,[1,2,3,2,3]) should return 2.

    # (2 Points) Write a python function upTo(n) which accepts a non-
    negative number n and returns a list of numbers from 0 to n. For
    example, upTo(3) should return the list [0, 1, 2, 3].

    # (2 Points) Write a python function duplicate(lst) which accepts a
    lst of items and returns a list with the items duplicated. For
    example, duplicate([1,2,2,3]) should return the list [1, 1, 2, 2, 2,
    2, 3, 3].

    # (2 Points) Write a python function dotProduct(a,b) which accepts two
    lists of integers a and b that are of equal length and which returns
    the dot product of a and b. I.e., the sum a0 * b0 + ... + an-1 * bn-1
    where n is the length of the lists. For example:

    dotProduct([1,2,3],[4,5,6]) is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32

    # (2 Points) A pair (exp0, exp1) is a combination of expressions that
    are attached together by their joint membership in the pair. For
    example:

    >>> (1+2, 'This')

    (3, 'This')

    A component of a pair can be obtained using an index in brackets as
    with lists (and strings!). For example:

    >>> (33,44)[0]

    33

    Write a function zip(lst1, lst2) such that zip accepts two equal
    length lists and returns a list of pairs. For example, zip(['a', 'b',
    'c'], [10, 20, 30]) should evaluate to the list [('a', 10), ('b', 20),
    ('c', 30)].

    # (2 Points) Write a function unzip(lst) such that unzip accepts a
    list of pairs and returns two lists such that lst == zip(unzip(lst)).
    For example, unzip([('a', 10), ('b', 20), ('c', 30)] should evaluate
    to the pair (['a', 'b', 'c'], [10, 20, 30]).

    # (2 Points) Write a python function isAscending(lst) which accepts a
    non-empty list of integers and returns True if the numbers in the list
    are in ascending order. Otherwise it should return False. For example,
    isAscending([1]) should evaluate to True while isAscending([1,2,2])
    should return False.
    , Apr 1, 2008
    #1
    1. Advertising

  2. Paul Rubin Guest

    writes:
    > I don't necessarily want the answers, but need help on how to approach
    > it/the steps i need to solve the problems


    What parts are you having difficulty with? Are there some course
    materials and have you read them yet?
    Paul Rubin, Apr 1, 2008
    #2
    1. Advertising

  3. a écrit :
    > Hey guys
    > I haev this homework assignment due today


    Isn't it a bit too late to worry about it then ?
    Bruno Desthuilliers, Apr 1, 2008
    #3
  4. Guest

    On Apr 1, 12:17 pm, Paul Rubin <http://> wrote:
    > writes:
    > > I don't necessarily want the answers, but need help on how to approach
    > > it/the steps i need to solve the problems

    >
    > What parts are you having difficulty with? Are there some course
    > materials and have you read them yet?


    I just don't know how to start the problems off
    , Apr 1, 2008
    #4
  5. Tommy Grav Guest

    On Apr 1, 2008, at 12:29 PM, wrote:
    > On Apr 1, 12:17 pm, Paul Rubin <http://> wrote:
    >> writes:
    >>> I don't necessarily want the answers, but need help on how to
    >>> approach
    >>> it/the steps i need to solve the problems

    >>
    >> What parts are you having difficulty with? Are there some course
    >> materials and have you read them yet?

    >
    > I just don't know how to start the problems off


    How about reading the course material and then firing up python and
    trying
    some code. When you have done that and have questions regarding your
    code you are more than welcome back with questions.

    Cheers
    TG
    Tommy Grav, Apr 1, 2008
    #5
  6. Dave Hansen Guest

    On Apr 1, 11:29 am, wrote:
    > On Apr 1, 12:17 pm, Paul Rubin <http://> wrote:
    >
    > > writes:
    > > > I don't necessarily want the answers, but need help on how to approach
    > > > it/the steps i need to solve the problems

    >
    > > What parts are you having difficulty with? Are there some course
    > > materials and have you read them yet?

    >
    > I just don't know how to start the problems off


    Well, for the first problem, the first line is

    def howMany(item,lst):

    If you can't figure out where to go from there, start here:
    http://docs.python.org/tut/tut.html

    Regards,

    -=Dave
    Dave Hansen, Apr 1, 2008
    #6
  7. Terry Reedy Guest

    <> wrote in message
    news:...
    | Hey guys
    | I haev this homework assignment due today
    | I don't necessarily want the answers, but need help on how to approach
    | it/the steps i need to solve the problems
    | Thanks

    Read the section of the tutorial (and possibly Launguage Reference) on
    writing for statements and functions.
    Then read the sections of the Library Reference on builtin types and
    specifically sequences and more specifically lists.

    tjr
    Terry Reedy, Apr 1, 2008
    #7
  8. On Tue, 01 Apr 2008 09:11:12 -0700, bobby.connor wrote:

    > # (2 Points) Write a python function howMany(item,lst) which accepts
    > an item and a lst of items and returns the number of times item occurs
    > in lst. For example, howMany(3,[1,2,3,2,3]) should return 2.


    Study the methods on lists.

    > # (2 Points) Write a python function upTo(n) which accepts a non-
    > negative number n and returns a list of numbers from 0 to n. For
    > example, upTo(3) should return the list [0, 1, 2, 3].


    Study the built in functions. I don't know if it is considered cheating
    but you can get away with binding an existing one to the new name.

    > # (2 Points) Write a python function dotProduct(a,b) which accepts two
    > lists of integers a and b that are of equal length and which returns
    > the dot product of a and b. I.e., the sum a0 * b0 + ... + an-1 * bn-1
    > where n is the length of the lists. For example:
    >
    > dotProduct([1,2,3],[4,5,6]) is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32


    Again study the built in functions. Here the function from the `zip()`
    exercise below might be handy.

    > # (2 Points) A pair (exp0, exp1) is a combination of expressions that
    > are attached together by their joint membership in the pair. For
    > example:
    >
    >>>> (1+2, 'This')

    > (3, 'This')
    >
    > A component of a pair can be obtained using an index in brackets as
    > with lists (and strings!). For example:
    >
    >>>> (33,44)[0]

    > 33


    And the exercise to solve is!? Study the built in data types.

    > Write a function zip(lst1, lst2) such that zip accepts two equal
    > length lists and returns a list of pairs. For example, zip(['a', 'b',
    > 'c'], [10, 20, 30]) should evaluate to the list [('a', 10), ('b', 20),
    > ('c', 30)].


    Hey not even a rebinding necessary. :)

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Apr 1, 2008
    #8
  9. John Nagle Guest

    wrote:
    > Hey guys
    > I haev this homework assignment due today
    > I don't necessarily want the answers, but need help on how to approach
    > it/the steps i need to solve the problems
    > Thanks
    >
    > # (2 Points) Write a python function howMany(item,lst) which accepts
    > an item and a lst of items and returns the number of times item occurs
    > in lst. For example, howMany(3,[1,2,3,2,3]) should return 2.


    def howmany(item, lst) :
    return(len(filter(lambda x: x==item, lst)))
    >
    > # (2 Points) Write a python function upTo(n) which accepts a non-
    > negative number n and returns a list of numbers from 0 to n. For
    > example, upTo(3) should return the list [0, 1, 2, 3].


    def howmany(n) :
    return(range(n+1))

    That should get you started.

    John Nagle
    John Nagle, Apr 1, 2008
    #9
  10. Dan Upton Guest

    > > Write a function zip(lst1, lst2) such that zip accepts two equal
    > > length lists and returns a list of pairs. For example, zip(['a', 'b',
    > > 'c'], [10, 20, 30]) should evaluate to the list [('a', 10), ('b', 20),
    > > ('c', 30)].

    >
    > Hey not even a rebinding necessary. :)
    >


    We had some exercises like this in Scheme in my undergrad programming
    languages class (specifically, rewriting map/mapcar). It's not that
    the method doesn't already exist in the language, it's more about
    understanding what's going on at a lower level.
    Dan Upton, Apr 1, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    666
    Guest
    Feb 25, 2004
  2. Rhino

    need help with homework

    Rhino, Nov 16, 2003, in forum: Java
    Replies:
    1
    Views:
    300
    Roedy Green
    Nov 16, 2003
  3. Andrew Thompson
    Replies:
    33
    Views:
    880
    Alex Hunsley
    Oct 30, 2004
  4. xscarsofdesirex

    Homework Help! Urgent!

    xscarsofdesirex, Feb 27, 2005, in forum: Java
    Replies:
    5
    Views:
    521
    Alun Harford
    Feb 28, 2005
  5. Replies:
    2
    Views:
    403
    Roedy Green
    Oct 10, 2005
Loading...

Share This Page