Skip to content

Instantly share code, notes, and snippets.

View sahid's full-sized avatar

Sahid Orentino Ferdjaoui sahid

View GitHub Profile
@sahid
sahid / gist:4973986
Last active December 13, 2015 20:58
Selction Sort in Python
def ssort(A):
if len(A) <= 1: return A
i = 0
while i < len(A):
imin, j = i, i
while j < len(A):
if A[imin] > A[j]:
A[j], A[i] = A[i], A[j]
imin = j
j += 1
@sahid
sahid / gist:4973851
Last active December 13, 2015 20:58
Find the least common ancestor in a binary tree in Python.
def lca(node, n1, n2):
if match(node.left, n1) and match(node.left, n2):
return lca(node.left, n1, n2) # until we find the least common ancestor by node.left
if match(node.right, n1) and match(node.right, n2):
return lca(node.right, n1, n2) # until we find the least common ancestor by node.right
return node
def match(node, n):
if node is None: return False
if node == n: return True
@sahid
sahid / gist:4972875
Created February 17, 2013 19:08
Rotate a Matrix N*N to 90 degrees
def rotate(M):
S = len(M)
out = [[0] * S for x in xrange(S)] # create the output matrix
for i in xrange(S):
for j in xrange(S):
out[i][j] = M[S-j-1][i]
return out
@sahid
sahid / gist:4972687
Created February 17, 2013 18:34
Square Root in Python by Heron/Newton
def sqrt(A):
x = A # the floor
while x**2 - A > 0.0001: # Approch the value by a precision
x = (x + A/x) / 2
return x
@sahid
sahid / gist:4972596
Last active December 13, 2015 20:48
Insertion Sort in Python
def isort(A):
if len(A) <= 1: return A
i = 1
while i < len(A):
k = A[i]
j = i - 1
while j >= 0 and A[j] > k:
A[j+1] = A[j]
j -= 1
A[j+1] = k
@sahid
sahid / gist:4086369
Created November 16, 2012 10:50
java.lang.ClassCastException
15-Nov-2012 18:31:26 org.apache.solr.common.SolrException log
SEVERE: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lorg.apache.solr.common.util.ConcurrentLRUCache$CacheEntry;
at org.apache.solr.common.util.ConcurrentLRUCache$PQueue.myInsertWithOverflow(ConcurrentLRUCache.java:377)
at org.apache.solr.common.util.ConcurrentLRUCache.markAndSweep(ConcurrentLRUCache.java:329)
at org.apache.solr.common.util.ConcurrentLRUCache.put(ConcurrentLRUCache.java:144)
at org.apache.solr.search.FastLRUCache.put(FastLRUCache.java:131)
at org.apache.solr.search.SolrIndexSearcher.getDocSet(SolrIndexSearcher.java:573)
at org.apache.solr.search.SolrIndexSearcher.getDocSet(SolrIndexSearcher.java:641)
at org.apache.solr.search.SolrIndexSearcher.getDocListNC(SolrIndexSearcher.java:1109)
at org.apache.solr.search.SolrIndexSearcher.getDocListC(SolrIndexSearcher.java:1090)
<class 'google.appengine.runtime.DeadlineExceededError'>:
Traceback (most recent call last):
File "/base/data/home/apps/devel-inchallah/prod.353938252700001385/main.py", line 89, in <module>
main()
File "/base/data/home/apps/devel-inchallah/prod.353938252700001385/main.py", line 31, in real_main
util.run_wsgi_app (WSGIHandler ())
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
@sahid
sahid / util.py
Created September 7, 2011 10:14
A simple way to generate a checksum of a db.Model
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Sahid Orentino Ferdjaoui"
__license__ = "Apache License, Version 2.0"
"""A simple way to generate a checksum from an instance of db.Model.
Note:
These function run without dependences.
@sahid
sahid / hook_indexes.py
Created September 6, 2011 09:09
A simple way to log all indexes used by the application
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Sahid Orentino Ferdjaoui"
__license__ = "Apache License, Version 2.0"
"""A simple way to log in the datastore all indexes used by your app
This module has 2 function:
- The first is the hook, it run like a middleware with django.
- The second is a view that simply the generation of the index.yaml file.
@sahid
sahid / hook.py
Created July 5, 2011 10:39
A simple way to log all of indexes used by the application
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2011 Sahid Orentino Ferdjaoui
import logging
import hashlib
from google.appengine.api import memcache
from google.appengine.api import apiproxy_stub_map