PROGRAM-iOS-JAVA

 PROGRAM


/////////////////////

https://teams.microsoft.com/_#/pre-join-calling/19:meeting_Y2EyYTI4MjItZDRmZC00MjgyLTk0ODQtNTQxNmY4YzRjMjcz@thread.v2


https://gist.github.com/leemorgan/b26288e6909435c3193e

JD- IndusInd Bank


https://www.fullstack.cafe/blog/sorting-algorithms-interview-questions

/////////////////////////////////////////////////////////////////////////////////////////////


Fibonacci.swift


func fibonacci(_ n: Int) -> Int {

    guard n != 0, n != 1 else { return n }

    return fibonacci(n - 1) + fibonacci(n - 2)

}

 

func fibonacci(_ i: Int) -> Int { if i <= 2 { return 1 } else { return fibonacci(i - 1) + fibonacci(i - 2) } } let numbers = Array(1...13).map { fibonacci($0) } print(numbers)


/////////////////////////////////////////////////////////////////////////////////////////////

// Using Recursion


func fibonacciRecursiveNum1(num1: Int, num2: Int, steps: Int) {





    if steps > 0 {


        let newNum = num1 + num2


        fibonacciRecursiveNum1(num2, num2: newNum, steps: steps-1)


    }


    else {


        print("result = \(num2)")


    }


}


fibonacciRecursiveNum1(0, num2: 1, steps: 7)


// Fibonacci series


// F[n] = F[n-1] + F[n-2]


// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144


// Find the fibonacci number for n interations


/////////////////////////////////////////////////////////////////////////////////////////////


func fibonacci(n: Int) {




    var num1 = 0


    var num2 = 1




    for _ in 0 ..< n {


    


        let num = num1 + num2


        num1 = num2


        num2 = num


    }


    


    print("result = \(num2)")


}


fibonacci(7)

// Fibonacci series


// F[n] = F[n-1] + F[n-2]


// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144


// Find the fibonacci number for n interations


/////////////////////////////////////////////////////////////////////////////////////////////

func fib(_ n: Int) -> Int { return n < 2 ? n : (fib(n-1) + fib(n-2)) }

/////////////////////////////////////////////////////////////////////////////////////////////

Prime Number : number that is divisble itself.

import Foundation

var flag : Bool = false;

let number : Int = 13;

for i in 2…number/2 {

if(number % i == 0){

flag = true

break;

}

}

