Jump to content

Pergunta sobre variaveis


bernardus
 Share

Recommended Posts

Boas.

Antes de mais os parabens pelo excelente trabalho realizado.

A minha duvida é a seguinte.

Eu gostava de fazer um script em python para fazer o seguinte.

10 ou mais variaveis.

Eu sei a variavel final mas nao sei que como chegar a variavel final.

ex.

x

y

z

eu sei que a soma de isto é (vamos supor) A

mas vamos imaginar que existe mais variaveis alem dos x,y,z etc etc.

Como faço para que ele me calcule a probablidade de chegar ao A????

abraço

O que eu queria era que ele me fizesse a probablidades

Link to comment
Share on other sites

Bem desculpem lá 🙂

Por exemplo eu tenho uma lista com os totais e com o detalhado.

O que eu queria era que através de pyhton fazer um soft para me dar as possibilidades, através  soma de algum detalhado desse um dos totais.

EX.

detalhado

>>>>>>>>>>

1

2

3

4

5

>>>>>>>>>>

Totais

>>>>>>>>>>

8

7

Quero que o soft me diga as como chegar a um dos totais

EX

Total 8= 5+3

Total 7=4+3

So que com valores mt maiores

Abraço

Link to comment
Share on other sites

boas.

Talvez com o pseudo codigo seja mais simples entenderam 😛

int vector[100]; // 100 numeros a verificar no maximo...

int matriz[100][100]; // cada linha terá uma possibilidade e cada coluna terá os números necessários para atingir a soma. (máximo de 100 possibilidades somando um máximo de 100 números [altamente exagerado])

int n_valores;

int valor_a_descobrir;

int linhas_matriz = 0;

int colunas_matriz = 0;

bool valor_encontrado = false;

print "a fazer reset à matriz de resultados..."

for(int i = 0; i < 100; i++)

  for(int x = 0; x < 100; x++)

    matriz[x] = 0;

print "quantos numeros? "

read n_valores;

for(int i = 0; i < n_valores ; i++)

{

  print "entre com o valor numero {i}: "

  read vector;

}

print "qual o valor a descobrir? "

read valor_a_descobrir;

print "a pesquisar..."

// percorrer todos os valores inseridos

for(int i = 0; i < n_valores; i++)

{

  print "a testar valores para o {i}º numero..."

  soma=0;

  colunas_matriz = 0;

  // percorrer todos os valores inseridos

  for(int x = 0; x < n_valores ; x++)

  {

    soma+=vector[x];

    if(soma>valor_a_descobrir)

      soma-=vector[x];

    else

      matriz[linhas_matriz][colunas_matriz++]=vector[x];

    if(soma==valor_a_descobrir)

      valor_encontrado=true;

  }

  if(valor_encontrado)

  {

    linhas_matriz++;

    valor_encontrado=false;

  }

}

print "Foram encontradas {linhas_matriz} possibilidades!"

print "a mostrar resultados..."

for(int i = 0; i < linhas_matriz;i++)

{

  if(linhas_matriz [0]!=0) // significa que inserimos qualquer coisa...logo, há um resultado nesta linha!

  {

    print "{valor_a_descobrir} = "

    for(x=0;linhas_matriz [x]!=0;x++)

    {

      print "{matriz [x]}"

      if((x < 99) && matriz [x+1]!=0)

        print " + "

    }

  } 

}

A minha duvida é passar isto para python. eu sei passar para c# mas como estou a começar no fantastico mundo python peço a vossa ajuda 👍

abraço

Link to comment
Share on other sites

Usa pontos em vez de vírgulas.

list = (1.5883, 2.2332, 3.1, 4.0, 5.44) # lista dos detalhados

Depois no print tens que usar %f em vez de %d.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

o gajo nao compila:(

list = (134.35,3687.46,724.76,345.67,3336.89,200.45,1000.32,57.45,200.43,7004.68,3951.09,461.87) # lista dos detalhados

tot = (737.9,5671.16) # lista dos totais

for i in list:

        for l in list:

                for t in tot:

                        if i+l == t:

                                print "%f + %f = %f" % (i, l, t)

Link to comment
Share on other sites

Primeira observação: não uses "list" como nome de variáveis, visto que é uma função do próprio Python (http://docs.python.org/library/functions.html#list)

Segunda : para criar uma lista, usa os parêntesis quadrados, em vez dos redondos:

list = [134.35,3687.46,724.76,345.67,3336.89,200.45,1000.32,57.45,200.43,7004.68,3951.09,461.87]

O que tu estás a criar são tuplos, não listas.

No meu PC ele não dá erros, simplesmente não imprime nada, suponho que seja problema do algoritmo.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

desculpem o double post.

mas com este codigo ele nao dava erro nenhum e trabalhava direitinho 😛

list = (1, 2, 3, 4, 5) # lista dos detalhados

tot = (7, 8) # lista dos totais

for i in list:

        for l in list:

                for t in tot:

                        if i+l == t:

                                print "%d + %d = %d" % (i, l, t)

Mas desde que tentei meter numeros decimais o gajo nao mostra nada B)

abraço

Link to comment
Share on other sites

E tens a certeza que há algum valor que dê bem? É que eu fiz só as somas e nenhuma me deu esses resultados...

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

tenho porque esses valores sao de testes e confirmados manualmente.

era fixe uma ajuda +. ja deves ter percebido o problema.

lista de detalhados

lista de totais

e atraves dos detalhados dar as possibilidades de um dos totais. como o codigo acima

list = (1, 2, 3, 4, 5) # lista dos detalhados

tot = (7, 8) # lista dos totais

for i in list:

        for l in list:

                for t in tot:

                        if i+l == t:

                                print "%d + %d = %d" % (i, l, t)

so que quando meto os decimais chapéu.

Any help please B)

