Skip to content

Instantly share code, notes, and snippets.

View aniltv06's full-sized avatar

Anil T V aniltv06

View GitHub Profile
@aniltv06
aniltv06 / LowestCommonAncestor.swift
Created September 16, 2023 02:51
Lowest Common Ancestor
class TreeNode {
var value: Int
var left: TreeNode?
var right: TreeNode?
init(_ value: Int) {
self.value = value
}
}
@aniltv06
aniltv06 / PrintInReverse.swift
Created September 13, 2023 00:36
Print in Reverse
/*Given a pointer to the head of a singly-linked list, print each value from the reversed list. If the given list is empty, do not print anything.
Example
refers to the linked list with values
Print the following:
3
2
1
Function Description
Complete the reversePrint function in the editor below.
reversePrint has the following parameters:
@aniltv06
aniltv06 / MaxConsecutiveOnes.swift
Created February 28, 2021 21:57
Max Consecutive Ones
/*
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:
@aniltv06
aniltv06 / RotateArray.swift
Created February 28, 2021 21:29
Rotate Array
/*Given an array, rotate the array to the right by k steps, where k is non-negative.
Follow up:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
Example 1:
@aniltv06
aniltv06 / DesignableUITextField.swift
Created April 4, 2019 21:21
Textfield with image placeholder
import UIKit
@IBDesignable
class DesignableUITextField: UITextField {
// Provides left padding for images
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
var textRect = super.leftViewRect(forBounds: bounds)
textRect.origin.x += leftPadding
return textRect
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/// A Mark is an value in the range 1...9
///
/// An assertion failure will be triggered if an attempt is made
@aniltv06
aniltv06 / BinarySearch.swift
Created August 22, 2018 06:00
Binary search written in swift
func binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>) -> Int? {
if range.lowerBound >= range.upperBound {
// If we get here, then the search key is not present in the array.
return nil
} else {
// Calculate where to split the array.
let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2
// Is the search key in the left half?
@aniltv06
aniltv06 / LCM.swift
Created August 22, 2018 05:53
Find LCM and GCD, written in swift
/*
Returns the Greatest Common Divisor of two numbers.
*/
func gcd(_ x: Int, _ y: Int) -> Int {
var a = 0
var b = max(x, y)
var r = min(x, y)
while r != 0 {
a = b
@aniltv06
aniltv06 / navigationLabel.swift
Created May 4, 2018 08:35
UINavigationBar multi-line title
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 44.0))
label.backgroundColor = UIColor.clear
label.numberOfLines = 0
label.textAlignment = NSTextAlignment.center
label.text = "FirstLine\nSecondLine"
label.textColor = .white
self.navigationItem.titleView = label
func fetchCount(array: [Int], length: Int, symmetric: Bool) -> Int {
var count = 0
for index in array.indices where index + length < array.count {
let subArray = array[index..<(index + length)]
print("Sub-array \"\(subArray)\" is \(subArray.allEqual() == true ? "Symmetrical" : "Asymmetrical")")
if symmetric == subArray.allEqual() {
count = count + 1
}
}