Python Forum
[Learning] 3 hours for one simple exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Learning] 3 hours for one simple exercise
#1
Sad 
Hi there, I'm currently working as an Analysts of a System of a bank, so I don't program there but I know a little bit (barely to get in).
I started working there with the hope to work as a developer but my hopes died out when they put me in charge of a whole dead project (which I revived btw). I been there for one year and started again the collage being 27 years old (yeah it sucks).
#rantover#
So...
I don't program a lot, not at all you can say.
It took me literally 3 hours to get this exercise working in a rather awful way.
You have any ideas or pointer that you can give me to make it work smoother and don't repeat too much code?

RESTRICTIONS:
ONLY using: if, elif, else, and, or, not, while, break, continue, for, in, range(), lambda, modules and packages.

No lists or anything similar.

Exercise:
For an integer N less than 100 received as a parameter, write a program
that uses a function to return the sum of the squares of those
numbers between 1 and N that are separated from each other by four units. (12 + 52 +
92 + 132 + ...)


PS: SORRY! The code is in spanish but you can understand the meaning no?

# Práctica 1, Ejercicio 3. Autor: KoFu

def calcularcuadrado(n):
    
    """ Devuelve la suma de los cuadrados del numero ingresado (entre 1 y 100) de aquellos
números entre 1 y N que están separados entre si por cuatro unidades"""
    n1,n2,n3,n4,n5=0,0,0,0,0
    c1,c2,c3,c4,c5=0,0,0,0,0
    acumulador=0

    
    if (1 <= n <= 100):
        while (n > 0):
            cuadrado = n**2
            if (n1 == 0):
                n1 = n
                c1 = cuadrado
            elif (n2 == 0):
                n2 = n
                c2 = cuadrado
            elif (n3 == 0):
                n3 = n
                c3 = cuadrado
            elif (n4 == 0):
                n4 = n
                c4 = cuadrado
            elif (n5 == 0):
                n5 = n
                c5 = cuadrado
            
            if (n5 - n1 == -4):
                sumaparcial = c1+c5
                acumulador = sumaparcial + acumulador
                n1,n2,n3,n4=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
            elif (n5 - n == 4):
                sumaparcial = cuadrado
                acumulador = sumaparcial + acumulador
                n1,n2,n3,n4=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
                
            elif (n4 - n1 == -4):
                sumaparcial = c1+c4
                acumulador = sumaparcial + acumulador
                n1,n2,n3,n5=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
            elif (n4 - n == 4):
                sumaparcial = cuadrado
                acumulador = sumaparcial + acumulador
                n1,n2,n3,n5=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
                
            elif (n3 - n1 == -4):
                sumaparcial = c1+c4
                acumulador = sumaparcial + acumulador
                n1,n2,n5,n4=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
            elif (n3 - n == 4):
                sumaparcial = cuadrado
                acumulador = sumaparcial + acumulador
                n1,n2,n5,n4=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
                
            elif (n2 - n1 == -4):
                sumaparcial = c1+c4
                acumulador = sumaparcial + acumulador
                n1,n4,n3,n4=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0
            elif (n2 - n == 4):
                sumaparcial = cuadrado
                acumulador = sumaparcial + acumulador
                n1,n4,n3,n5=0,0,0,0
                c1,c2,c3,c4,c5=0,0,0,0,0              

            sumatotal = acumulador
            n = n - 1
            
    else:
        sumatotal = -1
    return sumatotal;



# Programa principal
num=int(input("Ingrese un numero (entre 1 y 100) para calcular la suma de los cuadradros separados entre cuatro digitos: "))

suma = calcularcuadrado (num)

if (suma==-1):
    print("El numero ingresado no esta en el rango permitido")
else:
    print("La suma de los cuadrados es de: ", suma)
Reply
#2
I don't understand why you need all those variables. Is it not sufficient to have a loop variable that goes up in steps of 4 between the correct bounds (see range()) and a variable to keep track of the sum?

Also, your example contains the numbers 12, 52, 92 and 132, none of which are square.
Reply
#3
Please explain explicitly how these number are calculated: (12 + 52 + 92 + 132 + ...)
Please explain what is meant with this statement: "return the sum of the squares of those numbers between 1 and N that are separated from each other by four units."
What is meant with "4 units"?
Where did you get this exercise? Do you know the solutions for numbers n == 1,2,3,4,5,6 etc?
----------------------------------
Let´s look at the number 1 to 10 and their squares:
1 1, 2 4, 3 9, 4 16, 5 25, 6 36, 7 49, 8 64, 9 81, 10 100
If n == 1: the sum of the square of 1 is 1.
if n == 2: the sum is 1 + 5 = 6
if n == 3: the sum is 1+5+9 = 15
if n == 4: the sum is 1+5+9+16 = 31
if n == 5: the sum is 1+5+9+16+25 = 56
n == 6: 56+36 = 92
n == 7: 92+49 = 141
n == 8: 141+64 = 205
n == 9: 205+81 = 286
n == 10: 286+100 = 386

