Skip to content

Instantly share code, notes, and snippets.

@etandel
Last active October 8, 2015 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save etandel/3370778 to your computer and use it in GitHub Desktop.
Save etandel/3370778 to your computer and use it in GitHub Desktop.
Ajudas misc
nhomens = 0
nmulheres = 0
soma_mulheres = 0
max_altura = 0
min_altura = float('inf')
npessoas = int(raw_input("Quantas pessoas no total?"))
for i in xrange(npessoas):
sexo = raw_input("F ou M?")
altura = int(raw_input("Digite a altura em centimetros."))
if sexo.upper() == 'F':
soma_mulheres += altura
nmulheres += 1
else:
nhomens += 1
if altura > max_altura:
max_altura = altura
if altura < min_altura:
min_altura = altura
print "A maior altura:", max_altura
print "A menor altura:", min_altura
print "Media mulheres:", float(soma_mulheres) / nmulheres
print "Numero de homens:", nhomens
#include <stdio.h>
#include <time.h>
#include <limits.h>

#define NUM_MAX LONG_MAX
typedef long int num_t;

int is_even_loop(num_t num){
    while(num>0)
        num -=2;
    return num==0 ? 1:0;
}

int is_even_bitwise(num_t num){
    return !(((num_t)1) & num);
}

int main(void){
    float start_t, dt;
    int even;
    num_t num = NUM_MAX;

    puts("Using loop:");
    start_t = clock();
    even = is_even_loop(num);
    dt = (clock() - start_t)/CLOCKS_PER_SEC;
    printf("%d is %s; total time = %f\n", num, even?"even":"odd", dt);

    puts("Using bitwise operations:");
    start_t = clock();
    even = is_even_bitwise(num);
    dt = (clock() - start_t)/CLOCKS_PER_SEC;
    printf("%d is %s; total time = %f\n", num, even?"even":"odd", dt);
    return 0;
}

Resultados na minha máquina (Core 2 Duo, Arch Linux kernel 3.5.3-1 32-bits):

Using loop:
2147483646 is even; total time = 3.330000
Using bitwise operations:
2147483646 is even; total time = 0.000000
t1 = {}
print(t1.chave) --> nil
t2 = {chave = 'valor'}
print(t2.chave) --> valor
t2.__index = t2
setmetatable(t1, t2)
-- o campo 'chave' nao existe em t1, entao lua vai ver se ele possui
-- uma metatabela com o campo '__index'. Nesse caso, o campo eh uma tabela, t2,
-- em que sera procurado o campo 'chave'.
print(t1.chave) --> 'valor'
-- rawget ignora metatabelas, mostrando que de fato 'chave' nao existe em t1
print(rawget(t1, 'chave')) --> nil
print'\n---------------------------------\n'
Classe = {}
-- faz com que valores nao existentes nas instancias sejam procurados na classe
Classe.__index = Classe
function Classe:new(nome)
local o = {nome = nome}
setmetatable(o, Classe)
return o
end
function Classe:hello()
print("Oi, meu nome: " .. self.nome)
end
instancia = Classe:new('Luma')
instancia:hello() --> Oi, meu nome: Luma
outrainstancia = Classe:new('Elias')
outrainstancia:hello() --> Oi, meu nome: Elias
function new_country(name, g,s,b)
return {
name = name,
gold = g,
silver = s,
bronze = b,
}
end
function compare(p1, p2)
for _,medal in ipairs({'gold', 'silver', 'bronze'}) do
if p1[medal] > p2[medal] then
return true
end
end
return false
end
countries = {
new_country('arg', 3,1,1),
new_country('bol', 3,1,0),
new_country('bra', 3,2,1),
}
table.sort(countries, compare)
for _,c in ipairs(countries) do
print(c.name)
end
#include <stdlib.h>
#define is_lower(c) (c >= 'a' && c <= 'z')
#define is_upper(c) (c >= 'A' && c <= 'Z')
#define is_digit(c) (d >= '0' && c <= '9')
typedef struct {
char symb[4]; //1 maiuscula, ate 2 minusculas e 1 para o '\0'
int count;
} atom_t;
void next_atom(molec_t molecule, atom_t *a){
char digits[4]; //mais de 999 atomos de um tipo é sacanagem.
int i = 0;
a->symb[i++] = next_upper(molecule);
do {
char c = next_lower(molecule);
if (is_lower(c))
a->symb[i++] = c
} while (i<3 && is_lower(c);
a->symb[i++] = '\0';
i = 0;
do {
char d = next_digit(molecule);
if (is_digit(d))
digits[i++] = d;
} while (i<3 && is_digit(d));
digits[i++] = '\0';
a->count = atoi(digits); //atoi fica na stdlib
if (!a->count) //caso em que so existe 1 atomo, nao há digitos.
a->count = 1;
}
#include <stdio.h>
struct s {
char c;
int i;
};
int main (void){
struct s a={'a', 1}, b={'b', 2}, tmp;
tmp = a;
a = b;
b = tmp;
printf("valores de a: %c , %d\n", a.c, a.i);
printf("valores de b: %c , %d\n", b.c, b.i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment