Time complexity: O(nlog(n)) for average case. Worst case O(n^2). Usually better than other nlog(n).

The idea is really simpe and adheres ‘divide and conquer’ paradigm:

  1. choose 1st element as a pivot
  2. put elems that bigger to ‘high’ group and qsort it //recursion
  3. put elems that smaller in ‘low’ group and qsort it //recursion
  4. base case: return if only 1 elem

Writing it in python takes only few lines:

1
2
3
4
5
6
7
8
9
10
11
12
def qsort(arr):
    print arr
    if len(arr) < 2:
        return arr
    pivots = [x for x in arr if x == arr[0]]
    lo = qsort([x for x in arr if x < arr[0]])
    hi = qsort([x for x in arr if x > arr[0]])

    return lo + pivots + hi

array = [11, 50, 2, 43, 6, 100, 8, 20, 3, 15]
print qsort(array)
(Read More...)

Time complexity: O(n*log(n)) for any case.

The idea is really simpe and adheres ‘divide and conquer’ paradigm:

  1. divide on halves until get list with length equal 1 //just a recursive calls
  2. merge recursively // takes log(n) merges with n comparison in each

Writing it in python takes only few lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def merge(l, r):
    result = []
    i = j = 0
    while i < len(l) and j < len(r):
        if l[i] < r[j]:
            result.append(l[i])
            i += 1
        else:
            result.append(r[j])
            j += 1
    result += l[i:]
    result += r[j:]
    return result

def msort(a):
    if len(a) == 1:
        return a
    mid = len(a) / 2
    return merge(msort(a[:mid]), msort(a[mid:]))


array = [101, 50, 2, 43, 6, 100, 8, 20, 3, 15]
print array
print msort(array)
(Read More...)

In the previos post I tried to access location data using AssetsLibrary. However this method is depreciated, today we will try to use PHAsset from Photos framework

So the code of block below retrieves all the assets of type. Image and prints location info if available

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import UIKit
import Photos
import CoreMedia
import CoreLocation
import AddressBook

//fetch all photo assets from device
if let allAssets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: nil){    
    //enumerate through all assets efficiently doing the block of code
    allAssets.enumerateObjectsUsingBlock({asset, index, stop in
        
        //optional unwrapping to make sure assets have what we need
        if let ass = asset as? PHAsset{
            if let location = ass.location{                
                println(location)                
            }else{
                println("No location for this asset")
            }
        }else{
            println("Error while converting to PHAsset")
        }
    })
    println("Finished running location getter")
}else{
    println("Error with fetching the assets")
}

Then, to display the actual address instead of location coordinates we can use CLgeodecoder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import CoreLocation
import AddressBook

//used to convert coordinates to the physical address
let geocoder = CLGeocoder()

//request Apple server to geodecode the coordinates
//works async, be careful!
geocoder.reverseGeocodeLocation(location) { (data, error) -> Void in
    
    //get address and extract city and state
    let address = data[0].addressDictionary!
    let city = address[kABPersonAddressCityKey] as String
    let state = address[kABPersonAddressStateKey] as String
    
    //unique id for the place - need to be carefully taken care of
    let place = city + ", " + state
    prinln(place)
}

All this works great except Apple restricts the amount of requests to the server for geodecoding. Hope that helps.

(Read More...)

The purpose of this block of code is to print to the console the city and region where the photo, selected via standard ios image picker controller, was taken.

In this example I used ALAssets from AssetsLibrary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!){
    let library = ALAssetsLibrary()
    let URL: AnyObject? = info["UIImagePickerControllerReferenceURL"]
    var error: NSError?
    library.assetForURL(URL as NSURL, resultBlock: { (asset) -> Void in
        let location = asset.valueForProperty(ALAssetPropertyLocation) as? CLLocation
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(location!) { (data, error) -> Void in
            let address = data[0].addressDictionary!
            let city = address[kABPersonAddressCityKey] as  NSString!
            let state = address[kABPersonAddressStateKey] as  NSString!
            println(city, state)
        }
    }) { (error) -> Void in
        println(error)
    }
}
(Read More...)