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


Messages In This Thread
[Learning] 3 hours for one simple exercise - by KoFu - Aug-25-2019, 02:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Plz help Ive tried for hours and cant do these three problems VB4 1 2,030 Mar-17-2018, 04:59 AM
Last Post: micseydel
  Simple exercise - how to write in python tomtom55 6 5,112 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