Programming Questions

Array following an arithmetic or geometric sequence

Description - For this challenge you will determine if numbers within an array follow an arithmetic or geometric sequence.
Hello Coderbyte!
I'm working my way through all the challenges in coderbyte and the time for a question has arised, so i'd pick one for the mathematically non-challenged out there, but i have some requirements for the answer that you provide.
My current problem is basically the lack of math skills. and i have a challenging learning disability alongside this. i can learn just about anything, but if i don't grasp the basics and the framwork around a solution i really don't understand jack shit.
I know the structure and understand the difference between the two arrays, and i know in my head how i can check for the different types of sequences, but i REALLY would like for once to understand the actual thought pattern that lies behind the mathematical equation used to solve the two different sequences.
2 + 5 + 8 + 11 + 14
i know that this line has an arithmetic progression, but how the heck do you check for it through a code by applying the "equation" that goes along with it?
Looking at the actual equation behind this when googling gives me the urge of headbanging a pencil.
I would like to understand how to solve the equation outside of coding and then be able to apply this through code.
Please stay away from mathematical terms, i love the thought of apples and cake! xD

Juksefantomet
posted this question on 5/20/14 **|**

5

You might be over-thinking it. To check for arithmetic using the numbers you gave you would see if 5 - 2 equals 14 - 11. So, 2nd minus 1st and last minus 2nd to last. If you were checking for geometric you would just switch to division. Does 5 / 2 equal 14 / 11? Apples and cake for all!

mattlarsh

answered on 05/20/14

1

Hi,
I like to think of this problem as looking for the hidden multiplier or adder/subtracter between each element. To use your example 2 + 5 + 8 + 11 + 14, in order to get from 2 to 5 takes the addition of 3, or the multiplication of something (nasty). So every other 'space' or hop should give us the same 'added 3' or 'multilplied nasty'.
We know that the space between 5 and 8 also takes and 'add 3', as do the rest, but we should check each one (that will be a loop). As we do, we should probably scratch a tally mark in a bedpost for each similar adder-- in this case there will be 4 scratchmarks for each of the similar 'adders' for this arithmetic progression, which is exactly 1 less than the number of items in the list.
Then, do the same for the geo 'nasty' and see if each on is similar. Again, keep a tally. In this case, it yields just the first instance, the 'nasty' number by itself would count as one, but that's it. [I just realized the 'nasty' number is only 2.5, but you get the idea, it's not clean]
Now, if either of the tallies is one less than the amount of elements in the set, you have that type of value.
But, hang on a moment, you might say! How do you determine how you check the adder or multiplier? I think I started by subtracting the (nth + 1) item from the nth item (starting with index 0, using a range, this would be the 1st minus the 2nd items, or 2 minus 5), and, even though it was a negative number, I still assigned that number to my variable to check it as I continued my iteration. Does that make sense?
For the geo series, I think I divided the nth + 1 item by the nth item to set my initial variable against which others where checked.
Hope that helps, in a bit of a cake and apples kind of way. :-)

HeronCode

answered on 05/24/14

0

EDIT: Paragraph two, line one should read:
"We know that the space between 5 and 8 also takes *an* 'add 3'..."

HeronCode

answered on 05/24/14

0

Thanks everyone for your replies :D
So let's say on my example, if i split the input into a list, by adding each element that is not a symbol, i can then iterate through that list and on every other occurance check with a subraction formula, add that number to my "equality counter", IF the next occurances also is equal to that counter result i will have an arithmetic sequence?
(of course if it is not equal i understand the pattern is not equal all the way, hence it is not a sequence hehe)
same goes for the geometric only with division checks :D
am i correct in this statement?
if so, the apples and cakes where eaten by a hungry dude with a better understanding for mathematics. Thanks, this really helps alot.

Juksefantomet

answered on 06/13/14

0

This works inside my PyCharm edit, not when applying to the website it is telling I am
def ArithGeo(arr):
tmp = arr.split(',')
if int(tmp[1]) - int(tmp[0]) == int(tmp[-1]) - int(tmp[-2]):
return "Arithmetic"
elif int(tmp[1]) / int(tmp[0]) == int(tmp[-1]) / int(tmp[-2]):
return "Geometric"
else:
return "-1"
if __name__ == '__main__':
print ArithGeo(raw_input())

niceseb

answered on 07/16/15

0

Good morning guys , I was wondering if somebody could help me wih this code.
Im not sure on how to express the the expression inside the if/ else if.
Thank you in advance guys

```
function ArithGeo(arr) {
// code goes here
for (var i=0;i<arr.length;i++)
for (var n=1;n<100000;n++)
if((arr[i] += n) || (arr[i] -= n))
{return "Arithmetic"}
else if(arr[i] *= n)
{return "Geometric"}
else
{return "-1"}
}
// keep this function call here
ArithGeo(readline());
```

ge00rge

answered on 03/23/17

Log in to write an answer.