P

@Pallavir

String Reverse

C#
3 years ago
using System; using System.Text; namespace MyCompiler { class Program { public static void Main(string[] args) { string str="Pallavi"; int count= str.Length; Char[] str1= str.ToCharArray();

Enumerations

Swift
3 years ago
enum myEnum:Int{ case A, B } var enumDemo = myEnum.B print(enumDemo)

Hiigher order functions

Swift
3 years ago
//for each let numbersInWord = ["One", "Two", "Three", "Four", "Five", "Six"] numbersInWord.forEach { element in print(element) } //map() let numbers = [1, 2, 3, 4, 5, 6, 7] let numbersInString = numbers.map {

Exponent Operator

Swift
3 years ago
func pow (_ base:Int, _ power:Int) -> Int { var answer : Int = 1 for _ in 0..<power { answer *= base } return answer } var a : Int = 3 var b : Int = 2 var c = a ^ b

Equatable Protocol

Swift
3 years ago
struct Person { var name: String var age: String } //To make that Equatable you need to add the Equatable conformance. If don’t want to check all properties for equality, or if any of your properties are not also Equatable, then you ne

Hashable protocol

Swift
3 years ago
struct GridPoint { var x: Int var y: Int } extension GridPoint: Hashable { static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y }

Concurrency

Swift
3 years ago
func fetchUserID(from server: String) async -> Int { if server == "primary" { return 97 } return 501 } func fetchUsername(from server: String) async -> String { let userID = await fetchUserID(from: server)

Generics

Swift
3 years ago
struct IntStack { var items: [Int] = [] mutating func push(_ item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() }

Extensions

Swift
3 years ago
//Computed properties extension Double { var km: Double { return self * 1_000.0 } var m: Double { return self } var cm: Double { return self / 100.0 } var mm: Double { return self / 1_000.0 } var ft: Double { return self

Protocols

Swift
3 years ago
protocol FullyNamed { var fullName: String { get } } struct Person: FullyNamed { var fullName: String } let john = Person(fullName: "John Appleseed") print("\(john.fullName)" )

Property Observer

Swift
3 years ago
class StepCounter { var totalSteps: Int = 0 { willSet(newTotalSteps) { print("About to set totalSteps to \(newTotalSteps)") } didSet { if totalSteps > oldValue { print("Adde

PropertyWrapper

Swift
3 years ago
@propertyWrapper struct TwelveOrLess { private var number = 0 var wrappedValue: Int { get { return number } set { number = min(newValue, 12) } } } struct SmallRectangle {

Explicity Conversion and Arrays

Swift
3 years ago
let explicitDouble: Double = 70 let label = "The width is " let width = 94 let widthLabel = label + String(width) let apples = 3 let oranges = 5 let appleSummary = "I have \(apples) apples."

Subscripts

Swift
3 years ago
struct TimesTable { let multiplier: Int subscript(index: Int) -> Int { return multiplier * index } } let threeTimesTable = TimesTable(multiplier: 3) print("six times three is \(threeTimesTable[6])")

Error Handling

Swift
3 years ago
enum PrinterError: Error { case outOfPaper case noToner case onFire } func send(job: Int, toPrinter printerName: String) throws -> String { if printerName == "Never Has Toner" { throw PrinterError.noToner }

Structs and classes

Swift
3 years ago
// Here width and height are stored properties struct Resolution { var width = 0 var height = 0 } class VideoMode { var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String?

Struct Computed Properties

Swift
3 years ago
struct Point { var x = 0.0, y = 0.0 } struct Size { var width = 0.0, height = 0.0 } struct Rect { var origin = Point() var size = Size() var center: Point {

Structs Initailization

Swift
3 years ago
struct Color { let red, green, blue: Double init(red: Double, green: Double, blue: Double) { self.red = red self.green = green self.blue = blue } init(white: Double) { red = white