if flag == false {

print(“\(number ) is prime!”);

else {

print(“\(number ) is not prime!”);

}


/////////////////////////////////////////////////////////////////////////////////////////////


  1. func findFactorial(of num: Int) -> Int {  
  2.     if num == 1 {  
  3.         return 1  
  4.     } else {  
  5.         return num * findFactorial(of:num - 1)  
  6.     }  
  7. }  
  8.   
  9. let x = 6  
  10. let result = findFactorial(of: x)  
  11. print("The factorial of \(x) is \(result)")   



func factorial(n: Int) -> Int {

    return n <= 1 ? 1 : n * factorial(n: n - 1)

}

 

let num = 5

let result = factorial(n: num)

print("\(num)! = \(result)")


/////////////////////////////////////////////////////////////////////////////////////////////

func factorial(n: Int) -> Int {

    var result = 1

    if(n > 0) {

        for i in 1...n {

            result *= i

        }

    }

    return result

}

 

let num = 4

let result = factorial(n: num)

print("\(num)! = \(result)")



/////////////////////////////////////////////////////////////////////////////////////////////

pelindrome


import UIKit


var rev = 0

var rem = 0

var n = 142

var no = n

no=n


while n != 0 {

    rem = n%10

    rev = rev * 10 + rem

    n /= 10

}

if(no == rev)

{

    print("\(no)" + " is pelindrome")

}

else

{

    print("\(no)" + " is  not pelindrome")

   

}


Result:


/////////////////////////////////////////////////////////////////////////////////////////////

Permutation

P(5, 3) = 5! / (5 - 3)! = 120 / 2 = 60.


func permutations(n: Int, _ k: Int) -> Int {

  var n = n

  var answer = n

  for _ in 1..<k {

    n -= 1

    answer *= n

  }

  return answer

}


permutations(5, 3)   // returns 60


/////////////////////////////////////////////////////////////////////////////////////////////

let str = "Hello, world!"

let reversed = String(str.reversed())

print(reversed)



func reverse(_ s: String) -> String {

 var str = ""

 for character in s.characters {

    str = "\(character)" + str

 }

 return str

}

print (reverse("!pleH"))



let inputstr = "ABCDEFGHIGKLMNPO"

var resultstr = "";

    for strchar in inputstr {

     resultstr = String(strchar) + resultstr

    }

                  print("Result = ",resultstr)


/////////////////////////////////////////////////////////////////////////////////////////////

let string = readLine()!

var resultString = ""

for i in 1...string.count {

    let index = string.index(string.endIndex, offsetBy: -i)

    resultString.append(string[index])

}

print(resultString)



/////////////////////////////////////////////////////////////////////////////////////////////

Access Levels in Swift!

An entity describes an object, including its name, attributes, and relationships. Create an entity for each of your app's objects.



/////////////////////////////////////////////////////////////////////////////////////////////

Difference between Frame and Bounds in Swift



private func printAll() {

    print("=========================")

    print("X : ", self.orangeView.frame.origin.x)

    print("Y : ", self.orangeView.frame.origin.y)

    print("width : ", self.orangeView.frame.size.width)

    print("height : ", self.orangeView.frame.size.height)

    print("=========================")

    print("X : ", self.orangeView.bounds.origin.x)

    print("Y : ", self.orangeView.bounds.origin.y)

    print("width : ", self.orangeView.bounds.size.width)

    print("height : ", self.orangeView.bounds.size.height)

    print("=========================")

}


=========================

X :  87.0

Y :  384.0

width :  240.0

height :  128.0

=========================

X :  0.0

Y :  0.0

width :  240.0

height :  128.0

=========================



Frame :  View's location and size using the parent view's coordinate system

Needed while placing the view in the parent


bounds = View's location and size using its own coordinate system

Needed while placing the view's content or subviews within itself


/////////////////////////////////////////////////////////////////////////////////////////////

What is flatMap in Swift?

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.


If you need to simply transform a value to another value, then use map . If you need to remove nil values, then use compactMap . If you need to flatten your result one level down, then use flatMap . It is also possible to chain these functions to achieve the intended result.


So map transforms an array of values into an array of other values, and flatMap does the same thing, but also flattens a result of nested collections into just a single array. ... In fact, mapping is a much more general concept, that can be applied in many more situations than just when transforming arrays.


/////////////////////////////////////////////////////////////////////////////////////////////

Use reduce to combine all items in a collection to create a single new value. The reduce method takes two values, an initial value and a combine closure. For example, to add the values of an array to an initial value of 10.0: let items = [2.0,4.0,5.0,7.0] let total = items.


/////////////////////////////////////////////////////////////////////////////////////////////

let numbers = [1, 2, 3, 4, 5]

let doubled = numbers.map { $0 * 2 }


That will take each value in the array and run it through our closure, where $0 refers to the number in question. So, it will be 1 2, 2 2, 3 * 2, and so on – map() will take a value out of its container, transform it using the code you specify, then put it back in its container. In this case, that means taking a number out of an array, doubling it, and putting it back in a new array.

/////////////////////////////////////////////////////////////////////////////////////////////

let definitelyNumbers = strings.compactMap { Int($0) }


There are lots of places in Swift that return optionals, including try?as?, and any failable initializer like creating an integer from a string – these are all great candidates for compactMap().

compactMap() will perform the same transformation as Map but then unwrap the optionals and discard any nil values.


/////////////////////////////////////////////////////////////////////////////////////////////


Associated types and generic type parameters are very different kinds of tools: associated types are a language of description, and generics are a language of implementation.


An associated type gives a placeholder name to a type that's used as part of the protocol. The actual type to use for that associated type isn't specified until the protocol is adopted. Associated types are specified with the associatedtype keyword.



/////////////////////////////////////////////////////////////////////////////////////////////

Now how do we achieve both of them in Swift ?

  • To achieve Dynamic Dispatch, we use inheritance, subclass a base class and then override an existing method of the base class. Also, we can make use of dynamic keyword and we need to prefix it with @objc keyword so as to expose our method to Objective-C runtime
  • To achieve Static Dispatch, we need to make use of final and static as both of them ensures that the class and method cannot be overridden



/////////////////////////////////////////////////////////////////////////////////////////////


class MyTVDataSource: NSObject, UITableViewDataSource{

    private var tableView: UITableView

    private var items = ["Item 1","item 2","item 3","Item 4"]


    init(tableView: UITableView) {

        self.tableView = tableView

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return items.count


    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = ItemCell(style: .default, reuseIdentifier: "Cell")

        cell.label.text = items[indexPath.row]


        return cell

    }


}



class MyTVDelegate: NSObject,UITableViewDelegate{


   var presentingController: BluePresenting?


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            presentingController?.currentSelected(indexPath)

    }

}




/////////////////////////////////////////////////////////////////////////////////////////////



var array1 = [2.1, 2.2, 2.5, 3.0, 4.2, 2]


var array2 = array1.sort(){ $0 > $1}


//One way 

let firstMax = array2[0]

let secondMax = array2[1]


//Second way

let firstMax = array2.removeFirst()

let secondMax = array2.removeFirst()




/////////////////////////////////////////////////////////////////////////////////////////////

Time complexity of binary search is O(Logn). See Binary Search for more details.



/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////



































Comments