Skip to content

Instantly share code, notes, and snippets.

View vicxruiz's full-sized avatar

Victor Ruiz vicxruiz

View GitHub Profile
Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it's main diagonal,
switching the row and column indices of the matrix.
Example 1:
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]
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:
@vicxruiz
vicxruiz / Sum
Last active April 8, 2020 15:41
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].
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:
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]
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
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"
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)
}
}
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)
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
}