I am a beginner working my way through How To Think Like A Computer Scientist. I am having a problem with an exercise that multiplies a matrix by a scalar. def scalar_mult(n, m), n being the scalar and m being the matrix.
My code works but I guess its not a pure function because if I put in
>>> m = [[1, 2], [3, 4]]
>>> scalar_mult(3, m)
my return is [[3, 6], [9, 12]] which is correct, but when i check m after I run my function I get [[3, 6], [9, 12]] instead of [[1, 2], [3, 4]]. I don't understand this because I cloned m and used mx throughout the function. This is the first time I have posted on a forum so please excuse me if my format is incorrect. -
def scalar_mult(n, m):
-
mx = m[:]
-
(my code is here)
-
The problem is that you have a list of lists. You made a copy of the original list, but you did not make copies of the internal lists. Do this: - def scalar_mult(n, m):
-
mx = [item[:] for item in m]
-
x = 0
-
while x < len(mx):
-
for index, value in enumerate(mx[x]):
-
mx[x][index] = mx[x][index] * n
-
x += 1
-
return mx
Following is an example that makes a new list. It uses recursion to get at nested lists also. - def scalar_mult(seq, n):
-
results = []
-
for elem in seq:
-
if isinstance(elem, (list, tuple)):
-
results.append(scalar_mult(elem, n))
-
else:
-
results.append(elem * n)
-
return results
-
-
seq = [[1, 2], [3, 4]]
-
seq1 = scalar_mult(seq, 3)
-
print seq
-
print seq1
-
-
seq2 = ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
-
seq3 = scalar_mult(seq2, 3)
-
print seq2
-
print seq3
-
-
''' The output:
-
>>> [[1, 2], [3, 4]]
-
[[3, 6], [9, 12]]
-
((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
-
[[3, 6], [9, [12, 15, 18], [[18, 21, 24], [27, 60]]]]
-
'''
3 2126
Hi
Welcome to the forum. Please go ahead and post the rest of your code.
Thanks
Glenton
@Glenton
Glenton, here is my code. -
def scalar_mult(n, m):
-
mx = m[:]
-
x = 0
-
while x < len(mx):
-
for index, value in enumerate(mx[x]):
-
mx[x][index] = mx[x][index] * n
-
x += 1
-
return mx
-
bvdet 2,851
Expert Mod 2GB
The problem is that you have a list of lists. You made a copy of the original list, but you did not make copies of the internal lists. Do this: - def scalar_mult(n, m):
-
mx = [item[:] for item in m]
-
x = 0
-
while x < len(mx):
-
for index, value in enumerate(mx[x]):
-
mx[x][index] = mx[x][index] * n
-
x += 1
-
return mx
Following is an example that makes a new list. It uses recursion to get at nested lists also. - def scalar_mult(seq, n):
-
results = []
-
for elem in seq:
-
if isinstance(elem, (list, tuple)):
-
results.append(scalar_mult(elem, n))
-
else:
-
results.append(elem * n)
-
return results
-
-
seq = [[1, 2], [3, 4]]
-
seq1 = scalar_mult(seq, 3)
-
print seq
-
print seq1
-
-
seq2 = ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
-
seq3 = scalar_mult(seq2, 3)
-
print seq2
-
print seq3
-
-
''' The output:
-
>>> [[1, 2], [3, 4]]
-
[[3, 6], [9, 12]]
-
((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
-
[[3, 6], [9, [12, 15, 18], [[18, 21, 24], [27, 60]]]]
-
'''
Sign in to post your reply or Sign up for a free account.
Similar topics
by: santosh |
last post by:
Hello,
I was going through the Marshal Cline's C++ FAQ-Lite.
I have a doubt regarding section 33.10.
Here he is declaring a pure virtual destructor in the base class.
And again defining...
|
by: WittyGuy |
last post by:
Hi,
I wonder the necessity of constructor and destructor in a Abstract
Class? Is it really needed?
?
Wg
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: Kevin Bracey |
last post by:
I've been implemening Annex F in my compiler, and one issue to do with "pure"
functions has come to my attention.
The compiler has for a long time had a __pure keyword (and a related pragma...
|
by: junky_fellow |
last post by:
I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.
I got one example that expalins how the code can be optimised....
|
by: Michael C |
last post by:
I'm trying to convert this interface over to C# but can't get any of the
functions to work at all. I'm just trying as a start to get the get_Name
function to work but can't get anything out of it...
|
by: Edward Diener |
last post by:
I received no answers about this the first time I posted, so I will try
again. My inability to decipher an MSDN topic may find others who have the
same inability and someone who can decipher and...
|
by: sks |
last post by:
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
|
by: John Goche |
last post by:
Hello,
page 202 of Symbian OS Explained by Jo Stichbury states
"All virtual functions, public, protected or private, should be
exported"
then page 203 states
"In the rare cases where a...
|
by: Miguel Guedes |
last post by:
Hello,
I recently read an interview with Bjarne Stroustrup in which he says
that pure abstract classes should *not* contain any data. However, I
have found that at times situations are when it...
|
by: Rahul |
last post by:
Hi,
I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...
I'm trying to think...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |