# python help

#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.
0
3 years ago
#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)
0
#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)
I understand the question I'm just not sure how to actually write the code for it.
0
3 years ago
#4
How much of the code can you do?
0
#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
3 years ago
#6
Could you post an example of what you have tried?
0
#7
0
#8
I understand why it's not working I'm just not sure what to do instead
0
3 years ago
#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.
Last edited by MathsIsOkayish; 3 years ago
0
#10
how are you able to select the first and second elements in the list?
0
3 years ago
#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
0
#12
think I got it now, thank you!
0
#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
3 years ago
#14
0
3 years ago
#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.
0
#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
#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
3 years ago
#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.
Last edited by MathsIsOkayish; 3 years ago
0
#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
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Year 12s - where are you at with making decisions about university?

I’ve chosen my course and my university (18)
31.03%
I’ve chosen my course and shortlisted some universities (22)
37.93%
I’ve chosen my course, but not any universities (2)
3.45%
I’ve chosen my university, but not my course (3)
5.17%
I’ve shortlisted some universities, but not my course (4)
6.9%
I’m starting to consider my university options (7)
12.07%
I haven’t started thinking about university yet (1)
1.72%
I’m not planning on going to university (1)
1.72%