Skip to content

Instantly share code, notes, and snippets.

@elleryq
elleryq / tokenize.cpp
Created July 8, 2014 06:44
Split string according to space and convert each token to uppercase.
#include <sstream>
#include <iostream>
#include <string>
#include <algorithm>
std::string toupper(std::string& s) {
for(std::string::iterator c=s.begin(); c!=s.end(); c++) {
*c = std::toupper(*c);
}
return s;
@elleryq
elleryq / checksum.c
Created July 8, 2014 06:48
For each 512 bytes in the input binary file, calculate the 16-bit checksum and write the 512 bytes with the checksum as the 512th byte to the output binary file. At the end of the file, pad bytes might need to be added if the remaining byes in the input binary file is less then 512 bytes. If present, each pad byte shall be set to 0xff.
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 512
int doPadding(char* buffer, int startOffset) {
int i;
for(i = startOffset; i<BUFFER_SIZE; ++i) {
*(buffer+i) = 0xff;
@elleryq
elleryq / tokenize.c
Created July 8, 2014 06:54
Split string according to space and convert each token to uppercase.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* strupper(char* s) {
char* t = strdup(s);
char* p = t;
while( *p ) {
*p = toupper(*p);
p++;
@elleryq
elleryq / padding.c
Last active August 29, 2015 14:03
padding example (in 64-bit machine) which is from http://www.catb.org/esr/structure-packing/
// You wrote:
struct foo1 {
char *p;
char c;
long x;
};
// Actually:
struct foo1 {
char *p; /* 8 bytes */
@elleryq
elleryq / nullable.cs
Created August 11, 2014 02:42
Try C# Nullable. The code can be paste in LINQPad and run. If you want to run in Visual Studio or other IDE, you need to rewrite it.
void Main()
{
String s = null;
Console.WriteLine("s is {0}", s ?? "Null");
Animal animal = null;
Console.WriteLine("Animal is {0}", animal ?? new Animal());
Nullable<int> i = null;
Console.WriteLine("i is {0}", i ?? default(int));
@elleryq
elleryq / filter_var.php
Created September 24, 2014 02:16
用 filter_var 檢查字串是否符合 IPv4 格式,以及濾掉不合法字元。
<?php
echo "Hello\n";
$successedString = "192.168.11.1";
$valid = filter_var($successedString, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE);
echo "\n1.'" . $successedString . "'" . var_dump($valid) . "\n";
if(!$valid) {
echo "IP is not valid\n";
}
else {
@elleryq
elleryq / get_continuous_items_count.py
Last active August 29, 2015 14:06
將連續元素分組,然後找出出現次數最多的。
from itertools import groupby
from collections import Counter
from pprint import pprint
def getContinuousItemsCount(l):
x = [tuple(g) for k, g in groupby(l)]
tupleCounter = Counter(x)
s = set(x)
d = {}
for t in s:
@elleryq
elleryq / lookup_word.l
Last active August 29, 2015 14:07
lex & yacc 第一章的練習,然後改用 glib 的 GList 來做。
%{
#include <glib.h>
#include <stdlib.h>
/* token recognizer */
enum {
LOOKUP = 0, /* default is lookup, not add */
VERB,
ADJ,
ADV,
NOUN,
@elleryq
elleryq / bingo_table.py
Last active February 8, 2023 10:27
產生 Bingo 表格的程式,裏面的數字是亂數,預設一頁產生 12 個表格,結果會輸出為 PDF。參數是要產生的頁數,輸出檔案會在當前目錄下的 bingo.pdf
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Dependencies:
# * wkhtmltopdf - Convert HTML to PDF
# * pdftk - Merge multiple PDFs to single PDF
# * jinja2 - Template engine
import sys
import os
from itertools import permutations, izip_longest
import random

我是用 virtualenv,不用也可以,就忽略前3個步驟即可,virtualenv 好處是可以不用搞亂系統環境 (不安裝到 /usr, /usr/local)。

步驟:

  1. virtualenv kaggleaux_env
  2. cd kaggleaux_env
  3. source bin/activate
  4. wget https://bootstrap.pypa.io/get-pip.py
  5. python get-pip.py
  6. sudo apt-get install libblas-dev liblapack-dev gfortran :這是為了 scipy 而裝的
  7. git clone https://github.com/agconti/KaggleAux.git