Skip to content

Commit 3d767da

Browse files
committed
add vs code support
1 parent fb2df73 commit 3d767da

6 files changed

Lines changed: 215 additions & 0 deletions

File tree

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"swiftlang.swift-vscode",
4+
"llvm-vs-code-extensions.lldb-dap",
5+
"formulahendry.code-runner"
6+
]
7+
}

.vscode/keybindings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"key": "cmd+r",
4+
"command": "code-runner.run",
5+
"when": "editorTextFocus && editorLangId == 'swift' && !inDebugRepl && workspaceFolderName == 'Algorithm-Solutions-In-Swift'"
6+
}
7+
]

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Requires extension "LLDB DAP" (llvm-vs-code-extensions.lldb-dap).
3+
// If debugging fails to start: Terminal → `xcrun -f lldb-dap` and set
4+
// "lldb-dap.executable-path" in settings to that path.
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb-dap",
9+
"request": "launch",
10+
"name": "Debug main.swift",
11+
"program": "${workspaceFolder}/.build/main-swift-out",
12+
"args": [],
13+
"cwd": "${workspaceFolder}",
14+
"preLaunchTask": "Swift: Build main (debug)",
15+
"console": "integratedTerminal",
16+
"stopOnEntry": false
17+
}
18+
]
19+
}

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
// Cmd+R → Code Runner (Swift): see `.vscode/keybindings.json`. Standard VS Code ignores that file;
3+
// Cursor may honor it. If ⌘R does nothing, open Keyboard Shortcuts (JSON) (⌘K ⌘S → “Open Keyboard
4+
// Shortcuts (JSON)”) and paste the array entries from `.vscode/keybindings.json` into your user file.
5+
"code-runner.runInTerminal": true,
6+
"code-runner.clearPreviousOutput": true,
7+
"code-runner.saveFileBeforeRun": true,
8+
"code-runner.executorMap": {
9+
"swift": "cd \"$dir\" && swift \"$fileName\""
10+
}
11+
}

.vscode/tasks.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Swift: Run main.swift",
6+
"type": "shell",
7+
"command": "swift",
8+
"args": [
9+
"${workspaceFolder}/Algorithm Solutions In Swift/main.swift"
10+
],
11+
"group": {
12+
"kind": "build",
13+
"isDefault": true
14+
},
15+
"presentation": {
16+
"reveal": "always",
17+
"panel": "shared",
18+
"focus": false
19+
},
20+
"problemMatcher": []
21+
},
22+
{
23+
"label": "Swift: Run current Swift file",
24+
"type": "shell",
25+
"command": "swift",
26+
"args": ["${file}"],
27+
"group": "build",
28+
"presentation": {
29+
"reveal": "always",
30+
"panel": "shared"
31+
},
32+
"problemMatcher": []
33+
},
34+
{
35+
"label": "Swift: Build main (debug)",
36+
"type": "shell",
37+
"command": "mkdir -p \"${workspaceFolder}/.build\" && swiftc -g \"${workspaceFolder}/Algorithm Solutions In Swift/main.swift\" -o \"${workspaceFolder}/.build/main-swift-out\"",
38+
"group": "build",
39+
"presentation": {
40+
"reveal": "silent",
41+
"panel": "shared"
42+
},
43+
"problemMatcher": []
44+
}
45+
]
46+
}

Algorithm Solutions In Swift/main.swift

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,129 @@ import Foundation
1111

1212

