-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathContents.swift
57 lines (46 loc) · 1.46 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Foundation
/*
* Reto #40
* TRIÁNGULO DE PASCAL
* Fecha publicación enunciado: 03/10/22
* Fecha publicación resolución: 10/10/22
* Dificultad: MEDIA
*
* Enunciado: Crea una función que sea capaz de dibujar el "Triángulo de Pascal" indicándole
* únicamente el tamaño del lado.
* - Aquí puedes ver rápidamente cómo se calcula el triángulo:
* https://commons.wikimedia.org/wiki/File:PascalTriangleAnimated2.gif
*
* Información adicional:
* - Usa el canal de nuestro Discord (https://mouredev.com/discord) "🔁reto-semanal"
* para preguntas, dudas o prestar ayuda a la comunidad.
* - Tienes toda la información sobre los retos semanales en
* https://retosdeprogramacion.com/semanales2022.
*
*/
func pascalTriangle(size: Int) {
if size < 0 {
return
}
var lastRow: [Int] = []
for row in 0..<size {
var currentRow: [Int] = []
var triangleRow = ""
for element in 0...row {
if element > 0 && element < row {
let value = lastRow[element - 1] + lastRow[element]
triangleRow += "\(value) "
currentRow.append(value)
} else {
triangleRow += "1 "
currentRow.append(1)
}
}
print(String(repeating: " ", count: size - row) + triangleRow)
lastRow = currentRow
}
}
pascalTriangle(size: 5)
pascalTriangle(size: 1)
pascalTriangle(size: 0)
pascalTriangle(size: -5)