Drowze
Drowze
Rafael G
7 Snippets  (112nd place)
Published
1 Channel
Created
1 Channel
Following
Jan 23, 2016
Last Visit
Jan 23, 2016
Registered
3 points  (13750th place)
Reputation
Serious Code Generator
Junior Popular Coder
Junior Autobiographer
Senior Autobiographer
Junior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by Drowze

public by Rafael G created Dec 12, 2015  10297  1  4  1

WIP: script to auto-correct almost any program (the outputs should not contain "\n\n" though)

WIP: script to auto-correct almost any program (the outputs should not contain "\n\n" though): examinator.rb
## Converting line endings to unix:
inputs_file = File.open('./inputs.txt', 'a+')
outputs_file = File.open('./outputs.txt', 'a+')

input_file_contents = inputs_file.read.gsub(/\r\n?/,"\n")
inputs_file.truncate(0)
inputs_file.print input_file_contents

outputs_file_contents = outputs_file.read.gsub(/\r\n?/,"\n")
outputs_file.truncate(0)
outputs_file.print outputs_file_contents

inputs_file.close
outputs_file.close

#  Getting each input and each output into an array the idea is, basically, 
# separate them each time we find an empty line ("\n").
#  Should be noted though, that since the expected output should contain an
# "\n" at its ending, so should each element on our array.

inputs_file = File.open('./inputs.txt', 'r')
outputs_file = File.open('./outputs.txt', 'r')

inputs = []
outputs = []

loop do
  inputs_contents = inputs_file.gets('')
  outputs_contents = outputs_file.gets('')
  if inputs_contents.nil? || outputs_contents.nil?
    break
  else
    inputs_contents.gsub!("\n\n", "\n")
    outputs_contents.gsub!("\n\n", "\n")
    inputs << inputs_contents
    outputs << outputs_contents
  end
end

# To continue. Next step: get user's output.
# Little snippet on how to get terminal's output below:

test = __FILE__
puts `ls #{test}`.class # It's a string.


;

public by Rafael G created Dec 4, 2015  3477  3  3  0

#sort #sorting #puc apc-b 2016

#sort #sorting #puc apc-b 2016: mergesort.c
#include <stdio.h>

void merge(int v[], int inicio, int meio, int fim) {
  int aux[100];
  int aux_inicio = inicio, aux_meio = meio + 1, k=0;
  while(aux_inicio <= meio && aux_meio <= fim)
    if(v[aux_inicio] <= v[aux_meio])
      aux[k++] = v[aux_inicio++];
    else
      aux[k++] = v[aux_meio++];
      
  for(aux_inicio = aux_inicio; aux_inicio <= meio; aux_inicio++)
    aux[k++] = v[aux_inicio];
    
  for(aux_meio = aux_meio; aux_meio <= fim; aux_meio++)
    aux[k++] = v[aux_meio];
    
  k=0;
  
  for(aux_inicio = inicio; aux_inicio <= fim; aux_inicio++) //copia aux para v
    v[aux_inicio] = aux[k++];
}

void merge_sort(int v[], int inicio, int fim) {
  int meio = (inicio + fim) / 2;
  if(inicio < fim) {
      merge_sort(v, inicio, meio);
      merge_sort(v, meio + 1, fim);
      merge(v, inicio, meio, fim);
    }
}

int main (void) {
  int var[] = {0, 6, 4, 2, 16, 22, 3, 9};
  merge_sort(var, 0, 7);
  int i;
  for (i = 0; i < 8; i++)
    printf("%d ", var[i]);
  return 0;
}

;

public by Drowze created Dec 2, 2015  12607  0  4  0

mim ajuda

mim ajuda: mim_ajuda.rb
require 'matrix'
class Matrix
  def []=(i, j, x)
    @rows[i][j] = x
  end
end

class String # Only works for single letters; could be extended to uppercase letters too
  def step(c, delta=1)
    if c.ord + delta > 122 then
      return [c.ord + delta - 26].pack 'U'
    elsif c.ord + delta < 97 then
      return [c.ord + delta + 26].pack 'U'
    else
      return [c.ord + delta].pack 'U'
    end
  end

  def increment(c)
    return nil if self.size > 1
    c %= 26
    step self, c
  end

  def decrement(c)
    return nil if self.size > 1
    c %= 26
    step self, -c
  end
end

