Skip to content

Instantly share code, notes, and snippets.

View hmenn's full-sized avatar
🎧
Focusing

Hasan MEN hmenn

🎧
Focusing
View GitHub Profile
@hmenn
hmenn / my-length.cl
Created October 15, 2016 11:45
lisp-examples
; find list length
(defun my-list-length (l)
(cond ((endp l) 0)
(t (1+ (MY-LENGTH (rest l))))
))
@hmenn
hmenn / count-sublists.cl
Created October 15, 2016 11:47
Count sublists number and return
(defun COUNT-SUBLISTS (l)
(cond
((endp l) 0)
((listp (first l))
(+ 1 (COUNT-SUBLISTS (rest l))))
(T (COUNT-SUBLISTS (rest l)))
))
@hmenn
hmenn / remove-first-occ.cl
Created October 15, 2016 11:49
remove first occurence of element from list
;item -> item to remove, can be atom or list
;l -> list to remove from
(defun REMOVE-FIRST (item l)
(cond
((endp l) nil) ; end of list
((equal item (first l)) (rest l)) ; if find, return rest
(t (cons (first l) (REMOVE-FIRST item (rest l)))) ; cons head item and list
))
@hmenn
hmenn / my-all-occ-remove.cl
Last active October 15, 2016 13:09
remove all occurance of element from list
(defun my-all-occ-remove (item l)
(cond
((endp l) nil)
((equal item (first l)) (my-all-occ-remove item (rest l))) ; remove item
(t (cons (first l) (my-all-occ-remove item (rest l))))
))
@hmenn
hmenn / replace-first.cl
Created October 15, 2016 13:29
replaces first occurence with new value and returns array
(defun replace-first (i1 i2 l)
(cond
((endp l) nil) ; end of list
((equal i1 (first l)) (cons i2 (rest l))) ; found. combine cells with cons
(t (cons (first l) (replace-first i1 i2 (rest l)))) ; recursive rest call
)
)
@hmenn
hmenn / count-atoms.cl
Created October 15, 2016 13:34
counts how many of its top-level expressions are atomic expressions
(defun count-atoms (l)
(cond
((endp l) 0)
((symbolp (first l)) (1+ (count-atoms (rest l)))) ; found an atom
(t (count-atoms (rest l))) ; recursive call
))
@hmenn
hmenn / bubble-sort.c
Created October 29, 2016 12:08
Bubble sort on array
#include <stdio.h>
// Best case : Omega(n)
// worst case : O(n^2)
// awerage : Theta(n^2)
void bubbleSort(int arr[],int size);
void swap(int arr[],int x,int y);
int main(){
@hmenn
hmenn / quickSelect.cpp
Created November 11, 2016 08:15
Quick Select with Lomuto Partition
// _
// | |
// | |__ _ __ ___ ___ _ __ _ __
// | '_ \| '_ ` _ \ / _ \ '_ \| '_ \
// | | | | | | | | | __/ | | | | | |
// |_| |_|_| |_| |_|\___|_| |_|_| |_|
#include <iostream>
using namespace std;
@hmenn
hmenn / findEqualIndexVal.cpp
Last active December 4, 2016 13:51
This algorithm finds element in an ordered array which is i=A[i]
#include <stdlib.h>
#include <stdio.h>
int findEqualIndex(int arr[],int start,int end);
int main(){
int arr[6]={-10,-5,-1,0,4,5};
int arr2[5]={0,2,5,8,7};
int res = findEqualIndex(arr,0,5);
@hmenn
hmenn / .bashrc
Last active January 22, 2017 22:08
My last bashrc file
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac