python help
Watch this threadPage 1 of 1
Skip to page:
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Hey, I really need help with this question:
We can store a complex number in python as a list of two floats, [x,y]. Write a function which takes two complex numbers w and z as lists of length two, and returns a list of length two which represents the product w x z.
We can store a complex number in python as a list of two floats, [x,y]. Write a function which takes two complex numbers w and z as lists of length two, and returns a list of length two which represents the product w x z.
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report
#2
Let w = x + iy, z = v + iu
We can represent these complex numbers in lists like this (like the question wants us to).
w = [x,y] z = [v,u]
We can multiply w and z using standard multiplication rules.
w*z = (x + iy)(v + iu) = xv + i(xu) + i(yv) - yu = (xv - yu) + i(xu + yv)
This can be represented in a list like this.
w*z = [xv - yu, xu + yv]
So, all you have to do is write a function that takes in two lists (representing complex number w and z) and return the list that represents w*z (the one above)
We can represent these complex numbers in lists like this (like the question wants us to).
w = [x,y] z = [v,u]
We can multiply w and z using standard multiplication rules.
w*z = (x + iy)(v + iu) = xv + i(xu) + i(yv) - yu = (xv - yu) + i(xu + yv)
This can be represented in a list like this.
w*z = [xv - yu, xu + yv]
So, all you have to do is write a function that takes in two lists (representing complex number w and z) and return the list that represents w*z (the one above)
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
(Original post by MathsIsOkayish)
Let w = x + iy, z = v + iu
We can represent these complex numbers in lists like this (like the question wants us to).
w = [x,y] z = [v,u]
We can multiply w and z using standard multiplication rules.
w*z = (x + iy)(v + iu) = xv + i(xu) + i(yv) - yu = (xv - yu) + i(xu + yv)
This can be represented in a list like this.
w*z = [xv - yu, xu + yv]
So, all you have to do is write a function that takes in two lists (representing complex number w and z) and return the list that represents w*z (the one above)
Let w = x + iy, z = v + iu
We can represent these complex numbers in lists like this (like the question wants us to).
w = [x,y] z = [v,u]
We can multiply w and z using standard multiplication rules.
w*z = (x + iy)(v + iu) = xv + i(xu) + i(yv) - yu = (xv - yu) + i(xu + yv)
This can be represented in a list like this.
w*z = [xv - yu, xu + yv]
So, all you have to do is write a function that takes in two lists (representing complex number w and z) and return the list that represents w*z (the one above)
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
I've tried a few different things and none of them have been working so I'm not sure what I'm doing is really getting me anywhere
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#7
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#8
I understand why it's not working I'm just not sure what to do instead
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#9
Report
#9
Uhh, im not too sure what you are doing.
Do you know that you can access a specific element in a list?
For example, if you have w = [1.0, 2.0], then you can do w[0] to refer to the first element in w? (In this case it would return 1.0).
By doing this, you can select the correct elements to multiply (remember how you work out w*z) and return the final list.
EDIT: Now I think I see what is going on; you have misunderstood how 'for' works in Python.
Do you know that you can access a specific element in a list?
For example, if you have w = [1.0, 2.0], then you can do w[0] to refer to the first element in w? (In this case it would return 1.0).
By doing this, you can select the correct elements to multiply (remember how you work out w*z) and return the final list.
EDIT: Now I think I see what is going on; you have misunderstood how 'for' works in Python.
Last edited by MathsIsOkayish; 3 years ago
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#10
how are you able to select the first and second elements in the list?
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#11
Report
#11
Say we have a list
A = [1.4,99,24,'abc',5]
In Python, lists start counting from position 0.
This means that to access the first element of the list A, you must enter A[0] (returns 1.4).
If you want the second element in the list, enter A[1] (returns 99)
if you want the third element in the list, enter A[2] (returns 24)
if you want the Nth element in the list, enter A[n-1]
Example of this in use: print(A[3])
This will display the fourth element in the list A in the console
A = [1.4,99,24,'abc',5]
In Python, lists start counting from position 0.
This means that to access the first element of the list A, you must enter A[0] (returns 1.4).
If you want the second element in the list, enter A[1] (returns 99)
if you want the third element in the list, enter A[2] (returns 24)
if you want the Nth element in the list, enter A[n-1]
Example of this in use: print(A[3])
This will display the fourth element in the list A in the console
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#12
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#13
would you also be able to help me with this question? I can't figure out how to make it return 'true' for 0*n
0
reply
vilefor
Badges:
10
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#14
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#15
Report
#15
There are a few problems with that code.
(There is no need for a while loop, in fact it actually makes it impossible to return True)
(i%n cannot equal n because it would revert back to 0, making it irrelevant to check for this).
So how do we do this?
All you need to do is loop through the list (using a for loop)
And for every element in the list, check to see if the element is a multiple of n. Aka check if i%n = 0 - if so, return True as this implies that there is an element in the list that divides n, otherwise continue looping.
This means that if we make it out of the loop without returning True, then that implies that no element in the list was a multiple of n, and thus we should return False.
(There is no need for a while loop, in fact it actually makes it impossible to return True)
(i%n cannot equal n because it would revert back to 0, making it irrelevant to check for this).
So how do we do this?
All you need to do is loop through the list (using a for loop)
And for every element in the list, check to see if the element is a multiple of n. Aka check if i%n = 0 - if so, return True as this implies that there is an element in the list that divides n, otherwise continue looping.
This means that if we make it out of the loop without returning True, then that implies that no element in the list was a multiple of n, and thus we should return False.
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#16
I have changed the code to this but in the case of ([-27,8,5,14,-13,17],3)) it's producing false when it should be true
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#17
sorry that was supposed to say in the case of the list (-27,8,5,14,-13,17) with divisor 3 but it deleted the list in the square brackets
0
reply
MathsIsOkayish
Badges:
1
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#18
Report
#18
The problem here is with your logic.
We want to return True if there exists a single element in the list that is a multiple of n.
What you are doing is working out if the last element of the list is a multiple of n (because you are switching the multiple variable for each element, depending if it is a multiple of n)
In the case of passing in (-27,8,5,14,-13,17) with divisor 3, it is returning False because the final element,17, is not a multiple of 3.
We don't care if there are 100 elements in the list that are not multiples of n; we only care if at least one element in a multiple of n.
For this reason, we just need to return True if there exists a single multiple of n, thus we can remove the else section of your code.
We still need the 'return False' section in the function, but it should be outside of the for loop.
We want to return True if there exists a single element in the list that is a multiple of n.
What you are doing is working out if the last element of the list is a multiple of n (because you are switching the multiple variable for each element, depending if it is a multiple of n)
In the case of passing in (-27,8,5,14,-13,17) with divisor 3, it is returning False because the final element,17, is not a multiple of 3.
We don't care if there are 100 elements in the list that are not multiples of n; we only care if at least one element in a multiple of n.
For this reason, we just need to return True if there exists a single multiple of n, thus we can remove the else section of your code.
We still need the 'return False' section in the function, but it should be outside of the for loop.
Last edited by MathsIsOkayish; 3 years ago
0
reply
Bermuda
Badges:
6
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#19
I understand what you're saying but I'm still not sure where to place the 'return False' in order for it to not always just return false
0
reply
X
Page 1 of 1
Skip to page:
Quick Reply
Back
to top
to top