10 Replies - 402 Views - Last Post: 01 November 2017 - 06:02 PM Rate Topic: -----

#1 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

geometric progression in a list

Posted 31 October 2017 - 08:57 AM

Okay, so I'm working on an assignment for class. The assignment is as follows:

Quote

Given the positive integer ratio greater than 1, and the non-negative integer n, create a list consisting of the geometric progression of numbers between (and including) 1 and n with a common ratio of ratio. For example, if ratio is 2 and n is 8, the list would be [1, 2, 4, 8].

Associate the list with the variable geom_prog.


Okay, seems pretty straight forward. So I came up with:

geom_prog = []
i = 0
while i <= n+1:
	entry = geom_prog[i]*ratio
	geom_prog.append(entry)


So we have i set to zero as a counter for the index of the list. Then take that entry, multiply it by the ratio (supplied by MyProgrammingLab for testing hence why it's not declared in my code), and assign that total to entry, then write entry to the next index in geom_prog. rinse and repeat right?

Well according to MPL all I'm getting are null values.

So... what am I over looking here?

Is This A Good Question/Topic? 0
  • +

Replies To: geometric progression in a list

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,766
  • Joined: 12-December 12

Re: geometric progression in a list

Posted 31 October 2017 - 09:35 AM

Where is the value of n assigned?
You cannot index an element of geom_prog if it is just an empty list.
Was This Post Helpful? 0
  • +
  • -

#3 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: geometric progression in a list

Posted 31 October 2017 - 09:39 AM

View Postandrewsw, on 31 October 2017 - 09:35 AM, said:

Where is the value of n assigned?


n is supplied by MyProgrammingLab.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,053
  • Joined: 27-December 13

Re: geometric progression in a list

Posted 31 October 2017 - 10:14 AM

You do not use the info that first element is 1 (one).
Try to implement it...

... and you while loop is wrong.
In the example n=8, thus you'll get a list of 8 values.
The correct answer only has 4 values...
Was This Post Helpful? 1
  • +
  • -

#5 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: geometric progression in a list

Posted 31 October 2017 - 10:30 AM

So I set the geom_prog list to have an initial value at the first index


geom_prog = [1]
i = 1
while i <= n:
	entry = geom_prog[i]*ratio
	geom_prog.append(entry)



Now MyProgrammingLab is providing the following 'helpful' hints:
⇒ We think you might want to consider using: *=
⇒ Solutions with your approach don't usually use: *


So I'm not inclined to trust MPL's 'hints'. It's given worthless feedback before. So I'm kind of shooting blind.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,053
  • Joined: 27-December 13

Re: geometric progression in a list

Posted 31 October 2017 - 11:19 AM

Your while loop is still corrupt.
A ) It will produce a list of length 'n', not a list ending at value 'n' (provided problem B is solved)
B ) the value of 'i' is never updated >> the loop never ends.

Do you know how to point to the last element in a list? This may be useful here...

I think the MPL is trying to bring your focus to the difference of *= and *, but put this aside (for now) and focus on your code.

This post has been edited by DK3250: 31 October 2017 - 11:28 AM

Was This Post Helpful? 1
  • +
  • -

#7 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: geometric progression in a list

Posted 31 October 2017 - 05:04 PM

Okay, so as DK3250 pointed out, the 'i' was never increasing so it was just running an infinite loop.

So upon seeing that little oversight I revised my code to:

geom_prog = []
i = 1
while i <= n:
	geom_prog.append(i)
	i *= ratio



and now MPL is saying:

⇒ Solutions with your approach don't usually use: *=

So... I don't know what it's telling me to do at this point. increment the i by a different means perhaps? could I do something like i = i + 1? That seems needlessly clunky assuming it would even work.

View PostDK3250, on 31 October 2017 - 11:19 AM, said:

Do you know how to point to the last element in a list? This may be useful here...


I know A way that I have been told points to the last element in a list, just call the list and index it with a -1 ( list[-1] ). I've been told that points to the last value before the end of the list. I have little faith in the accuracy of that though.
Was This Post Helpful? 1
  • +
  • -

#8 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,053
  • Joined: 27-December 13

Re: geometric progression in a list

Posted 31 October 2017 - 11:23 PM

I think you have now a very nice and efficient code. Congratulations. :bigsmile:

Just disregard my question of last list element, it was intended on an intermediate solution.

You can also disregard the MPL comment; however, 'i' is normally used as name for counter. I (like the MPL) would prefer another name for 'i' in your code, e.g. 'entry' as used before.
Was This Post Helpful? 0
  • +
  • -

#9 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: geometric progression in a list

Posted 01 November 2017 - 03:25 PM

View PostDK3250, on 31 October 2017 - 11:23 PM, said:

I think you have now a very nice and efficient code. Congratulations. :bigsmile:/>

that's nice. It's still being marked as 'wrong'.

View PostDK3250, on 31 October 2017 - 11:23 PM, said:

Just disregard my question of last list element, it was intended on an intermediate solution.

You can also disregard the MPL comment; however, 'i' is normally used as name for counter. I (like the MPL) would prefer another name for 'i' in your code, e.g. 'entry' as used before.


I would love to disregard it. But as it stands it's still being counted as wrong by MPL. So... yeah. I can swap the 'i' for anything. But according to MPL using

geom_prog = []
i = 1
while i <= n:
    geom_prog.append(i)
    i *= ratio



is not the proper solution as I shouldn't be using the *=.
Was This Post Helpful? 0
  • +
  • -

#10 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: geometric progression in a list

Posted 01 November 2017 - 04:36 PM

Forget it. I can't afford to spend anymore time on this. I've had 3 different sources all tell me the code is good and should be working. MPL can go suck it.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13487
  • View blog
  • Posts: 53,857
  • Joined: 12-June 08

Re: geometric progression in a list

Posted 01 November 2017 - 06:02 PM

View PostTorroes.Prime, on 01 November 2017 - 05:25 PM, said:

View PostDK3250, on 31 October 2017 - 11:23 PM, said:

I think you have now a very nice and efficient code. Congratulations. :bigsmile:/>/>/>

that's nice. It's still being marked as 'wrong'.

FYI you may want to lay off the rep system until you get a better handle on it. It is not just a right/wrong operation.

Check FAQ (at the bottom of every page).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1