class Board
  attr_reader :cell_pairs
  MOVES = {
    'nw' => [-1, 0],
    'ne' => [-1, +1],
    'sw' => [+1, -1],
    'se' => [+1, 0]
  }
  CELL_CODES = Matrix[
    [0,0,0,0,0,1,3,3,3,2],
     [0,0,0,0,5,7,7,7,7,2],
      [0,0,0,5,7,7,7,7,7,2],
       [0,0,5,7,7,7,7,7,7,2],
        [0,5,7,7,7,7,7,7,7,2],
         [4,7,7,7,7,7,7,7,7,0],
          [4,7,7,7,7,7,7,7,0,0],
           [4,7,7,7,7,7,7,0,0,0],
            [4,7,7,7,7,7,0,0,0,0],
             [4,7,7,7,7,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0]
  ]

  CELL_PAIRS = module Cell_pairs # "Function" to prepare the cell_pairs
    cell_pairs = Hash.new
    start_letter = 'a'
    increment = 0
    checker = 0
    CELL_CODES.each_with_index do |element, row, col|
      if row <= 5
        if checker != row then # I get here everytime it goes to another row
          increment = -row + 5
          checker = row
        end
        if(element == 7) then
          cell_pairs["#{row} #{col}"] = "#{row.to_s}#{start_letter.increment(increment)}"
          increment += 2
        end
      else
        if checker != row then # I get here everytime it goes to another row
          increment = row - 5
          checker = row
        end
        if(element == 7) then
          cell_pairs["#{row} #{col}"] = "#{row.to_s}#{start_letter.increment(increment)}"
          increment += 2
        end
      end
    end
    cell_pairs
  end



  def self.prepare_board positions
    seq = [' ',1,2,3,4,5,6,7,8,9,' ']
    counter = 0
    indentation = 0
    ret = ''

    CELL_CODES.to_a.map.with_index do |m, i|
      if i != 10 then
        ret += seq[counter].to_s + ' ' * indentation 
        m.each_with_index do |n, j| 
          n == 7 ? ret += ("#{positions[i,j]} ") : ret += ('  ')  # piece occupying the cell
          [4,5,6,7].include?(n) ? ret += ('| ') : ret += ('  ') 
        end
        ret += "\n" + ' ' * indentation unless i == CELL_CODES.to_a.map.size-1
        m.each_with_index do |n|
          [2,3,6,7].include?(n) ? ret += ('\\ ') : ret += ('  ') 
          [1,3,5,7].include?(n) ? ret += ('/ ') : ret += ('  ') 
        end
        ret += "\n" 
        counter+=1 
        indentation+=2
      end
    end
    ret += ' ' * 15 + 'a b c d e f g h i j k l m n o' + "\n"
    ret
  end
end

print Board::CELL_PAIRS

;

public by Drowze created Nov 11, 2015  2674  0  3  0

Only simple letters for now

Only simple letters for now: caesar.c
#include <stdio.h>

void caesar_cipher(FILE *input, FILE *output, int k){
  int ch;
  k %= 26; //26 indica uma volta completa, logo qualquer k>26 é excessivo
  do{
    ch = fgetc(input);
    if(ch >= 65 && ch <= 90) { //letra maiuscula? (valores de 'A' e 'Z' da tabela ASCII)
      ch += k;
      if(ch > 90) 
        ch -= 26; //se tiver passado do 'Z', subtrai uma volta completa
    }
    else
      if(ch >= 97 && ch <= 122) { //letra minuscula? (valores de 'a' e 'z' da tabela ASCII)
        ch += k;
        if(ch > 122) 
          ch -= 26; //se tiver passado do 'z', subtrai uma volta completa
      }
    if(ch != EOF)
      fputc(ch, output);
  }while(ch != EOF);
}

void caesar_decipher(FILE *input, FILE *output, int k){
  int ch;
  k %= 26; //26 indica uma volta completa, logo qualquer k>26 é excessivo
  do{
    ch = fgetc(input);
    if(ch >= 65 && ch <= 90) { //letra maiuscula? (valores de 'A' e 'Z' da tabela ASCII)
      ch -= k;
      if(ch < 65) 
        ch += 26; //se tiver passado do 'A', soma uma volta completa
    }
    else
      if(ch >= 97 && ch <= 122) { //letra minuscula? (valores de 'a' e 'z' da tabela ASCII)
        ch -= k;
        if(ch < 97) 
          ch += 26; //se tiver passado do 'a', soma uma volta completa
      }
    if(ch != EOF)
    fputc(ch, output);
  }while(ch != EOF);
}

int main(){
  FILE *input = NULL;
  FILE *output = NULL;
  int k = 3;

  input = fopen("teste.txt", "r");
  output = fopen("teste_out.txt", "w");
  if(input == NULL || output == NULL){ //se deu erro
    printf("shit");
    return 1;
  }

  caesar_cipher(input, output, k); //encripta usando a chave k
  //caesar_cipher(input, output, k); //decripta usando a chave k
  
  fclose(input);
  fclose(output);

  return 0;
}

;

public by Drowze created Nov 3, 2015  279255  1  3  0

Compress all files in a folder into individual 7z files

Compress all files in a folder into individual 7z files: Compress_individual_files.txt
FOR %i IN (*.*) DO 7z.exe a -m0=LZMA2 -mmt=x "%~ni.7z" "%i"

where x = number of cores

;