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
func numberOfVowels(in string: String) -> Int { | |
var numberOfVowels: Int = 0 | |
for x in string { | |
if x == "a" || x == "e" || x == "i" || x == "o" || x == "u" { | |
numberOfVowels += 1 | |
} | |
} | |
return numberOfVowels | |
} |
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
extension String { | |
func anotherContains(string: String) -> Bool { | |
if self.lowercased().range(of: string) != nil { | |
return true | |
} else if self.uppercased().range(of: string) != nil { | |
return true | |
} | |
else { | |
return false | |
} |
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
extension Int { | |
func expressNumber() -> [Int] { | |
var result: [Int] = [] | |
let stringNum = String(self) | |
var count = stringNum.count - 1 | |
for x in stringNum { | |
var element = String(x) | |
let str = String(repeating: "0", count: count) | |
element += str | |
result.append(Int(element) ?? 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
extension Array { | |
func shuffleArray() { | |
var shuffled: [Int] = [] | |
for i in 0..self.count { | |
let rand = Int(arc4random_uniform(UInt32(self.count))) | |
shuffled.appened(self[rand]) | |
shuffled.remove(at: rand) | |
} | |
} |
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
func doAsTheRomansDoAndNumeralize(elementInt: Int) -> String { | |
var element = elementInt | |
var romanNumeralString: String = "" | |
if element >= 1000 { | |
//divide element by 1000 till it's less than 1000 | |
let result = element / 1000 | |
if result >= 1 { | |
element = element % 1000 | |
romanNumeralString += "M" |
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
func isTwinPrime(_ number: Int) -> Bool { | |
if number > 1 && !(2..<number).contains { number % $0 == 0 } { | |
let lowerDifference = number - 2 | |
let higherDifference = number + 2 | |
if lowerDifference > 1 && !(2..<lowerDifference).contains { lowerDifference % $0 == 0 } { | |
return true | |
} | |
if higherDifference > 1 && !(2..<higherDifference).contains { higherDifference % $0 == 0 } { | |
return true |
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
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. | |
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. | |
Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. | |
Example 1: | |
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] |
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
Write a program that outputs the string representation of numbers from 1 to n. | |
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. | |
For numbers which are multiples of both three and five output “FizzBuzz”. | |
Example: | |
n = 15, | |
Return: |
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
Given an array of integers, return indices of the two numbers such that they add up to a specific target. | |
You may assume that each input would have exactly one solution, and you may not use the same element twice. | |
Example 1: | |
Given nums = [2, 7, 11, 15], target = 9, | |
Because nums[0] + nums[1] = 2 + 7 = 9, | |
return [0, 1]. |
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
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. | |
Find all the elements of [1, n] inclusive that do not appear in this array. | |
Example: | |
Input: | |
[4,3,2,7,8,2,3,1] | |
Output: |
OlderNewer