Link to comment
Share on other sites

Epá, eu fiz simplesmente:

ll = [134.35,3687.46,724.76,345.67,3336.89,200.45,1000.32,57.45,200.43,7004.68,3951.09,461.87] # lla dos detalhados

for i in ll:
    for l in ll:
        print "%f + %f = %f" % (i, l, (i+l))

E os resultados são:

134.350000 + 134.350000 = 268.700000
134.350000 + 3687.460000 = 3821.810000
134.350000 + 724.760000 = 859.110000
134.350000 + 345.670000 = 480.020000
134.350000 + 3336.890000 = 3471.240000
134.350000 + 200.450000 = 334.800000
134.350000 + 1000.320000 = 1134.670000
134.350000 + 57.450000 = 191.800000
134.350000 + 200.430000 = 334.780000
134.350000 + 7004.680000 = 7139.030000
134.350000 + 3951.090000 = 4085.440000
134.350000 + 461.870000 = 596.220000
3687.460000 + 134.350000 = 3821.810000
3687.460000 + 3687.460000 = 7374.920000
3687.460000 + 724.760000 = 4412.220000
3687.460000 + 345.670000 = 4033.130000
3687.460000 + 3336.890000 = 7024.350000
3687.460000 + 200.450000 = 3887.910000
3687.460000 + 1000.320000 = 4687.780000
3687.460000 + 57.450000 = 3744.910000
3687.460000 + 200.430000 = 3887.890000
3687.460000 + 7004.680000 = 10692.140000
3687.460000 + 3951.090000 = 7638.550000
3687.460000 + 461.870000 = 4149.330000
724.760000 + 134.350000 = 859.110000
724.760000 + 3687.460000 = 4412.220000
724.760000 + 724.760000 = 1449.520000
724.760000 + 345.670000 = 1070.430000
724.760000 + 3336.890000 = 4061.650000
724.760000 + 200.450000 = 925.210000
724.760000 + 1000.320000 = 1725.080000
724.760000 + 57.450000 = 782.210000
724.760000 + 200.430000 = 925.190000
724.760000 + 7004.680000 = 7729.440000
724.760000 + 3951.090000 = 4675.850000
724.760000 + 461.870000 = 1186.630000
345.670000 + 134.350000 = 480.020000
345.670000 + 3687.460000 = 4033.130000
345.670000 + 724.760000 = 1070.430000
345.670000 + 345.670000 = 691.340000
345.670000 + 3336.890000 = 3682.560000
345.670000 + 200.450000 = 546.120000
345.670000 + 1000.320000 = 1345.990000
345.670000 + 57.450000 = 403.120000
345.670000 + 200.430000 = 546.100000
345.670000 + 7004.680000 = 7350.350000
345.670000 + 3951.090000 = 4296.760000
345.670000 + 461.870000 = 807.540000
3336.890000 + 134.350000 = 3471.240000
3336.890000 + 3687.460000 = 7024.350000
3336.890000 + 724.760000 = 4061.650000
3336.890000 + 345.670000 = 3682.560000
3336.890000 + 3336.890000 = 6673.780000
3336.890000 + 200.450000 = 3537.340000
3336.890000 + 1000.320000 = 4337.210000
3336.890000 + 57.450000 = 3394.340000
3336.890000 + 200.430000 = 3537.320000
3336.890000 + 7004.680000 = 10341.570000
3336.890000 + 3951.090000 = 7287.980000
3336.890000 + 461.870000 = 3798.760000
200.450000 + 134.350000 = 334.800000
200.450000 + 3687.460000 = 3887.910000
200.450000 + 724.760000 = 925.210000
200.450000 + 345.670000 = 546.120000
200.450000 + 3336.890000 = 3537.340000
200.450000 + 200.450000 = 400.900000
200.450000 + 1000.320000 = 1200.770000
200.450000 + 57.450000 = 257.900000
200.450000 + 200.430000 = 400.880000
200.450000 + 7004.680000 = 7205.130000
200.450000 + 3951.090000 = 4151.540000
200.450000 + 461.870000 = 662.320000
1000.320000 + 134.350000 = 1134.670000
1000.320000 + 3687.460000 = 4687.780000
1000.320000 + 724.760000 = 1725.080000
1000.320000 + 345.670000 = 1345.990000
1000.320000 + 3336.890000 = 4337.210000
1000.320000 + 200.450000 = 1200.770000
1000.320000 + 1000.320000 = 2000.640000
1000.320000 + 57.450000 = 1057.770000
1000.320000 + 200.430000 = 1200.750000
1000.320000 + 7004.680000 = 8005.000000
1000.320000 + 3951.090000 = 4951.410000
1000.320000 + 461.870000 = 1462.190000
57.450000 + 134.350000 = 191.800000
57.450000 + 3687.460000 = 3744.910000
57.450000 + 724.760000 = 782.210000
57.450000 + 345.670000 = 403.120000
57.450000 + 3336.890000 = 3394.340000
57.450000 + 200.450000 = 257.900000
57.450000 + 1000.320000 = 1057.770000
57.450000 + 57.450000 = 114.900000
57.450000 + 200.430000 = 257.880000
57.450000 + 7004.680000 = 7062.130000
57.450000 + 3951.090000 = 4008.540000
57.450000 + 461.870000 = 519.320000
200.430000 + 134.350000 = 334.780000
200.430000 + 3687.460000 = 3887.890000
200.430000 + 724.760000 = 925.190000
200.430000 + 345.670000 = 546.100000
200.430000 + 3336.890000 = 3537.320000
200.430000 + 200.450000 = 400.880000
200.430000 + 1000.320000 = 1200.750000
200.430000 + 57.450000 = 257.880000
200.430000 + 200.430000 = 400.860000
200.430000 + 7004.680000 = 7205.110000
200.430000 + 3951.090000 = 4151.520000
200.430000 + 461.870000 = 662.300000
7004.680000 + 134.350000 = 7139.030000
7004.680000 + 3687.460000 = 10692.140000
7004.680000 + 724.760000 = 7729.440000
7004.680000 + 345.670000 = 7350.350000
7004.680000 + 3336.890000 = 10341.570000
7004.680000 + 200.450000 = 7205.130000
7004.680000 + 1000.320000 = 8005.000000
7004.680000 + 57.450000 = 7062.130000
7004.680000 + 200.430000 = 7205.110000
7004.680000 + 7004.680000 = 14009.360000
7004.680000 + 3951.090000 = 10955.770000
7004.680000 + 461.870000 = 7466.550000
3951.090000 + 134.350000 = 4085.440000
3951.090000 + 3687.460000 = 7638.550000
3951.090000 + 724.760000 = 4675.850000
3951.090000 + 345.670000 = 4296.760000
3951.090000 + 3336.890000 = 7287.980000
3951.090000 + 200.450000 = 4151.540000
3951.090000 + 1000.320000 = 4951.410000
3951.090000 + 57.450000 = 4008.540000
3951.090000 + 200.430000 = 4151.520000
3951.090000 + 7004.680000 = 10955.770000
3951.090000 + 3951.090000 = 7902.180000
3951.090000 + 461.870000 = 4412.960000
461.870000 + 134.350000 = 596.220000
461.870000 + 3687.460000 = 4149.330000
461.870000 + 724.760000 = 1186.630000
461.870000 + 345.670000 = 807.540000
461.870000 + 3336.890000 = 3798.760000
461.870000 + 200.450000 = 662.320000
461.870000 + 1000.320000 = 1462.190000
461.870000 + 57.450000 = 519.320000
461.870000 + 200.430000 = 662.300000
461.870000 + 7004.680000 = 7466.550000
461.870000 + 3951.090000 = 4412.960000
461.870000 + 461.870000 = 923.740000

