Skip to content

Instantly share code, notes, and snippets.

View ihercowitz's full-sized avatar

Igor Hercowitz ihercowitz

View GitHub Profile
@ihercowitz
ihercowitz / golsbach.js
Created February 22, 2012 00:48
Goldbach's conjecture
/* Goldbach's conjecture in JavaScript
*
* author: Igor Hercowitz
*/
function isPrime(n) {
if (n % 2 === 0) return false;
var sqrtn = Math.sqrt(n)+1;
@ihercowitz
ihercowitz / problem.js
Created February 23, 2012 00:35
The 3n + 1 Problem
/*
* The 3n + 1 Problem
*Consider the following algorithm to generate a sequence of numbers. Start with an
*integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this
*process with the new value of n, terminating when n = 1. For example, the following
*sequence of numbers will be generated for n = 22:
*22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
*It is conjectured (but not yet proven) that this algorithm will terminate at n = 1 for
*every integer n. Still, the conjecture holds for all integers up to at least 1, 000, 000.
*For an input n, the cycle-length of n is the number of numbers generated up to and
@ihercowitz
ihercowitz / image_resize.py
Created October 23, 2010 20:19
Python Script to resize all the images, on a given directory, to a 1024x768 JPEG format.
#!/usr/bin/env python
import Image
import os, sys
def resizeImage(infile, dir, output_dir="", size=(1024,768)):
outfile = os.path.splitext(infile)[0]+"_resized"
extension = os.path.splitext(infile)[1]
if extension.lower()!= ".jpg":
@ihercowitz
ihercowitz / pdfreducer.sh
Created August 6, 2021 15:48
Reduce PDF Size
LEVEL=1.3&& RES=92 && gs -sDEVICE=pdfwrite -dCompatibilityLevel=$LEVEL -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -dColorImageDownsampleType=/Bicubic \ -dColorImageResolution=$RES \ -dGrayImageDownsampleType=/Bicubic \ -dGrayImageResolution=$RES \ -dMonoImageDownsampleType=/Subsample \ -dMonoImageResolution=$RES \ -sOutputFile=output.pdf input.pdf
@ihercowitz
ihercowitz / gist:83fff0d1e6a96ee991723d2b7fc4ca21
Created November 16, 2020 14:00
Some Tips to do a better code or points to have in mind when do a cross code-review
Some Tips to do a better code or points to have in mind when do a cross code-review:
- Classes and methods small -> Classes should represent simple entities. Methods should have one job. This is crucial for testing, refactoring, understanding, and maintaining.
- Make class fields private, and provide getters when necessary -> This is necessary to encapsulate the logic in a class, so callers are not dependent on the class implementation.
- Immutable classes -> Usage of setters are not a good practice. Using this on the classes, will let the data mutable, and the results can be catastrophic. Also, use final on variables (this will make them immutable).
- Avoid large blocks of code -> Use whitespace to separate tiny, logical chunks of code. (Do not be afraid of whitespace. It is your friend.) Use inline documentation to describe unclear bits of code, but better yet, restructure your code and rename methods/variables until the code is clear.
;; Bingo Real recriado em Lisp
;; - Roda direto no Emacs -
;;
;; Como rodar:
;; - Abrir o arquivo no Emacs
;; - Alt-x
;; - Digitar eval-current-buffer
;; - Os assuntos serao gerados na area de mensagens apertando Control-F2
(require 'cl)
@ihercowitz
ihercowitz / xmlbuilder.py
Created August 22, 2012 12:37
Building a XML Tree as Easy as ABC in Python
'''
****************************************************************************************
Building a XML Tree as Easy as ABC in Python
This implementation requires lxml
Created by Igor Hercowitz
2012-07-04
******************************************************************************************
'''
@ihercowitz
ihercowitz / goldbach_recursion.js
Created February 22, 2012 02:14
Goldbach's conjecture in JavaScript - using Recursion
/* Goldbach's conjecture in JavaScript - using Recursion
*
* author: Igor Hercowitz
*/
function isPrime(n) {
if (n % 2 === 0) return false;
var sqrtn = Math.sqrt(n)+1;
/* Demonstrates the Goldbach's conjecture - Every even integer greater than 2 can be expressed as the sum of two primes
*
* author: Igor Hercowitz
*
* usage: java Goldbach <number>
* output: the sum of the primes list
*
* ex:
* > java Goldbach 100
* 100 can be writen as: [97+3, 89+11, 83+17, 71+29, 59+41, 53+47]
@ihercowitz
ihercowitz / std-militar.clj
Last active July 22, 2016 15:51
Convert Standard Time (AM/PM) to Militar Time (24h)
(defmulti std-to-militar (fn [hour time-info] (keyword time-info)))
(defmethod std-to-militar :AM [hour _]
(format "%02d" (if (= hour 12) 0 hour)))
(defmethod std-to-militar :PM [hour _]
(if (= hour 12) hour (+ hour 12)))
(defn convert-time [hour]
(let [[hour minute seconds time-info] (rest (re-find #"(\d+):(\d+):(\d+)(\w+)" hour))]