Skip to main content

Posts

Showing posts from September, 2021

Printing Staircase Pattern : Swift coding challenge

In this post, we can try interesting pattern printing challenge in Swift. The problem statement goes like this " Print a staircase of given size 'n'. Make sure that its base and height are both equal to n, and the image is drawn only using `#` symbols and spaces. The last line is not preceded by any spaces." Expected Output : # ## ### #### ##### ###### Working solution: func makePatternOf ( _ size : Int ) { var str = "" // 1 for index in ( 0 ..< size ) { let stop = size -index-1; // 2 for _ in 0 ..< stop { str. append ( " " ) ; } // 3 for _ in 0 .. . index { str. append ( "#" ) ; } print ( str ) str = "" } } makePatternOf ( 6 ) Loop to visit every row of stair case. Loop for appe...

HackerRank Challenges : Migratory Birds solution in Swift

In this post solution written in Swift for Migratory Bird Practice challenge from HackerRank. The problem statement goes like this - "Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids"  For Example, Given array [1, 1, 2, 3, 3] There are two each of types 1 and 3, and one sighting of type 2. Pick the lower of the two types seen twice: type 1. Working solution in Swift 5.0:      func   migratoryBirds ( arr :   [ Int ])   ->   Int   {          // Write your code here          var   dict : [ String :   Int ]   =   [ : ]          // 1          arr . forEach ({  ...

Codility Challenge : Tennis tournament

In this post, we have Swift 5.0 solution for - Given the number of players P and the number of reserved courts C, returns the maximum number of games that can be played in parallel. For Example, Given P = 5 players and C = 3 available courts, the function should return 2. Working solution :      public func solution ( _ P: Int , _ C: Int ) -> Int {     // 1     if P < 2 || C < 1 {       return 0     }          // 2     return P >= (C * 2 ) ? C : (P / 2 ) % C   }    solution ( 5 , 3 ) // returns 2 When insufficient Players or Courts, games can not be played. If total players exceeds courts capacity, all courts occupied & games will be played. Otherwise return total number of games played from Players strength. Full detail of exercise challenge : https://app.codility.com/programmers/trainings/3/tennis_tournament/ Happy coding !

Codility National Coding Week Sep-2021 : String transformation solution in Swift

In this post Swift 5.0 solution given for National Coding Week Sep-2021 challenge. Given a string S of letters 'a' and/or 'b', replaces some consecutive sequence "abb" in S by "baa". Example,  S = "ababb" then  "ababb" → "abbaa" → "baaaa". Working solution :      public func solution ( _ S : inout String ) -> String {       var hasFound = true       var res = Array(S)       // 1       while hasFound && res.count > 2 {         hasFound = false         //2         for index in 0 ..<res.count- 2 {           // 3           if res[index] == "a" && res[index+ 1 ] == "b" && res[index+ 2 ] == "b" {             hasFound = true             res[index] = "b"         ...

Codility Challenge : Find longest sequence of zeros in binary representation of an integer (Binary gap)

In this post Swift 5.0 solution given for the Codility exercise. Solving Binary gap for given positive integer. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. Complete details of exercise  :  https://app.codility.com/programmers/lessons/1-iterations/binary_gap/ Here is the working solution:      public func solution ( _ N : Int ) -> Int {              var max_gap: UInt = 0       var current_gap: UInt = 0               // 1       let binSequence = Array(String(N, radix: 2 ))       var index = 0              while index < binSequence.count {                  // 2         if binSequence[index] == "1" {                      // 3...