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
def getCount(inputStr): | |
num_vowels = 0 | |
for char in inputStr: | |
if char in "aeiouAEIOU": | |
num_vowels = num_vowels + 1 | |
return num_vowels |
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
def filter_list(l): | |
new_list =[] | |
for x in l: | |
if type(x) != str: | |
new_list.append(x) | |
return new_list |
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
def count_bits(n): | |
count = 0 | |
while n != 0 : | |
if n&1 : | |
count += 1 | |
n = n >> 1 | |
return count |
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
class TicTacToe { | |
var rows: [Int]! | |
var cols: [Int]! | |
var diagonal: Int! | |
var antiDiagonal: Int! | |
/** Initialize your data structure here. */ | |
init(_ n: Int) { | |
rows = [] |
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
class FileSystem { | |
class File { | |
var isFile = false | |
var files: [String: File] = [String: File]() | |
var content = "" | |
} | |
var root: File! | |
init() { | |
root = File() | |
} |
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
class BrowserHistory { | |
var history: [String]! | |
var current: Int! | |
var bound: Int! | |
init(_ homepage: String) { | |
history = [homepage] | |
current = 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
class Solution { | |
var deque: [Int] = [] | |
var nums: [Int]! = [] | |
func cleanDeque(_ i: Int, _ k: Int) { | |
if deque.isEmpty == false{ | |
if let first = deque.first, first == i - k { | |
deque.removeFirst() | |
} | |
} |
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
class Solution { | |
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int { | |
var grid = obstacleGrid | |
let row = grid.count | |
let col = grid[0].count | |
//Base case | |
if grid.isEmpty || grid[0][0] == 1 { return 0 } | |
grid[0][0] = 1 | |
for i in 1..<row { | |
grid[i][0] = (grid[i][0] == 0 && grid[i-1][0] == 1) ? 1 : 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
class Solution { | |
var hashMap: [String: String] = ["2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"] | |
func generateCombination(result: String, nextDigit: String, output: inout [String]) { | |
if nextDigit.isEmpty { | |
output.append(result) | |
return | |
}else { | |
var newDigit = nextDigit | |
let firstString = newDigit.removeFirst() | |
if let value = hashMap[String(firstString)] { |
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
let evenNumbers = [2,4,6,8,10] | |
let half = evenNumbers.map { $0/2 } | |
//[1, 2, 3, 4, 5] |