# SESSION SIX: REPETIÇÃO FOR | LÓGICA Author: Igor Lima Charles N°: 18 Grade: INFOA ## TreinoFocadoA ```csharp= using System; using System.Collections.Generic; public class Notas { public int Nota1 {get; set;} public int Nota2 {get; set;} public int Nota3 {get; set;} } public class Retangulo { public double Base { get; set; } public double Altura { get; set; } } public class TreinoFocadoA { public List<int> GerarSequencia (int fim) { List<int> L1 = new List<int>(); for (int i = 0; i<= fim; i++) { L1.Add(i); } return L1; } public List<int> GerarSequencia(int inicio, int fim) { List<int> L2 = new List<int>(); for (int i = inicio; i <= fim; i++) { L2.Add(i); } return L2; } public List<int> GerarSequenciaPares(int inicio, int fim) { List<int> L3 = new List<int>(); for (int i = inicio; i <= fim; i++) { if (i % 2 == 0) L3.Add(i); } return L3; } public int SomarAte(int fim) { int soma = 0; for (int i = 1; i <= fim; i++) { soma += i; } return soma; } public double CalcularMedias(List<double> notas) { double soma = 0; for (int i = 0; i < notas.Count; i++) { double n = notas[i]; soma = soma + n; } double media = soma / notas.Count; return media; } public List<double> CalcularMedias(List<Notas> notas) { List<double> mediasAlunos = new List<double>(); for (int i = 0; i < notas.Count; i++) { Notas notasAluno = notas[i]; double media = (notasAluno.Nota1 + notasAluno.Nota2 + notasAluno.Nota3) / 3; mediasAlunos.Add(media); } return mediasAlunos; } public List<double> Quadrados (List<double> numeros) { List<double> quads = new List<double>(); for (int i = 0; i < numeros.Count; i++) { double n = numeros[i]; double q = Math.Pow(n, 2); quads.Add(q); } return quads; } private double AreaRetangulo(Retangulo r) { return r.Base * r.Altura; } public List<double> AreaRetangulos(List<Retangulo> retangulos) { List<double> areas = new List<double>(); for (int i = 0; i < retangulos.Count; i++) { Retangulo r = retangulos[i]; double area = AreaRetangulo(r); areas.Add(area); } return areas; } } TreinoFocadoA A = new TreinoFocadoA(); List<int> x1 = A.GerarSequencia(0); Console.WriteLine(string.Join("-", x1)); List<int> x2 = A.GerarSequencia(2, 4); Console.WriteLine(string.Join("-", x2)); List<int> x3 = A.GerarSequenciaPares(2, 4); Console.WriteLine(string.Join("-", x3)); int x4 = A.SomarAte(3); Console.WriteLine(x4); List<double> lx5 = new List<double>() { 4, 6, 8, 10 }; double x5 = A.CalcularMedias(lx5); Console.WriteLine(x5); Notas n1 = new Notas(); n1.Nota1 = 5; n1.Nota2 = 5; n1.Nota3 = 5; Notas n2 = new Notas(); n2.Nota1 = 10; n2.Nota2 = 10; n2.Nota3 = 10; Notas n3 = new Notas(); n3.Nota1 = 7; n3.Nota2 = 8; n3.Nota3 = 9; List<Notas> notas = new List<Notas>() { n1, n2, n3 }; List<double> x6 = A.CalcularMedias(notas); Console.WriteLine(string.Join("-", x6)); List<double> n7 = new List<double>() { 2,3,4,5 }; List<double> x7 = A.Quadrados(n7); Console.WriteLine(string.Join("-", x7)); List<Retangulo> AreasRet = new List<Retangulo>() { new Retangulo() { Base = 1, Altura = 3 }, new Retangulo() { Base = 5, Altura = 1 }, new Retangulo() { Base = 8, Altura = 2 } }; List<double> AreasRetangulo = A.AreaRetangulos(AreasRet); Console.WriteLine(string.Join("-", AreasRetangulo)); ``` ## TreinoFocadoB ```csharp= using System; using System.Collections.Generic; public class Pessoa { public string Nome { get; set; } public DateTime Nascimento { get; set; } public override string ToString() { return "(" + Nome + "," + Nascimento.ToShortDateString() + ")"; } } public class TreinoFocadoB { public List<DateTime> GerarSequencia(int qtd) { List<DateTime> L1 = new List<DateTime>(); DateTime inicio = DateTime.Now.Date; for (int i = 1; i <= qtd; i++) { L1.Add(inicio); inicio = inicio.AddDays(i); } return L1; } public List<DateTime> GerarSequencia(int qtd, DateTime data) { List<DateTime> L2 = new List<DateTime>(); DateTime inicio = data; for (int i = 1; i <= qtd; i++) { L2.Add(inicio); inicio = inicio.AddDays(1); } return L2; } public List<Pessoa> FiltrarMaiores18(List<Pessoa> pessoas) { List<Pessoa> PessoasFiltradas = new List<Pessoa>(); for (int i = 0; i < pessoas.Count; i++) { Pessoa p = pessoas[i]; if (p.Nascimento <= DateTime.Now.AddYears(-18)) { PessoasFiltradas.Add(p); } } return PessoasFiltradas; } public bool TodosMaiores18(List<Pessoa> Pessoas) { List<Pessoa> filtrados = new List<Pessoa>(); bool TodosMaiores18 = true; for (int i = 0; i < Pessoas.Count; i++) { Pessoa p = Pessoas[i]; if (p.Nascimento > DateTime.Now.AddYears(-18)) { TodosMaiores18 = false; } } return TodosMaiores18; } } TreinoFocadoB treinoB = new TreinoFocadoB(); List<DateTime> x1 = treinoB.GerarSequencia(10); Console.WriteLine(string.Join(" - ", x1)); List<DateTime> x2 = treinoB.GerarSequencia(5, new DateTime(2021, 6, 27)); Console.WriteLine(string.Join(" - ", x2)); List<Pessoa> pessoas = new List<Pessoa>() { new Pessoa() { Nome = "Pessoa A", Nascimento = new DateTime(1987, 12, 01) }, new Pessoa() { Nome = "Pessoa B", Nascimento = new DateTime(1999, 11, 11) }, new Pessoa() { Nome = "Pessoa C", Nascimento = new DateTime(2004, 08, 12) }, new Pessoa() { Nome = "Pessoa D", Nascimento = new DateTime(2004, 08, 05) }, new Pessoa() { Nome = "Pessoa E", Nascimento = new DateTime(1975, 05, 20) } }; List<Pessoa> x3 = treinoB.FiltrarMaiores18(pessoas); Console.WriteLine(string.Join(" - ", x3)); bool x4 = treinoB.TodosMaiores18(pessoas); Console.WriteLine(string.Join(" - ", x4)); ``` # TreinoFocadoC ```sql= using System; using System.Collections.Generic; public class TreinoFocadoC { public string SepararLetras(string frase) { string nova = string.Empty; for (int i = 0; i < frase.Length; i++) { char letra = frase[i]; nova = nova + letra + "-"; } return nova; } public string Inverter(string Frase) { string NovaFrase = string.Empty; for (int i = 0; i < Frase.Length; i++) { char Letra = Frase[i]; NovaFrase = Letra + NovaFrase; } return NovaFrase; } public bool ApenasCoresPrimarias(List<string> Cores) { bool todasPrimarias = true; for (int i = 0; i < Cores.Count; i++) { string Cor = Cores[i]; if (Cor != "Azul" && Cor != "Vermelho" && Cor != "Amarelo") { todasPrimarias = false; break; } } return todasPrimarias; } } TreinoFocadoC treinoC = new TreinoFocadoC(); string x1 = treinoC.SepararLetras("Professor, perdoa as minhas lições atrasadas"); Console.WriteLine(x1); string x2 = treinoC.Inverter("Ônibus"); Console.WriteLine(x2); bool x3 = treinoC.ApenasCoresPrimarias(new List<string>() { "Amarelo", "Azul" }); Console.WriteLine(x3); ```
{"metaMigratedAt":"2023-06-16T03:23:55.124Z","metaMigratedFrom":"Content","title":"SESSION SIX: REPETIÇÃO FOR | LÓGICA","breaks":true,"contributors":"[{\"id\":\"80bf1c37-2722-44c2-96a3-490c9bac8b2e\",\"add\":6879,\"del\":24}]"}
Expand menu