{"id":339,"date":"2013-01-15T07:13:58","date_gmt":"2013-01-15T14:13:58","guid":{"rendered":"http:\/\/www.gdjogos.com.br\/blog\/?p=339"},"modified":"2021-06-18T15:56:28","modified_gmt":"2021-06-18T18:56:28","slug":"exercicios-resolvidos-python","status":"publish","type":"post","link":"https:\/\/www.galirows.com.br\/meublog\/programacao\/exercicios-resolvidos-python\/","title":{"rendered":"Exerc\u00edcios resolvidos em Python"},"content":{"rendered":"\n<p>Diversos exerc\u00edcios resolvidos e explicados em v\u00eddeo. S\u00e3o algoritmos em linguagem Python e envolvendo o uso de fun\u00e7\u00f5es, vetores e matrizes.<\/p>\n\n\n\n<p><br><strong>Exerc\u00edcio 1:<\/strong> fa\u00e7a um algoritmo que solicite ao usu\u00e1rio n\u00fameros e os armazene em um vetor de 30 posi\u00e7\u00f5es. Crie uma fun\u00e7\u00e3o que recebe o vetor preenchido e substitua todas as ocorr\u00eancia de valores positivos por 1 e todos os valores negativos por 0.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">def troca(vet):\n    for i in range(3):\n        if vet[i] &gt;= 0:\n            vet[i] = 1\n        else:\n            vet[i] = 0\n    return vet\n\nvet = [0]*3\nfor i in range(3):\n    vet[i] = input('Digite um valor: ')\nprint vet\ntroca(vet)\nprint vet<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/8r0pu_vM5hY\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 2: <\/strong>crie uma fun\u00e7\u00e3o que retorne o valor da express\u00e3o: 2\/3 + 3\/5 + 4\/7 + 5\/9 + \u2026 + n\/m, para um valor de <i><b>n<\/b><\/i> definido pelo usu\u00e1rio. Verifique se o valor de <i><b>n<\/b><\/i> definido pelo usu\u00e1rio \u00e9 positivo e, caso n\u00e3o seja, solicite outro valor at\u00e9 ser fornecido um valor positivo.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">def sequ(n):\n    aux1 = 2\n    aux2 = 3.0\n    soma = 0\n\n    while aux1 &lt;= num:\n        print aux1, aux2\n        soma = soma + aux1\/aux2\n        aux1 = aux1 + 1\n        aux2 = aux2 + 2\n\n    return soma\n\nnum = input('Digite um valor: ')\nwhile num &lt; 0:\n    num = input('Digite um valor positivo: ')\n\nres = sequ(num)\nprint res<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/DZvi1nSYVNw\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 3: <\/strong>Fa\u00e7a um algoritmo que solicite ao usu\u00e1rio n\u00fameros e os armazene em um vetor de 20 posi\u00e7\u00f5es. Crie uma fun\u00e7\u00e3o que recebe o vetor preenchido e substitua todas as ocorr\u00eancias de valores negativos por zero, as ocorr\u00eancias de valores menores do que 10 por 1 e as demais ocorr\u00eancias por 2.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">def altera(vet, tam):\n    for i in range(tam):\n        if vet[i] &lt; 0:\n            vet[i] = 0\n        elif vet[i] &lt; 10:\n            vet[i] = 1\n        else:\n            v[i] = 2\n    return vet\n\ntam = 39\n\nv = [0]*tam\nfor i in range(tam):\n    v[i] = input('Digite um valor: ')\naltera(v,tam)\nprint v<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/KYIySggs-so\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 4: <\/strong>crie um algoritmo que solicite 3 valores que representar\u00e3o os lados de um tri\u00e2ngulo. Considere que n\u00e3o importa a ordem que ser\u00e3o fornecidos os valores, podendo ser fornecido primeiro a hipotenusa e depois os catetos, ou primeiro os catetos e depois a hipotenusa, etc. Crie tamb\u00e9m uma fun\u00e7\u00e3o que recebe o vetor e retorna se os lados informados formam um tri\u00e2ngulo ret\u00e2ngulo. Voc\u00ea pode utilizar o teorema de Pit\u00e1goras para auxiliar na resolu\u00e7\u00e3o: hiponusa<sup>2<\/sup> = cateto1<sup>2<\/sup> + cateto2<sup>2<\/sup>.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import math\n\ndef triangret(vet):\n    if vet[0] &gt; vet[1] and vet[0] &gt; vet[2]:\n        hip = vet[0]\n        cat1 = vet[1]\n        cat2 = vet[2]\n    elif vet[1] &gt; vet[0] and vet[1] &gt; vet[2]:\n        hip = vet[1]\n        cat1 = vet[0]\n        cat2 = vet[2]\n    else:\n        hip = vet[2]\n        cat1 = vet[0]\n        cat2 = vet[1]\n    print hip, cat1, cat2\n\n    if hip == math.sqrt(cat1**2 + cat2**2):\n        return 1\n    else:\n        return 0\n\nvet = [0]*3\nfor i in range(3):\n    vet[i] = input('Digite um valor: ')\n\nif triangret(vet) == 1:\n    print '\u00c9 retangulo'\nelse:\n    print 'N\u00e3o \u00e9 retangulo'<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/R8aPcd2l2EM\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 5: <\/strong>fa\u00e7a um algoritmo que solicite ao usu\u00e1rio n\u00fameros e os armazene em uma matriz 6&#215;6. Em seguida, crie um vetor que armazene os elementos da diagonal principal da matriz.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">vet = [0]*6\nmat = [0]*6\nfor x in range(6):\n    mat[x] = [0]*6\n\nfor lin in range(6):\n    for col in range(6):\n        mat[lin][col] = input('Digite um valor: ')\nprint mat\n\nfor lin in range(6):\n    for col in range(6):\n        print lin,col\n        if lin == col:\n            print 'DP', lin, col\n            vet[lin] = mat[lin][col]\nprint vet<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/drIym29Nmio\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 6: <\/strong>tendo uma matriz 10&#215;10 preenchida com valores aleat\u00f3rios entre 10 e 50, mostre a m\u00e9dia dos elementos da diagonal secund\u00e1ria.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import random\n\ntam = 10\nm = [0]*tam\nfor i in range(tam):\n    m[i] = [0]*tam\nfor i in range(tam):\n    for j in range(tam):\n        m[i][j] = random.randint(10,50)\n\naux = tam-1\nsoma = 0\n\nfor i in range(tam):\n    soma = soma + m[i][aux]\n    aux = aux - 1\n\nfor i in range(tam):\n    print m[i][:]\nprint 'M\u00e9dia: ', soma\/float(tam)<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/tAzoqa1KLkI\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 7: <\/strong>tendo uma matriz 10&#215;10 preenchida com valores aleat\u00f3rios entre 10 e 50, mostre qual \u00e9 o maior valor existente na matriz desconsiderando os elementos da diagonal principal.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import random\nmat = [0]*3\nfor i in range(3):\n    mat[i] = [0]*3\n\nfor i in range(3):\n    for j in range(3):\n        mat[i][j] = random.randint(10,50)\nprint mat\n\nmaior = mat[1][0]\nfor i in range(3):\n    for j in range(3):\n        if i != j:\n            if mat[i][j] &gt; maior:\n                maior = mat[i][j]\nprint maior<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/bokzzj5DaaM\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 8: <\/strong>tendo uma matriz 5&#215;5 preenchida com valores aleat\u00f3rios entre 0 e 99, mostre qual \u00e9 o <b>segundo maior<\/b> valor existente na matriz.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import random\ntam = 5\n\nmat = [0]*tam\nfor i in range(tam):\n    mat[i] = [0]*tam\nprint mat\n\nfor i in range(tam):\n    for j in range(tam):\n        mat[i][j] = random.randint(0, 99)\nprint mat\n\nmaior = mat[0][0]\nfor i in range(tam):\n    for j in range(tam):\n        if mat[i][j] &gt; maior:\n            maior = mat[i][j]\nprint maior\n\nsegundomaior = 0\nfor i in range(tam):\n    for j in range(tam):\n        if mat[i][j] &gt; segundomaior and mat[i][j] != maior:\n            segundomaior = mat[i][j]\nprint segundomaior<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/Dl6iwHENxXE\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 9: <\/strong>crie um algoritmo que leia um valor e a partir disso fa\u00e7a: (1) se o valor for 1, 2 ou 3, mostre o valor elevado ao quadrado; (2) se o valor for o n\u00famero 4 ou 9, mostre a raiz quadrada do n\u00famero; (3) se for os valores 6, 7 e 8, mostre o valor dividido 9.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import math\n\nnum = input('Digite um valor: ')\nif num &gt;= 1 and num &lt;= 3:\n    print num**2\nelif num == 4 or num == 9:\n    print math.sqrt(num)\nelif num == 6 or num == 7 or num == 8:\n    print num\/9.0<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/wdOVsUYSzzg\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 10: <\/strong>crie um algoritmo que leia um valor e a partir disso fa\u00e7a: (1) se for um valor negativo, mostre o m\u00f3dulo (valor sem sinal) do valor; (2) se for um valor maior do que 10, solicite outro valor e mostre a diferen\u00e7a entre eles; (3) Caso o valor n\u00e3o seja de nenhuma condi\u00e7\u00e3o anterior, mostre o valor dividido por 5.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">num = input('Digite um valor: ')\nif num &lt; 0:\n    print num * -1\nelif num &gt; 10:\n    num2 = input('Digite outro valor: ')\n    print num - num2\nelse:\n    print num\/5.0<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/Y8iI-8taFQo\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Exerc\u00edcio 11: <\/strong>crie um algoritmo que leia um valor e a partir disso fa\u00e7a: (1) se o valor for 1 e 2, mostre o valor elevado ao cubo; (2) se o valor for m\u00faltiplo de 3 mostre o fatorial desse n\u00famero; (3) se o valor for os valores 4, 5, 7 ou 8, mostre o valor dividido 9. Caso n\u00e3o seja nenhum dos valores, informe como \u201cvalor inv\u00e1lido\u201d.<\/p>\n\n\n\n<pre class=\"wp-block-code brush:py\"><code lang=\"python\" class=\"language-python\">import math\nnum = input('Digite um valor: ')\nif num == 1 or num == 2:\n    print num**3\nelif num%3 == 0:\n    print math.factorial(num)\nelif num == 4 or num == 5 or num ==7 or num == 8:\n    print num\/9.0\nelse:\n    print 'Valor inv\u00e1lido'<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/axlB_jdbvsI\" allowfullscreen=\"allowfullscreen\" width=\"620\" height=\"450\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Diversos exerc\u00edcios resolvidos e explicados em v\u00eddeo. S\u00e3o algoritmos em linguagem Python e envolvendo o uso de fun\u00e7\u00f5es, vetores e matrizes. Exerc\u00edcio 1: fa\u00e7a um algoritmo que solicite ao usu\u00e1rio n\u00fameros e os armazene em um vetor de 30 posi\u00e7\u00f5es. Crie uma fun\u00e7\u00e3o que recebe o vetor preenchido e substitua todas as ocorr\u00eancia de valores [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,4],"tags":[13,8,14],"class_list":["post-339","post","type-post","status-publish","format-standard","hentry","category-python","category-videos","tag-matriz","tag-modularizacao-funcoes","tag-vetor"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/comments?post=339"}],"version-history":[{"count":9,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":1096,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/339\/revisions\/1096"}],"wp:attachment":[{"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/media?parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/categories?post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/tags?post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}