Skip to content

Instantly share code, notes, and snippets.

View pbalduino's full-sized avatar

Plínio Balduino pbalduino

View GitHub Profile
// Primeiro eu crio uma variável chamada nome
// depois abro uma caixa pedindo o nome usando 'prompt'
// e jogo o que você digitar na variável nome
var nome = prompt("Qual o seu nome?");
// A função confirm vai me retornar true se você clicar em OK
// e vai retornar false se você clicar em cancelar ou apertar ESC
if(confirm("Seu nome é " + nome + "?")) {
// Se você apertou OK, o alert mostra uma caixa
// com um botão OK exibindo a mensagem Olá e o seu nome
@pbalduino
pbalduino / euler04-2.clj
Created April 26, 2013 01:11
Trying to solve the Euler #04 The sequence way
(ns euler04-2)
(defn peel
"Peel a string like a banana, removing first and last character"
[str]
(butlast (rest str)))
(defn palindrome?
"Returns true or false if name is a palindrome.
This recursive approach is slightly faster than compare with its reverse."
@pbalduino
pbalduino / server-core.clj
Last active December 16, 2015 16:40
Servidor TCP básico. Permite várias conexões simultâneas, mas os usuários não se conversam.
(ns server.core)
(import [java.net ServerSocket])
(import [java.io DataInputStream
DataOutputStream])
(defn- debug [message]
(println (str "[" (.getName (Thread/currentThread)) "] " message)))
(defn- start-server
@pbalduino
pbalduino / migration1.txt
Last active December 16, 2015 18:20
Criando um arquivo migration. O símbolo de $ indica que estou no prompt do sistema operacional e você não deve digitá-lo,
rails g migration ChangeNomeSizeInDepartamento
invoke active_record
create db/migrate/20130428143550_change_nome_size_in_departamento.rb
@pbalduino
pbalduino / 20130428143550_change_nome_size_in_departamento.rb
Created April 28, 2013 14:41
E esse é o arquivo de migration que altera o nome
class ChangeNomeSizeInDepartamento < ActiveRecord::Migration
def up
change_column(:departamentos, :nome, :string, :limit => 60, :null => false)
end
def down
end
end
@pbalduino
pbalduino / migration3.txt
Created April 28, 2013 14:44
Saída do comando rake db:migrate
rake db:migrate
== ChangeNomeSizeInDepartamento: migrating ===================================
-- change_column(:departamentos, :nome, :string, {:limit=>60, :null=>false})
-> 0.0445s
== ChangeNomeSizeInDepartamento: migrated (0.0447s) ==========================
# Primeiro você criou um model chamado Contato
rails g model contato nome:string{60} endereco:string{100}
invoke active_record
create db/migrate/20130428145000_create_contatos.rb
create app/models/contato.rb
invoke test_unit
create test/unit/contato_test.rb
create test/fixtures/contatos.yml
class AddFieldsToContato < ActiveRecord::Migration
def change
change_table :contatos do |t|
t.string :cidade , :limit => 60
t.string :cep , :limit => 8
t.string :telefone, :limit => 20
end
end
end
@pbalduino
pbalduino / group-by.clj
Last active December 16, 2015 20:29
Group by
(group-by even? (range 1 11))
; {false [1 3 5 7 9], true [2 4 6 8 10]}
(group-by #(= (mod % 3) 0) (range 1 15))
; {false [1 2 4 5 7 8 10 11 13 14], true [3 6 9 12]}
(group-by #(mod % 3) (range 1 15))
; {1 [1 4 7 10 13], 2 [2 5 8 11 14], 0 [3 6 9 12]}
@pbalduino
pbalduino / sudoku.txt
Last active December 16, 2015 22:19
Sudoku solver
Inicio
+---------+---------+---------+
| 6 | 3 | 8 4 |
| | | |
| 5 3 7 | 9 | |
| | | |
| 4 | 6 | 3 7 |
+---------+---------+---------+
| 9 | 5 1 | 2 3 8 |
| | | |