11. Count


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/count?action=lesson_resume

Im not very good at coding so i ask a lot of questions to see where i messed up at. What do i put in the code to make it execute what it needs to? It’s saying that it is not returning the correct total.


def count(sequence, item):
  total = 0
  for item in sequence:
    if sequence == item:
    	total += total + item
  return total


#2

You are over-increasing the total.

Notice there are two plus signs and an equal sign. That is all the operations that are going to take place with that statement.

>>> a = 0
>>> a += a + 5
>>> a
5
>>> a += a + 5
>>> a
15
>>> a += a + 5
>>> a
35
>>> a += a + 5
>>> a
75
>>> 

The key to this is recognizing the expression, a + 5. What ever value a holds will be incremented by 5. Now we take that value and return it to a += (expression value) and you see where this is going.

The purpose of the compound operator, += is to help us avoid this exponential growth, and concentrate upon the linear step value.

a += 5

Now we have what can be trusted to increment a in step values of 5.


Aside

We can build a term generator factory function for any step value and term n (sort of; need to iron out some details)…

>>> def a_plus_d_seq(d):
    def seq(n):
        a = list(range(n + 1))
        for i in range(n):
            a[i + 1] = a[i] + d
        return a[n]
    return seq

>>> fives = a_plus_d_seq(5)
>>> fives(0)
0
>>> fives(1)
5
>>> fives(2)
10
>>> fives(3)
15
>>> fives(4)
20
>>> 

Ironed out. Term n will indeed be correct where n > 0 and term 1 is 0.

>>> def a_plus_d_seq(d):
    def seq(n):
        a = list(range(n + 1))
        for i in range(n):
            a[i + 1] = a[i] + d
        return a[n - 1]
    return seq

>>> fives = a_plus_d_seq(5)
>>> fives(4)
15
>>> fives(1)
0
>>>

Taking it a bit further, let’s return the term, and the sequence up to that term…

>>> def a_plus_d_seq(d):
    def seq(n):
        a = list(range(n + 1))
        for i in range(n):
            a[i + 1] = a[i] + d
        return a[n - 1], a[:-1]
    return seq

>>> fives = a_plus_d_seq(5)
>>> fives(4)
(15, [0, 5, 10, 15])
>>> sixes = a_plus_d_seq(6)
>>> sixes(6)
(30, [0, 6, 12, 18, 24, 30])
>>> 

If this appears to be over one’s head, don’t let it get to you. Right now you are learning the basic mechanics this is built from. Focus on that and when you finsh the track come back. It will make much more sense by then.


And with all the ironing out of the serquence generator we finally come up to the point where we can press it into service to generate the sequence the OP code sample produces…

>>> def foo(d):
    def seq(n):
        a = list(range(n + 1))
        for i in range(n):
            a[i + 1] = 2 * a[i] + d
        return a[n - 1], a[:-1]
    return seq

>>> fives = foo(5)
>>> fives(1)
(0, [0])
>>> fives(2)
(5, [0, 5])
>>> fives(3)
(15, [0, 5, 15])
>>> fives(4)
(35, [0, 5, 15, 35])
>>> fives(5)
(75, [0, 5, 15, 35, 75])
>>> 

Gotta love math and programming together. Just astounds me the stuff we can come up with.


On the maths front, this falls under the concept of series and sequences which in Canada is explored very loosely in grade school, and comes up more seriously around grade 10 or 11 matric algebra. General math covers the topic but then abandons it. The academic route would be the way to go if maths are of any interest.


#3

We can intrroduce another variable to repace the literal 2 in the earlier code which also means adding a parameter…

>>> def foo(d):
    def seq(n=1, x=1):
        t = list(range(n + 1))
        for i in range(n):
            t[i + 1] = x * t[i] + d
        return t[n - 1], t[:-1]
    return seqs

>>> fives = foo(5)
>>> fives(1,1)
(0, [0])
>>> fives(2,1)
(5, [0, 5])
>>> fives(2,2)
(5, [0, 5])
>>> fives(3,2)
(15, [0, 5, 15])
>>> fives(5,2)
(75, [0, 5, 15, 35, 75])
>>> 

Now the literal is replaced with a parameter. This is becoming a free form function that is portable. Something that we might even delight in.


Now ask yourself if long variable names would make this any easier to understand.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.