Skip to content

Instantly share code, notes, and snippets.

Avatar

Anil T V aniltv06

View GitHub Profile
View MaxConsecutiveOnes.swift
/*
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:
View RotateArray.swift
/*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 Apr 4, 2019
Textfield with image placeholder
View DesignableUITextField.swift
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
View Sudoku.swift
//: 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 Aug 22, 2018
Binary search written in swift
View BinarySearch.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 Aug 22, 2018
Find LCM and GCD, written in swift
View LCM.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
UINavigationBar multi-line title
View navigationLabel.swift
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
View arraySymmCount.swift
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
}
}
@aniltv06
aniltv06 / alert.swift
Created Mar 30, 2018
UIAlertController with text field and disable buttons if textfield is empty
View alert.swift
let alertController = UIAlertController(title: nil, message: alertMessageString, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: alertLeftBtuTitle, style: .default, handler: nil)
alertController.addAction(cancelAction)
let okAction = UIAlertAction(title: alertRightBtnText , style: UIAlertActionStyle.default, handler: {
(action) -> Void in
print("ok tapped")
})
okAction.isEnabled = false
alertController.addAction(okAction)
@aniltv06
aniltv06 / Slider.swift
Created Sep 29, 2016
Programatically adding UISlider - swift 3
View Slider.swift
override func viewDidLoad() {
super.viewDidLoad()
let slider = UISlider(frame:CGRectMake(70, 550, 280, 20))
slider.minimumValue = 0
slider.maximumValue = 100
slider.continuous = true
slider.tintColor = UIColor.redColor()
slider.value = 50
slider.addTarget(self, action: #selector(ViewController.sliderValueDidChange(_:)), forControlEvents: .ValueChanged)
self.view.addSubview(slider)