Last active
December 14, 2015 06:38
-
-
Save lrlucena/5043822 to your computer and use it in GitHub Desktop.
Lista de Exercícios 09.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que imprime todos os múltiplos de 7 entre 1 e 1000. | |
print (1..1000).select{|n| n%7 == 0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que gera uma matriz “tabuada” com 10 linhas e duas colunas. | |
# Cada linha deve ter um número entre 0 e 9 na primeira coluna e a multiplicação | |
# deste valor por 9 na segunda coluna. | |
puts (0..9).map{|n| [n, n*9]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# Escreva um programa que tira os acentos de um texto. As letras acentuadas | |
# devem ser substituídas pela letra sem acento. | |
texto = "Isto é um texto com acentuação." | |
semacentos = texto.split("").map{|c| | |
if "áâãà".include? c then "a" | |
elsif "éèê".include? c then "e" | |
elsif "íìî".include? c then "i" | |
elsif "óòôõ".include? c then "o" | |
elsif "úùû".include? c then "u" | |
elsif "ç".include? c then "c" | |
else c | |
end | |
}.join | |
puts semacentos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que gera um array com 20 elementos (numerados de 1 a 20). | |
# Cada elemento é definido da seguinte forma: ((-1)**i) / (2.0*i) | |
# Imprima também a soma dos vinte elementos. | |
elementos = (1..20).map do |i| | |
((-1)**i) / (2.0*i) | |
end | |
print elementos | |
puts elementos.inject(:+) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que lê 5 números inteiros e verifica se todos são positivos. | |
# Sugestão: use inject. | |
cinco = [3,-5,0,2,7] | |
positivos = cinco.map{|n| n>0}.inject{:&} | |
# Alternativa (linha 5) | |
# positivos = cinco.inject(true){|a,b| a and (b>0)} | |
puts positivos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que lê 5 números inteiros e verifica se pelo menos um é par. | |
cinco = [3,-5,0,2,7] | |
par = cinco.map{|n| n%2==0}.inject(:|) | |
# Alternativa (linha 4) | |
# par = cinco.inject(false){|a,b| a or (b%2==0)} | |
puts par |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa (usando inject) para calcular qual o maior número | |
# em uma lista de 10 números inteiros. | |
dez = [3,-5,0,2,7,1,2,3,4,7] | |
maior = dez.inject{|a,b| if a>b then a else b end} | |
puts maior |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que lê o nome de uma pessoa e gera um texto | |
# com as iniciais em letras maiúsculas. | |
nome = "Fulano de Tal" | |
iniciais = nome.split.map{|n| n[0].upcase}.join | |
puts iniciais |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Escreva um programa que leia o nome de uma pessoa e retira as palavras: de, da, dos, e. | |
nome = "Fulano de Tal" | |
retira = nome.split | |
.reject{|n| ["de", "da", "dos","e"].include? n} | |
.join(" ") | |
puts retira |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lista de Exercícios |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment