Posts

Showing posts with the label Swift 4

Filter,Map and Reduce Higher Order Functions in Swift

Image
Let's code : Take an Input array : let inputArr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] print ( "Input Array : \ ( inputArr )" ) Perform Filter on array : let filterArr = inputArr. filter ({ return $0 % 2 == 0 }) print ( "Filtered Array (even elements) \ ( filterArr )" ) Perform Map on array   : let mapArr = inputArr. map ({ return $0 * 2 }) print ( "Map Array (*2) \ ( mapArr )" ) Perform Reduce on array : let reduce = inputArr. reduce ( 0 , +) print ( "Reduced Value (Sum of elements) \ ( reduce )" ) Here is the output :

Generic Stack implementation with Linked List using Swift

Image
Let's code : Create a generic Node class : class Node<T> {     let nodeVal : T     var next : Node ?     init (value : T ) {         self . nodeVal = value     } } Now, implement a Generic Stack class : class Stack<T> {     var top : Node < T >?     func push( _ value: T ){         let oldTop = top         top = Node (value: value)         top ?. next = oldTop         print ( "Push Item to Stack : \ ( value )" )     }     func pop() -> T ?{         print ( "Popped Item from Stack : " )         let currentTop = top         top = top ?. next         return currentTop?. nodeVal     } } Call from anywhere : imp...

Stack implementation with Linked List using Swift

Image
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Let's Code : First make a simple Node class , with a data and  a pointer: class Node {     let nodeVal : Int     var next : Node ?     init (value : Int ) {         self . nodeVal = value     } } Now make the Stack Class, As we know Stack follows LIFO. We will implement  Push() and Pop() method within the class to Push and Pop elements. class Stack {     var top : Node ?     func push( _ value: Int ){         let oldTop = top         top = Node (value: value)         top ?. next = oldTop         print ( "Push Item to Stack : \ ( value )" )     }     func pop() ->...

Append and Traverse a Doubly Linked List using Swift - iOS

Image
Let's Code : OK, let's see the result first , Adding some random city names to linked list and printing the linked list : Let's create the node now : Node.swift import Foundation class Node: NSObject {     var next: Node ?     weak var previous: Node ?     var value : String ?     init (value : String ) {         self . value = value     } } Now implement linked list in another swift file DoublyLinkedList. swift  import UIKit class DoublyLinkedList: NSObject {     fileprivate var head : Node ?     private var tail : Node ?     override init () {         super . init ()     }     public var isEmpty : Bool {         return head == nil     }     public var first : Node ? {         ...

Number of Sum Pairs in an Array using Swift - iOS Coding Challenge

Image
Let's Code : Remember we have to pass sorted array to this function : func numberOfSumPair(sortedArray : [ Int ] , key : Int ) -> Int {     var startPointer = 0     var endPointer = sortedArray. count - 1     var sumPairs = 0     if sortedArray. count < 2 {         return sumPairs     }     while startPointer < endPointer {         let sum = sortedArray[startPointer] + sortedArray[endPointer]         if sum == key {             print ( "Pairs : \ ( sortedArray[startPointer] ) + \ ( sortedArray[endPointer] )" )             sumPairs += 1             startPointer += 1         }         if sum > key {             endPointer -= 1 ...

Binary Search using Swift - iOS Coding Challenge

Image
Let's Code //: Playground - noun: a place where people can play import UIKit import Foundation var array : [ Int ] = [ 1 , 4 , 2 , 3 , 5 , 6 , 76 , 32 , 43 , 12 , 55 , 88 , 99 , 49 , 76 , 78 , 90 , 11 , 99 ] let key = 55 func binarySearch(array : [ Int ] , key : Int ) -> Bool {     let minIndex = 0     let maxIndex = array. count - 1     let midIndex = maxIndex / 2     let midValue = array[midIndex]          if (key < array[minIndex] || key > array[maxIndex]){         return false     }     if key < midValue {         let arraySlice = Array (array[ 0 ...midIndex - 1 ])         return binarySearch (array: arraySlice, key: key)     }     if key > midValue {         let arraySlice = Array (array[midIndex + 1 ...maxIndex]) ...

Merge Sort using Swift - iOS Coding Challenge

Image
Complexity : O(n log n) Let's Code : //: A UIKit based Playground for presenting user interface    import UIKit import Foundation var sortThisArray : [ Int ] = [ 11 , 2 , 43 , 12 , 45 , 2 , 42 , 66 , 70 , 21 , 23 , 48 , 88 , 65 , 99 , 21 ] func merge(left:[ Int ],right:[ Int ]) -> [ Int ] {     var mergedArr : [ Int ] = []     var left = left     var right = right          while left. count > 0 && right. count > 0 {         if left. first ! < right. first ! {             mergedArr. append (left. removeFirst ())         }         else {             mergedArr. append (right. removeFirst ())         }     }     return mergedArr + left + right } func mergeSort(array: [ Int ]) -> [ Int...

Most Common Elements in an Array using Swift - iOS Coding Challenge

Image
Complexity : O(n) Let's Code : //: Playground - noun: a place where people can play import Foundation import UIKit var elementArray : [ String ] = [ "one" , "two" , "three" , "four" , "five" , "two" , "five" , "two" , "one" , "two" , "five" , "three" , "three" , "four" , "one" , "four" , "three" , "five" ] func getMostCommonElementFromArray(array:[ String ]) -> [ String ] {     var mostOccuredElements : [ String ] = []     var elementDict : [ String : Int ] = [:]     for item in array {         if let count = elementDict[item] {             elementDict[item] = count + 1         } else {             elementDict[item] = 1         }     }     let highestVal = elementDict. values . max ()     for (element, _ ) in elementDict {     ...