Skip to content

Instantly share code, notes, and snippets.

View a-eid's full-sized avatar
🎯
Focusing

Ahmed Eid a-eid

🎯
Focusing
View GitHub Profile
@a-eid
a-eid / sq.js
Created March 21, 2017 03:24
newton method sqrt recursive
// square root of x in range of e .. g could be much better guessed .
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
@a-eid
a-eid / fac.js
Created March 22, 2017 09:47
implementation of tail recursive and no tail recursive factorial
function fac(n){
if( n == 0) return 1
return n * fac(n-1)
}
function fac2(n){
return (function tmp( n , a){
if(n == 0) return a
return tmp(n-1 , a * n)
})(n , 1)
@a-eid
a-eid / counter.js
Created March 30, 2017 17:54
simple counter example ....
import React from 'react';
import {render} from 'react-dom';
import {createStore} from 'redux'
const reducer = (state = 0 , action) =>
(action.type == 'inc' || action.type == 'dec') ? ((action.type == 'inc' ? state + 1 : state - 1) ) : state
const store = createStore(reducer)
const Counter = ({value , onInc , onDec}) => (
@a-eid
a-eid / bsearch.js
Created April 2, 2017 11:36
simple binary search example ...
const bsearch = (arr , start , end , el) => {
if(start > end) return -1
let mid = Math.round((start + end) / 2)
if(mid == el) return mid
else if ( mid < el ) return bsearch(arr , mid + 1 , end , el)
else if ( mid > el ) return bsearch(arr , start , mid - 1 , el)
}
@a-eid
a-eid / toggle_touchpad.sh
Created April 6, 2017 19:44
command toggle touchpad ubuntu
#!/bin/bash
read TPdevice <<< $( xinput | sed -nre '/TouchPad/s/.*id=([0-9]*).*/\1/p' )
state=$( xinput list-props "$TPdevice" | grep "Device Enabled" | grep -o "[01]$" )
if [ "$state" -eq '1' ];then
xinput --disable "$TPdevice"
else
xinput --enable "$TPdevice"
fi
function setObject(str , value){
var arr = str.split('.')
var init = window
for(var i = 0 ; i < arr.length-1 ; i++){
if(!init[arr[i]]) init[arr[i]] = {}
init = init[[arr[i]]]
}
init[arr[arr.length-1]] = value // trick ..
}

##what are generators##

  • They're pausable functions, pausable iterable functions, to be more precise
  • They're defined with the *
  • every time you yield a value, the function pauses until .next(modifiedYieldValue) is called
var myGen = function*() {
  var one = yield 1;
  var two = yield 2;
  var three = yield 3;
 console.log(one, two, three);
function bubble_sort(arr){
for(let i = 0 , n = arr.length ; i < n - 1 ; n -= 1){
for(let ii = i , j = i + 1 ; j < n ; j += 1 , ii += 1){
if(arr[ii] > arr[j]){
let temp = arr[ii]
arr[ii] = arr[j]
arr[j] = temp
}
}
}
import sys
from urllib.request import urlopen
def fetch_words(url):
words = []
with urlopen(url) as f:
for line in f:
for word in line.decode('utf-8').split():
words.append(word)
return words
<html><!-- #BeginTemplate "/Templates/master.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>Real Estate Puerto Rico ,Bienes Raices en Puerto Rico Clasificados Alquiler
Online</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="description" CONTENT="Real Estate Puerto Rico ,Bienes Raices en Puerto Rico Clasificados Alquiler Online">
<META NAME="keywords" CONTENT="Real Estate Puerto Rico , Alquiler Bienes Raices en Puerto Rico Clasificados Online, bienes raices puerto rico, puerto rico bienes raices, puerto rico real estate,real, estate, alquiler, rent, real estate, bienes raices, casa, clasificados, classifieds, puerto rico, online, homes, apartamento, house">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">