Skip to content

Instantly share code, notes, and snippets.

View alimir1's full-sized avatar

Ali Mir alimir1

View GitHub Profile
import { useState, useEffect } from 'react';
import { ethers } from "ethers";
import { bufferToHex } from "ethereumjs-util";
import { encrypt as metamaskEncrypt } from '@metamask/eth-sig-util';
function Example() {
const [encryptedMessage, setEncryptedMessage] = useState(null);
const [encryptionPublicKey, setEncryptionPublicKey] = useState(null);
const [decryptedString, setDecryptedString] = useState(null);
const [accounts, setAccounts] = useState(null);
import Foundation
class Animal: NSCopying {
var name: String
init(name: String) {
self.name = name
}
func copy(with zone: NSZone? = nil) -> Any {
class Animal {
var name: String
init(name: String) {
self.name = name
}
}
let animal = Animal(name: "Lion")
let animalCopy = animal
enum FruitType: String {
case fuji, gala, crispin
}
struct Fruit {
var type: FruitType
let expirationDate: Date
}
var apple = Fruit(type: .fuji, expirationDate: Date())
@alimir1
alimir1 / BinaryTree-DFS-Iterative-Swift4.swift
Last active June 23, 2018 06:02
Iterative Implementation of Binary Search Tree - Depth First Search (DFS) in Swift 4 - In order, pre order, and post order.
class TreeNode: Hashable {
var val: Int
var left: TreeNode?
var right: TreeNode?
init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
self.val = val
self.left = left
self.right = right
}
@alimir1
alimir1 / smallestKth.swift
Created January 8, 2017 21:09
Find Kth smallest element in array
func findSmallestKth(_ array: [Int], _ k: Int) -> Int {
let pivot = array.first!
let arraySmallerThanPivot = array.dropFirst().filter {$0 < pivot}
let arrayLargerThanPivot = array.dropFirst().filter { $0 > pivot }
let newArray = arraySmallerThanPivot + [Int(pivot)] + arrayLargerThanPivot
let positionOfPivot = array.count > 1 ? arraySmallerThanPivot.count : 0
if k-1 == positionOfPivot {
return newArray[positionOfPivot]
} else if k-1 > positionOfPivot {
return findSmallestKth(arrayLargerThanPivot, k-1-positionOfPivot)
@alimir1
alimir1 / QuickSortSwift3.swift
Last active December 2, 2017 13:08
Quick Sort in Swift 3
func quickSort<T: Comparable>(array: [T]) -> [T] {
guard !array.isEmpty else { return [] }
let pivot = array.first!
let arrayLessThanOrEqualToPivot = array.dropFirst().filter { $0 <= pivot }
let arrayGreaterThanOrEqualToPivot = array.dropFirst().filter { $0 > pivot }
return quickSort(arrayLessThanOrEqualToPivot) + [Int(pivot)] + quickSort(arrayGreaterThanOrEqualToPivot)
}
@alimir1
alimir1 / checkIfPalindrome.swift
Last active August 2, 2023 19:06
Check if string is palindrome Swift 3
func isPalindrome(_ word: String) -> Bool {
let word = word.lowercased().characters.filter{ $0 != " " }
for (i, character) in word.enumerated() {
if character != word[word.count-i-1] {
return false
}
}
return true
}
@alimir1
alimir1 / ActivityIndicator
Last active November 12, 2021 13:45
Activity indicator with loading text label under (written in swift)
//
// ActivityIndicator.swift
//
// Created by Ali Mir on 11/17/16.
// Copyright © 2016 com.AliMir. All rights reserved.
//
import UIKit
struct ActivityIndicator {