Skip to content

Instantly share code, notes, and snippets.

@lunhg
Last active August 9, 2020 16:26
Show Gist options
  • Save lunhg/a9f4bdc5e5726c0d0cc1fe3b229a7bc8 to your computer and use it in GitHub Desktop.
Save lunhg/a9f4bdc5e5726c0d0cc1fe3b229a7bc8 to your computer and use it in GitHub Desktop.
Algoritmos descritos no capítulo 3 do livro "Algortimos e Lógica de Programação" de Marco M. Furlan de Souza
#################################
# Uso:
# $> python bhaskara.py --help
# $> python bhaskara.py --valor-a 1 --valor-b 2 --valor-c 3
# $> python bhaskara.py -a 1 -b 2 -c 3
# Para inserir valores negativos:
# $> python bhaskara.py -a -1 -b -2 -c -3
#################################
from optparse import OptionParser
import math
# PROGRAMA PRINCIPAL
PROG = "bhaskara"
VERSION = "0.0.1"
description = "aplicação do algoritmo que calcula equações do tipo ax^2 + bx + c = 0"
parser = OptionParser(usage='usage: %prog [OPTIONS, [ARGS]]',
version='%s %s' % (PROG, VERSION),
description=description)
parser.add_option("-a", "--valor-a", action=None, help="variável a")
parser.add_option("-b", "--valor-b", action=None, help="variável b")
parser.add_option("-c", "--valor-c", action=None, help="variável c")
# 1. Ler os valores de H, d e gama
(options, args) = parser.parse_args()
a = float(options.valor_a)
b = float(options.valor_b)
c = float(options.valor_c)
# funcao para calcular delta
def delta(a, b, c):
return math.pow(b, 2) - (4 * a * c)
# funcao para calcular raiz 1
def raiz_1(a, b, d):
return (-(b) + math.sqrt(d)) / (2 * a)
# funcao para calcular raiz 2
def raiz_2(a, b, d):
return (-(b) - math.sqrt(d)) / (2 * a)
# algortimo
if (a != 0 ):
d = delta(a, b, c)
if (d >= 0):
print({ 'x1': raiz_1(a, b, d), 'x2': raiz_2(a, b, d) })
else:
print("Não existem raízes reais")
else:
print("Não é equação de segundo grau")
#################################
# Uso:
# $> python coluna_liquido.py --help
# $> python coluna_liquido.py --altura 18 --diametro 15 --gama 2
# $> python coluna_liquido.py -H 18 -d 15 -g 2
#################################
from optparse import OptionParser
import math
# PROGRAMA PRINCIPAL
PROG = "coluna-liquido"
VERSION = "0.0.1"
description = "aplicação do algoritmo que calcula a força exercida pela coluna de um liquido"
parser = OptionParser(usage='usage: %prog [OPTIONS, [ARGS]]',
version='%s %s' % (PROG, VERSION),
description=description)
parser.add_option("-H", "--altura", action=None, help="altura da coluna")
parser.add_option("-d", "--diametro", action=None, help="diametro valvula")
parser.add_option("-g", "--gama", action=None, help="gama")
# 1. Ler os valores de H, d e gama
(options, args) = parser.parse_args()
# 2. Calcular F <- (pi * gama * sqrt(d) * h) / 4
def F(gama, diametro, altura):
return (math.pi * float(gama) * math.pow(float(diametro), 2) * float(altura)) / 4
print("%s" % F(options.gama, options.diametro, options.altura))
#################################
# Uso:
# $> python euclides.py --help
# $> python euclides.py --valor-1 18 --valor-2 15
# $> python euclides.py --valor-1 18 --valor-2 15 --verbose
# $> python euclides.py -a 18 -b 15
# $> python euclides.py -a 18 -b 15 -V
#################################
from optparse import OptionParser
# PROGRAMA PRINCIPAL
PROG = "euclides"
VERSION = "0.0.1"
description = "aplicação do algoritmo de euclides para calcular o máximo divisor comum entre dois números inteiros"
parser = OptionParser(usage='usage: %prog [OPTIONS, [ARGS]]',
version='%s %s' % (PROG, VERSION),
description=description)
parser.add_option("-a", "--valor-1", action=None, help="Valor decimal 1")
parser.add_option("-b", "--valor-2", action=None, help="Valor decimal 2")
parser.add_option("-V", "--verbose", action="store_true", help="modo verborrágico")
(options, args) = parser.parse_args()
a = int(options.valor_1)
b = int(options.valor_2)
if(options.verbose):
options.iteracao = 0
while(b != 0):
if (options.verbose):
print("iteração %s: resto de %d/%d = %d" % (options.iteracao, a, b, (a % b)))
options.iteracao = options.iteracao + 1
resto = a % b
a = b
b = resto
print("MDC(%s, %s) = %d" % (options.valor_1, options.valor_2, a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment