Skip to content

Instantly share code, notes, and snippets.

@varnie
varnie / arrayUsage.rb
Created July 31, 2011 17:11
An intro to Arrays usage in Ruby
require 'benchmark'
NUM_PERSONS = 100_000#10_000_000
class Person < Struct.new(:sex, :age, :height, :index, :money)
end
class Array
def search(options={})
options.delete_if {|k,v| not Person.members.include?(k.to_s)}
@varnie
varnie / Ruby images downloader
Created August 4, 2011 16:09
Three ways of handling images from the net using Ruby
#!/usr/bin/env ruby
require 'open-uri'
require 'pool' #taken fron http://burgestrand.se/code/ruby-thread-pool/
IMG_REGEXP = /\<img[^(src)]*src=(\"|\')((http:\/\/)?[\w\.\/:-]+)\1[^\\>]*\\?\>/
VALID_IMAGE_EXTENSION_REGEXP = /.*\.(gif|tif|png|jpeg|jpg)$/
def image_extension_correct?(image)
not image.nil? and image =~ VALID_IMAGE_EXTENSION_REGEXP
end
require 'thread' #taken from http://burgestrand.se/code/ruby-thread-pool/
class Pool
def initialize(size)
@size=size
@jobs = Queue.new
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current[:id] = i
catch(:exit) do
@varnie
varnie / sentence_challenge.rb
Created August 31, 2011 15:31
Yet another programming challenge to blow up your brains
# coding: utf-8
src = '{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}';
r = Regexp.new(/\{[^\{\}]*\}/)
while (match = r.match(src)) do
match.length.times { |i|
offsets = match.offset(i)
chunk = src[offsets[0],offsets[1]-offsets[0]]
bracelessChunk = chunk[1..-2] #remove leading '{' and trailing '}'
@varnie
varnie / SentenceMixer.java
Created September 3, 2011 13:08
Yet another programming challenge to blow up your mind (Java version)
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Stack;
public class SentenceMixer {
private static class Item {
@varnie
varnie / sentenceMixer.php
Created September 4, 2011 16:14
Yet another programming challenge to blow up your mind (PHP version).
<?php
$src = '{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}';
while (preg_match("/\{([^\{\}]*)\}/", $src, $matches)){
$alt_items = explode("|", $matches[1]);
$item = $alt_items[array_rand($alt_items)];
$src = str_replace($matches[0], $item, $src);
}
@varnie
varnie / dateCheck.rb
Created September 7, 2011 05:49
A regexp for date validation
=begin rdoc
Checks that the supplied date has the correct date format '#YYYY:MM:DD' with the separators both be either - or : or /.
A year cannot start from the zero, while a month and a day can.
Doesn't check that the supplied day is within the specified month's day range. It may be further improved.
=end
def checkDate(aDate)
%r{
(?<year>
(^ #no input before the start
[1-9]\d{3} #a year cannot start from the zero
@varnie
varnie / dateCheckUpdated.rb
Created September 7, 2011 08:11
A function checking the date is valid
=begin rdoc
Checks that the supplied date has the correct date format '#YYYY:MM:DD' with the separators both be either - or : or /.
A year cannot start from the zero, while a month and a day can.
Performs additional checkings that the date is valid.
=end
def checkDateUpdated(aDate)
matched =
%r{
(?<year>
(^ #no input before the start
<?php
function checkBraces($str){
$stack = Array();
for ($i = 0; $i < strlen($str); ++$i) {
$value = $str[$i];
if ($value == '(' || $value == '{'){
array_push($stack, $value);
} else if ($value == ')' || $value == '}'){
if (count($stack) <= 0){
@varnie
varnie / changeTdStyle.html
Created September 8, 2011 11:09
an example of changing a style of td using javascript and DOM
<html>
<head>
<title>getElementById example</title>
<style>
table .c { color: red }
.a.c {color: green}
</style>
<script type="text/javascript">