I don´t find any of those sums in your numbers, so i don´t understand your exercise or the numbers you provided have nothing to do with that exercise.
Reply
#4
There are some ambiguity (what does mean separated by four) but sum of every forth integer in range n should be simply:

>>> n = 100
>>> sum(i ** 2 for i in range(1, n+1, 4))
80825
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Aug-25-2019, 04:48 AM)ndc85430 Wrote: I don't understand why you need all those variables. Is it not sufficient to have a loop variable that goes up in steps of 4 between the correct bounds (see range()) and a variable to keep track of the sum?

Also, your example contains the numbers 12, 52, 92 and 132, none of which are square.

Sorry google translate automatically resolved the exponentials of the numbers. Those numbers are exponentials... 1**2, 5**2, 9**2,13**2, etc....

Yeah, I want to make a loop that gos in steps of 4 but IDK how.


(Aug-25-2019, 07:41 AM)ThomasL Wrote: Please explain explicitly how these number are calculated: (12 + 52 + 92 + 132 + ...)
Please explain what is meant with this statement: "return the sum of the squares of those numbers between 1 and N that are separated from each other by four units."
What is meant with "4 units"?
Where did you get this exercise? Do you know the solutions for numbers n == 1,2,3,4,5,6 etc?
----------------------------------
Let´s look at the number 1 to 10 and their squares:
1 1, 2 4, 3 9, 4 16, 5 25, 6 36, 7 49, 8 64, 9 81, 10 100
If n == 1: the sum of the square of 1 is 1.
if n == 2: the sum is 1 + 5 = 6
if n == 3: the sum is 1+5+9 = 15
if n == 4: the sum is 1+5+9+16 = 31
if n == 5: the sum is 1+5+9+16+25 = 56
n == 6: 56+36 = 92
n == 7: 92+49 = 141
n == 8: 141+64 = 205
n == 9: 205+81 = 286
n == 10: 286+100 = 386

I don´t find any of those sums in your numbers, so I don´t understand your exercise or the numbers you provided have nothing to do with that exercise.

The exercise is simple if the user inputs the number 10. Is going to search for the number 4 units away from the original and then save the exponent, and keep repeating that until the input number reaches 0.
Ex: 10, 6, 2.
another ex: 5,1
yet another ex: 12,8,4


(Aug-25-2019, 10:42 AM)perfringo Wrote: There are some ambiguity (what does mean separated by four) but sum of every forth integer in range n should be simply:

>>> n = 100
>>> sum(i ** 2 for i in range(1, n+1, 4))
80825

Yeah! that's what I'm looking for! I'm gonna try that solution! Thanks!
Reply
#6
(Aug-25-2019, 10:42 AM)perfringo Wrote: There are some ambiguity (what does mean separated by four) but sum of every forth integer in range n should be simply:

>>> n = 100
>>> sum(i ** 2 for i in range(1, n+1, 4))
80825

Can you help me fix the code implementing range as you told me?
If is not much of a problem...
Thanks!
Reply
#7
Sum of which numbers squares is needed?

>>> list(range(1, 20, 4))
[1, 5, 9, 13, 17]
>>> list(range(1, 20, 5))
[1, 6, 11, 16]
You can use built-in help to understand how range works:

>>> help(range)

Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object
 |  
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 |  These are exactly the valid indices for a list of 4 elements.
 |  When step is given, it specifies the increment (or decrement).
 |  
/.../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
This is the entire code resolved. I will leave the answer here. Sorry for being late!

def calc(n):
    i = 1
    total = 0
    if (1 <= n <= 100):
        for i in range(1, n+1, 4):
            aux = i**2
            total = aux + total
    else:
        total = -1
    return total;
    

# Program
num=int(input("Input number "))

sum = calc (num)

if (sum==-1):
    print("The number is not in range")
else:
    print("The total of the calc is: ", sum)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plz help Ive tried for hours and cant do these three problems VB4 1 1,944 Mar-17-2018, 04:59 AM
Last Post: micseydel
  Simple exercise - how to write in python tomtom55 6 4,914 Sep-28-2017, 07:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020