Nenhum dá algum desses dois, mesmo contando com os arredondamentos...

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Ah, é que o código dado pelo Imoogi só faz somas de dois números.

Tenta passar o código em C para Python, é razoavelmente fácil nesse caso. Tens aqui o início, se tiveres dúvidas no resto avisa!


#declarações:
vector = [0.0] * 100                                  #100 numeros a verificar no maximo...
matriz = [[0.0] * 100 for i in range(100)]   #cada linha terá uma possibilidade e cada coluna terá os números necessários para atingir a soma. (máximo de 100 possibilidades somando um máximo de 100 números [altamente exagerado])
n_valores = 0              #isto não é necessário
valor_a_descobrir = 0.0  #idem
linhas_matriz = 0
colunas_matriz = 0
valor_encontrado = False

n_valores = int(raw_input("quantos numeros? "))
for i in range(n_valores):
    vector[i] = float(raw_input("entre com o valor numero {%d}: " % i))

valor_a_descobrir = float( raw_input( "qual o valor a descobrir? "))

print "a pesquisar..."

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Eu não me importo de fazê-lo, mas esse código tem uns problemas.

A variável linhas_matriz é um inteiro, mas nesta linha é tratada como um array:

  if(linhas_matriz[0]!=0) // significa que inserimos qualquer coisa...logo, há um resultado nesta linha!

E podiar pôr o código entre tags [code ], porque há caracteres que não se lêem bem,

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.