1313
//*************** START PRACTICE *************************
14+
func findPeakElement(_ nums: [Int]) -> Int {
15+
var left = 0, right = nums.count - 1
16+
while left >= 0, right < nums.count, left <= right {
17+
let mid = (left + right) / 2
18+
if isPeak(idx: mid) {
19+
return mid
20+
} else if mid > 0, nums[mid] < nums[mid - 1] {
21+
right = mid - 1
22+
} else {
23+
left = mid + 1
24+
}
25+
}
26+
return 0
1427

28+
func isPeak(idx: Int) -> Bool {
29+
let isGreaterThanLeftNeighbour = idx == 0 ? true : nums[idx - 1] < nums[idx]
30+
let isGreaterThanRightNeighbour = idx == nums.count - 1 ? true : nums[idx + 1] < nums[idx]
31+
return isGreaterThanLeftNeighbour && isGreaterThanRightNeighbour
32+
}
33+
}
34+
35+
36+
print(findPeakElement([1,2]))
37+
38+
39+
func shortestDistance(_ wordsDict: [String], _ word1: String, _ word2: String) -> Int {
40+
var i = -1, j = -1, distance = wordsDict.count
41+
for (idx, word) in wordsDict.enumerated() {
42+
if word == word1 {
43+
i = idx
44+
}
45+
if word == word2 {
46+
j = idx
47+
}
48+
if i > -1, j > -1 {
49+
distance = min(distance, abs(i - j))
50+
}
51+
52+
}
53+
return distance
54+
}
55+
56+
let words = ["practice", "makes", "perfect", "coding", "makes"]
57+
let word1 = "makes", word2 = "coding"
58+
//print(shortestDistance(words, word1, word2))
59+
60+
61+
func confusingNumber(_ n: Int) -> Bool {
62+
let invalidSet: Set<Int> = [2, 3, 4, 5, 7]
63+
let rotatedMap = [
64+
0 : 0,
65+
1 : 1,
66+
6 : 9,
67+
8 : 8,
68+
9 : 6
69+
]
70+
var num = n
71+
var currentNum = 0
72+
while num > 0 {
73+
let last = num % 10
74+
if invalidSet.contains(last) {
75+
return false
76+
}
77+
currentNum = currentNum * 10 + rotatedMap[last]!
78+
num /= 10
79+
}
80+
return currentNum != n
81+
}
82+
83+
84+
print(confusingNumber(89))
85+
86+
// [1, 1] [2, 2]
87+
88+
func fairCandySwap(_ aliceSizes: [Int], _ bobSizes: [Int]) -> [Int] {
89+
let totalAliceCandies = aliceSizes.reduce(0, +)
90+
let totalBobCandies = bobSizes.reduce(0, +)
91+
let bobCandyBoxes = Set(bobSizes)
92+
for aliceCandyBox in aliceSizes {
93+
let aliceExpects = aliceCandyBox + (totalBobCandies - totalAliceCandies) / 2
94+
if bobCandyBoxes.contains(aliceExpects) {
95+
return [aliceCandyBox, aliceExpects]
96+
}
97+
}
98+
return []
99+
}
100+
101+
print(fairCandySwap([2], [1, 3]))
102+
103+
104+
func isAlienSorted(_ words: [String], _ order: String) -> Bool {
105+
// create custom order map
106+
var alienOrder = [Character : Int]()
107+
for (idx, char) in order.enumerated() {
108+
alienOrder[char] = idx
109+
}
110+
111+
// compare adjacent words
112+
for i in 0..<words.count - 1 {
113+
let firstWord = Array(words[i])
114+
let secondWord = Array(words[i + 1])
115+
if !isOrdered(firstWord, secondWord) {
116+
return false
117+
}
118+
}
119+
return true
120+
121+
func isOrdered(_ first: [Character], _ second: [Character]) -> Bool {
122+
let minLength = min(first.count, second.count)
123+
for i in 0..<minLength {
124+
let w1 = first[i]
125+
let w2 = second[i]
126+
127+
if w1 != w2 {
128+
return alienOrder[w1]! < alienOrder[w2]!
129+
}
130+
}
131+
132+
return first.count <= second.count
133+
}
134+
}
135+
136+
137+
let words1 = ["hello","leetcode"]
138+
let order = "hlabcdefgijkmnopqrstuvwxyz"
139+
print(isAlienSorted(words1, order))

0 commit comments

Comments
 (0)