2

始める前に、コードを直接コピーしない限り、私のクラスはこの課題について外部の助けを求めることが許可されていることをお知らせします。私が求めているのはヘルプであり、あからさまに不正に入手したコードではありません。私は、この質問をすることで決してごまかすつもりはありません。

これでスッキリ…。

割り当ては次のとおりです。

#1: 数値 s とリスト v を取り、v の s 倍のスカラー倍数を返す関数 scalar_mult(s, v) を作成します。

例えば:

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]    
  """

私はその部分を始めました、そしてこれは私が持っているものです:

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)

しかし、私はこのエラーメッセージを受け取り続けます:

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

これを修正する方法を理解していますか?

そして、パート2があります...

#2: 文字列 s 内のすべての old を new に置き換える関数 replace(s, old, new) を記述します。

例えば:

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!'     """ 
  """

#2はまだ始めていませんが、アプローチ方法がよくわかりません。どのように開始するか、またはどのように機能するかについてのアイデアはありますか?

これは金曜日の期限で、昨日割り当てられました。参考までに。

答えてくれた人には、事前にとても感謝しています -- これはかなり大きな質問です >.<

割り当ての説明が必要な場合は、教えてください。どんな助けでも大歓迎です:)

4

3 に答える 3

1

投稿の最初の部分では、エラー メッセージは、+演算子を 2 つのオペランド,(コンマ) と (vおそらくリスト) で使用していることが原因です。を印刷するだけの場合vprintステートメントは次のようになります。

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

2 番目の部分では、この質問にアプローチする方法はいくつかありますが、概念的に最も簡単なのは、Python の文字列は文字のリストであり、配列と同様に操作できることを理解することです。例えば:

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

もちろん、私はあなたの宿題に答えることができませんが python の組み込み型のドキュメント を確認することをお勧めします。新しい関数を構築できるように、基本的な文字列操作を理解するのに役立つ場合があります。うまくいけば、これはあなたを少し助けます:)

于 2012-03-28T20:44:03.890 に答える
0

あなたが達成しようとしていることは、Pythonでは不可能です。あなたができることは

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

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

上記の内容は、リスト内包表記と呼ばれます。マップを使用して同じことを行うこともできますが、演習として使用できる場合があります。

したがって、コードを深く調べると、すべての要素を繰り返し処理し、要素ごとにスカラー値を掛けます。結果は、理解できる新しいリストに配置されます。

2 番目の質問については、通常、ライブラリのreplaceを使用する必要がありますが、使用しないようです。したがって、自己コメント付きのコードを実行できます

>>> 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
于 2012-03-28T20:26:21.777 に答える
0

1 つ目は、Python は厳密に型指定されているため、任意の型を一緒に追加できないためです。

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

2 つ目は、置き換える文字列メソッドにデリゲートします。

于 2012-03-28T20:21:04.540 に答える