Skip to content

Commit 0bde001

Browse files
committed
feat: Pascal's Triangle
1 parent 640257f commit 0bde001

4 files changed

Lines changed: 174 additions & 5 deletions

File tree

Help.xcodeproj/project.pbxproj

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
5E023E8B2683692F002465D8 /* ArrayZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E023E8A2683692F002465D8 /* ArrayZip.swift */; };
1212
5E023E8F26863070002465D8 /* Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E023E8E26863070002465D8 /* Dictionary.swift */; };
1313
5E1F2F4927873121009446ED /* FactoryMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E1F2F4827873121009446ED /* FactoryMethod.swift */; };
14+
5E1F30D6278C9C5E009446ED /* Pascal'sTriangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E1F30D5278C9C5E009446ED /* Pascal'sTriangle.swift */; };
1415
5E2CF7C1266E20AE007BB2EB /* UnwindSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C0266E20AE007BB2EB /* UnwindSegue.swift */; };
1516
5E2CF7C5266E242E007BB2EB /* segue-002.png in Resources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C3266E242E007BB2EB /* segue-002.png */; };
1617
5E2CF7C6266E242E007BB2EB /* segue-001.png in Resources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C4266E242E007BB2EB /* segue-001.png */; };
@@ -60,6 +61,7 @@
6061
5E023E8A2683692F002465D8 /* ArrayZip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayZip.swift; sourceTree = "<group>"; };
6162
5E023E8E26863070002465D8 /* Dictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dictionary.swift; sourceTree = "<group>"; };
6263
5E1F2F4827873121009446ED /* FactoryMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FactoryMethod.swift; sourceTree = "<group>"; };
64+
5E1F30D5278C9C5E009446ED /* Pascal'sTriangle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Pascal'sTriangle.swift"; sourceTree = "<group>"; };
6365
5E2CF7C0266E20AE007BB2EB /* UnwindSegue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnwindSegue.swift; sourceTree = "<group>"; };
6466
5E2CF7C3266E242E007BB2EB /* segue-002.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "segue-002.png"; sourceTree = "<group>"; };
6567
5E2CF7C4266E242E007BB2EB /* segue-001.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "segue-001.png"; sourceTree = "<group>"; };
@@ -125,6 +127,14 @@
125127
path = DesignPatterns;
126128
sourceTree = "<group>";
127129
};
130+
5E1F30D4278C9BF9009446ED /* LeetCode */ = {
131+
isa = PBXGroup;
132+
children = (
133+
5E1F30D5278C9C5E009446ED /* Pascal'sTriangle.swift */,
134+
);
135+
path = LeetCode;
136+
sourceTree = "<group>";
137+
};
128138
5E2CF7BF266E2087007BB2EB /* UIKit */ = {
129139
isa = PBXGroup;
130140
children = (
@@ -175,14 +185,15 @@
175185
5E8385512667635000ACA770 /* Help */ = {
176186
isa = PBXGroup;
177187
children = (
188+
5E1F30D4278C9BF9009446ED /* LeetCode */,
189+
5EAF0B7F268C5E9200D92322 /* Features */,
178190
5E83856C2667679B00ACA770 /* SupportingFiles */,
179191
5E83857726677A6100ACA770 /* CleanCode */,
180-
5E8385812667B91000ACA770 /* Protocols */,
181-
5E8385842667BCAB00ACA770 /* Extensions */,
182-
5E8385872667C57600ACA770 /* WorkingCode */,
183-
5E2CF7BF266E2087007BB2EB /* UIKit */,
184-
5EAF0B7F268C5E9200D92322 /* Features */,
185192
5E1F2F4727872F9B009446ED /* DesignPatterns */,
193+
5E2CF7BF266E2087007BB2EB /* UIKit */,
194+
5E8385872667C57600ACA770 /* WorkingCode */,
195+
5E8385842667BCAB00ACA770 /* Extensions */,
196+
5E8385812667B91000ACA770 /* Protocols */,
186197
);
187198
path = Help;
188199
sourceTree = "<group>";
@@ -380,6 +391,7 @@
380391
5E88DEA9273C85D700363BF6 /* DismissKeyboard.swift in Sources */,
381392
5EAC92BC26BAA06300A6198C /* DeleteRow.swift in Sources */,
382393
5E023E8B2683692F002465D8 /* ArrayZip.swift in Sources */,
394+
5E1F30D6278C9C5E009446ED /* Pascal'sTriangle.swift in Sources */,
383395
5E8385532667635000ACA770 /* AppDelegate.swift in Sources */,
384396
5E5FE8262695A27B00C5A396 /* Comparable.swift in Sources */,
385397
5E5FE82426959E5100C5A396 /* Equatable.swift in Sources */,

Help/CleanCode/LifeCycle.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Lifecycle.swift
3+
// Help
4+
//
5+
// Created by Sergey Lukaschuk on 02.06.2021.
6+
//
7+
8+
import Foundation
9+
10+
11+
// MARK: Life Cycle
12+
/// We move the logic out of the lifecycle methods into separate methods. The logic inside the methods of the ViewController lifecycle should be moved into separate methods, even if you have to create a method with one line of code. Today one, and tomorrow ten.
13+
14+
/*
15+
❌ NOT Preferred
16+
17+
override func viewDidLoad() {
18+
super.viewDidLoad()
19+
20+
navigationController?.navigationBar.backgroundColor = .red
21+
someButton.layer.cornerRadius = 10
22+
someButton.layer.masksToBounds = true
23+
navigationItem.title = "Some"
24+
print("Some")
25+
}
26+
27+
28+
✅ Preferred
29+
30+
// MARK: - Lifecycle
31+
override func viewDidLoad() {
32+
super.viewDidLoad()
33+
34+
setupNavigationBar()
35+
setupSomeButton()
36+
printSome()
37+
}
38+
39+
40+
// MARK: - Private Methods
41+
private func setupNavigationBar() {
42+
navigationController?.navigationBar.backgroundColor = .red
43+
navigationItem.title = "Some"
44+
}
45+
46+
private func setupSomeButton() {
47+
someButton.layer.cornerRadius = 10
48+
someButton.layer.masksToBounds = true
49+
}
50+
51+
private func printSome() {
52+
print("Some")
53+
}
54+
*/
55+
56+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Pascal'sTriangle.swift
3+
// Help
4+
//
5+
// Created by Sergey Lukaschuk on 10.01.2022.
6+
//
7+
8+
import Foundation
9+
10+
11+
// MARK: Pascal's Triangle
12+
/*
13+
14+
Given an integer numRows, return the first numRows of Pascal's triangle.
15+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
16+
17+
Example 1:
18+
Input: numRows = 5
19+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
20+
21+
Example 2:
22+
Input: numRows = 1
23+
Output: [[1]]
24+
25+
*/
26+
27+
28+
29+
func generate(_ numRows: Int) -> [[Int]] {
30+
31+
guard numRows > 0 else { return [] }
32+
if numRows == 1 { return [[1]] }
33+
34+
var results = [[Int]]()
35+
results.append([1])
36+
37+
for x in 1..<numRows {
38+
var newRow = [1]
39+
let prevRow = results[x - 1]
40+
41+
for j in 1..<prevRow.count {
42+
let sum = prevRow[j] + prevRow[j - 1]
43+
newRow.append(sum)
44+
}
45+
newRow.append(1)
46+
results.append(newRow)
47+
}
48+
49+
return results
50+
}

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ In this project, I have collected various best practices and iOS development tip
6161
- [Factory Method](#factory-method)
6262

6363

64+
- [**Leet Code**](#leet-code)
65+
- [Pascal's Triangle](#pascal's-triangle)
66+
6467

6568
## **Clean Code**
6669
Raising code readability in iOS development. Thanks to the application of these tips, your code will become readable, which will further ensure convenience and speed of working with it.
@@ -1444,6 +1447,54 @@ class SomeViewController: UIViewController {
14441447
```
14451448
14461449
1450+
1451+
## **Leet Code**
1452+
1453+
1454+
1455+
### [Pascal's Triangle](https://github.com/lgreydev/Help/blob/master/Help/LeetCode/Pascal'sTriangle.swift)
1456+
1457+
Given an integer numRows, return the first numRows of Pascal's triangle.
1458+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
1459+
1460+
**Example 1:**
1461+
Input: numRows = 5
1462+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
1463+
1464+
**Example 2:**
1465+
Input: numRows = 1
1466+
Output: [[1]]
1467+
1468+
1469+
1470+
``` swift
1471+
1472+
func generate(_ numRows: Int) -> [[Int]] {
1473+
1474+
guard numRows > 0 else { return [] }
1475+
if numRows == 1 { return [[1]] }
1476+
1477+
var results = [[Int]]()
1478+
results.append([1])
1479+
1480+
for x in 1..<numRows {
1481+
var newRow = [1]
1482+
let prevRow = results[x - 1]
1483+
1484+
for j in 1..<prevRow.count {
1485+
let sum = prevRow[j] + prevRow[j - 1]
1486+
newRow.append(sum)
1487+
}
1488+
newRow.append(1)
1489+
results.append(newRow)
1490+
}
1491+
1492+
return results
1493+
}
1494+
1495+
```
1496+
1497+
14471498
### 🛡️ License
14481499
14491500
This project is licensed under the MIT License - see the [`LICENSE`](https://github.com/lgreydev/Help/blob/master/License) file for details.

0 commit comments

Comments
 (0)