Jump to content

Anyone can help me with python? (I think i answered it, but don't know exactly)


Zanarkin
 Share

Recommended Posts

Here is the question:

(a) Use a for loop to create a list with ten elements in which the first is 1 and the last is 17 and the remaining elements are evenly spaced (ie so that the differences between adjacent elements are equal). Convert your result to an array using Numpy's array() function and print its contents. (b) Use Numpy's linspace() function to create an array with elements that correspond to your answer to (a). Print its contents. © Use a loop to print out the difference of the corresponding elements of your two arrays. (d) Repeat part © without using a loop.

UPDATE Please read the page on looping in Python. Make sure that you don't use floating point numbers for your loop index (or you will lose marks).

#Part a)

#Import stuff
from numpy import arange, array, linspace

#Set variable for arange
a = arange ( 1, 18, 16.0/9 )

#Convert to array
print array ( a )

#Part b)

b = array ( a )

#Set array to linspace
c = linspace ( 1, 17, num=10 )

#convert to linspace
print linspace ( 1, 17, num=10 )

#Part c)
#Find difference of arrays with loops

#Set loops
j = c 
for i in b:
   sub = i-j
   print sub

My question is, if i actually answered the question or not. It seems highly redundant all of what i did ...? Also the question says not to use floating numbers. As far as i have understood is that floats = numbers like 1.0, 2.0 etc. Well I can't use those, but in python if i don't use 16.0/9 then i can't have 10 elements that start at 1 and ends in 17. The things in # are just notes for the TA's. Otherwise we get 1 mark off. Also, part d i left it off for later because i'm tired of that question.

Edited by SlayerX
Link to comment
Share on other sites

how is this possible

1 3 5 7 9 11 13 15 17

Edit: I see you've discovered the issue yourself.

Welcome to modern academia. You pay fuckloads of money for homework that isn't even presented in an accurate and fair way (I immediately saw the problem; there's no way anyone proofread this), are taught memorization more than the actual subject and make grades based on whether you bother to do the tedious bullshit instead of how much you know and can do. Especially if you're like me and devote that time that would be spent on homework I dunno actually learning and then score over 100% on the midterm and final but are still given a B for the course (what).

Edited by Solais
Link to comment
Share on other sites

I managed to make it evenly spaced within programming. All you need is to make increments of 16/9. Also, I dn't need to memorize much in this class as anything in the assignments is allowed to be looked at while in the midterms/finals

Link to comment
Share on other sites

Wait what? Paste the output array. I don't feel like working out in my head how you got 10 evenly spaced integers from 1 through 17 in a 10 element array.

Edit: I read the actual question. It says not to use floats for your loop index, not the actual array values.

Your comments below the code block in your first post didn't make that clear at all.

Edited by Solais
Link to comment
Share on other sites

Raivix is right, but the fact that you did the thing in one line that the professor wanted you to do with an unnecessary loop should be left in with a comment indicating that you're smarter than that and should be given special treatment.

Kick some dirt in their face and spit on them for giving you such demeaning assignments that undermine and disrespect you. How dare they?!

Link to comment
Share on other sites

While the sarcastic advice is great and all, this IS a first-year level assignment. and TAs don't give a shit if you do something that would be considered real-world correct when the assignment is asking for something else. It's also important to distinguish that different functions that give similar results can come to those results in much different ways, and especially when dealing with floating point numbers, the differences can be astonishingly large. There's a reason the prof is asking what he's asking.

I personally don't know python, and I especially don't know anything about this particular library that he's using, but as far as schoolwork goes, most stuff has a reason for being done a certain way.

Link to comment
Share on other sites

and they're usually pretty stupid

I wasn't being sarcastic. If it's being graded by a TA then he should shove it into the professor's face on his own. He shouldn't pass up an opportunity to get on the prof's good side by showing proactivity.

Link to comment
Share on other sites

how is this possible

1 3 5 7 9 11 13 15 17

lol

Raivix is right, but the fact that you did the thing in one line that the professor wanted you to do with an unnecessary loop should be left in with a comment indicating that you're smarter than that and should be given special treatment.

Kick some dirt in their face and spit on them for giving you such demeaning assignments that undermine and disrespect you. How dare they?!

KiBQh.gif

I think the loop is written incorrectly. Each difference in the corresponding elements in the two arrays should be zero since the arrays are identical. As for the floating-point thing, I think that only comes into play (for the purposes of this assignment) if you're using a while loop.

Edited by Nessie
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...