# geometric progression in a list

Page 1 of 1

## 10 Replies - 765 Views - Last Post: 01 November 2017 - 06:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407298&amp;s=51c0e19639b8ae82b25717e12aa42314&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Torroes.Prime

Reputation: 3
• Posts: 32
• 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

• blow up my boots

Reputation: 6541
• Posts: 26,475
• 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.

### #3 Torroes.Prime

Reputation: 3
• Posts: 32
• Joined: 20-March 15

## Re: geometric progression in a list

Posted 31 October 2017 - 09:39 AM

andrewsw, on 31 October 2017 - 09:35 AM, said:

Where is the value of n assigned?

n is supplied by MyProgrammingLab.

### #4 DK3250

• Pythonian

Reputation: 395
• Posts: 1,268
• 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...

### #5 Torroes.Prime

Reputation: 3
• Posts: 32
• 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.

### #6 DK3250

• Pythonian

Reputation: 395
• Posts: 1,268
• 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

### #7 Torroes.Prime

Reputation: 3
• Posts: 32
• 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.

DK3250, 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.

### #8 DK3250

• Pythonian

Reputation: 395
• Posts: 1,268
• 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.

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.

### #9 Torroes.Prime

Reputation: 3
• Posts: 32
• Joined: 20-March 15

## Re: geometric progression in a list

Posted 01 November 2017 - 03:25 PM

DK3250, on 31 October 2017 - 11:23 PM, said:

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

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

DK3250, 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 *=.

### #10 Torroes.Prime

Reputation: 3
• Posts: 32
• 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.

### #11 modi123_1

• Suitor #2

Reputation: 13954
• Posts: 55,700
• Joined: 12-June 08

## Re: geometric progression in a list

Posted 01 November 2017 - 06:02 PM

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

DK3250, on 31 October 2017 - 11:23 PM, said:

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

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).