Chairish Mirae Kim
Chairish Mirae Kim

Reputation: 21

How can I tie lists, scalars and vectors together in this Python assignment?

Before I begin- let it be known that my class IS allowed to seek outside help for this assignment, provided we do not copy code directly. What I am asking for is help, not blatantly dishonestly obtained code. I am not intending to cheat by asking this question in any way.

Now that that's cleared up....

Here's the assignment:

#1: Write a function scalar_mult(s, v) that takes a number, s, and a list, v and returns the scalar multiple of v by s.

For example:

def scalar_mult(s, v):
  """    
  >>> scalar_mult(5, [1, 2])       
  [5, 10]       
  >>> scalar_mult(3, [1, 0, -1])      
  [3, 0, -3]       
  >>> scalar_mult(7, [3, 0, 5, 11, 2])       
  [21, 0, 35, 77, 14]    
  """

I have begun that part and this is what I have:

import math

s = input("Please enter a single number to be our scalar value: ")
v = input("Please enter a vector value, like [1, 2] or [5, 6, 3], to be our vector value: ")
#Note to self: TUPLES use the parentheses. LISTS use the brackets

print "scalar_mult(", s, ",", + v, "is:"
print v * s

scalar_mult(s, v)

But I keep getting this error message:

   print "scalar_mult(", s, ",", + v, "is:"
 TypeError: bad operand type for unary +: 'list'

Do you understand how to fix this?

And then there's a part two...

#2: Write a function replace(s, old, new) that replaces all occurrences of old with new in a string s.

For example:

def replace(s, old, new):     
  """      
  >>> replace('Mississippi', 'i', 'I')       
  'MIssIssIppI'       
  >>> s = 'I love spom!  Spom is my favorite food.  Spom, spom, spom, yum!'      
  >>> replace(s, 'om', 'am')       
  'I love spam!  Spam is my favorite food.  Spam, spam, spam, yum!'
  >>> replace(s, 'o', 'a')       
  'I lave spam!  Spam is my favarite faad.  Spam, spam, spam, yum!'     """ 
  """

I have not yet begun #2 but I don't really understand how to approach it. Any ideas on how to begin or how it works?

This is due Friday and was assigned yesterday. Just an FYI.

THANKS SO MUCH in advance to anyone who answers -- I know this is a pretty huge question to ask >.<

If you need any clarification of the assignment please tell me! Any help would be GREATLY appreciated :)

Upvotes: 2

Views: 2158

Answers (3)

Abhijit
Abhijit

Reputation: 63737

So what you are trying to achieve is not possible in Python. What you may possibly do is

>>> def scalar_mult(s, v):
    return [s*x for x in v]

>>> scalar_mult(5, [1, 2])
[5, 10]
>>> 

Now what you see above is called list comprehension. You can also do the same using map but may be you can take it as an exercise.

So if you look into the code deeply, you iterate across all the elements and for each element you multiply by the scalar value. The result is placed in a new list that you comprehend.

For the second question you should generally use the library replace but seems you may not use it. So you can go through the self commented code

>>> def replace(st,old,new):
    res=[] #As string is not mutable
           #so start with a list to store the result
    i=0    #you cannot change an index when using for with range
           #so use a while with an external initialization
    while i < len(st): #till the end of the string, len returns the string length
        if st[i:i+len(old)]==old: #you can index a string as st[begin:end:stride]
            res.append(new) #well if the string from current index of length
                            #equal to the old string matches the old string
                            #append the new string to the list
            i+=len(old)     #update the index to pass beyond the old string
        else:
            res.append(st[i]) #just append the current character
            i+=1              # Advance one character
    return ''.join(res) #finally join all the list element and generate a string

Upvotes: 0

James
James

Reputation: 1595

For the first part of your post, your error message is due to the fact that you're using the + operator with two operands: a , (a comma) and v, which is presumably a list. If you just want to print v, your print statement should look like:

print "scalar_mult(", s, ",", v, "is:"  # Note that I removed the `+`

For the second part, there are a number of ways to approach this question, but the easiest conceptually is to understand that a string in python is a list of characters, so you can operate on it similarly to an array. For example:

>>> example_string = 'hello world!'
>>> example_string[3]
'l'
>>>

I of course can't answer your homework assignment for you, but I would definitely recommend taking a look at python's built-in types documentation. It may help you to understand basic string operations so that you can build up your new function. Hopefully this will help you out a bit :)

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798754

The first is because Python is strongly typed, so you cannot add arbitrary types together.

>>> '%d %r' % (1, [2, 3])
'1 [2, 3]'

For the second, delegate to the string method that replaces.

Upvotes: 0

Related Questions