Sunday, November 17, 2013

Methods calculate, add, subtract

CODE:
def calculate(*args)
  if args[-1].is_a?(Hash)
    if args[-1][:add]
      add(*args[0..-2])
    elsif args[-1][:subtract]
      subtract(*args[0..-2])
    end
  else
    add(*args)
  end
end

def add(*numbers)
  numbers.inject(0) { |sum, num| sum + num }
end

def subtract(*numbers)
  numbers.inject { |dif, num| dif - num }
end

TESTS from rubymonk
invoking add(4, 5) returns 9 ✔
invoking add(-10, 2, 3) returns -5 ✔
invoking add(0, 0, 0, 0) returns 0 ✔
invoking subtract(4, 5) returns -1 ✔
invoking subtract(-10, 2, 3) returns -15 ✔
invoking subtract(0, 0, 0, 0, -10) returns 10 ✔
defaults to addtion when no option is specified ✔
invoking calculate(4, 5, add: true) returns 9 ✔
invoking calculate(-10, 2, 3, add: true) returns -5 ✔
invoking calculate(0, 0, 0, 0, add: true) returns 0 ✔
invoking calculate(4, 5, subtract: true) returns -1 ✔
invoking calculate(-10, 2, 3, subtract: true) returns -15 ✔
invoking calculate(0, 0, 0, 0, -10, subtract: true) returns 10 ✔

Tuesday, September 17, 2013

Update Ubuntu from 10.10 to 12.04

copy /etc/apt/sources.list from 12.04 to 10.10 and then 'sudo apt-get update'

# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ dists/precise/main/binary-i386/

# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ dists/precise/restricted/binary-i386/
# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ precise main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ro.archive.ubuntu.com/ubuntu/ precise main restricted
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ro.archive.ubuntu.com/ubuntu/ precise-updates main restricted
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://ro.archive.ubuntu.com/ubuntu/ precise universe
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise universe
deb http://ro.archive.ubuntu.com/ubuntu/ precise-updates universe
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://ro.archive.ubuntu.com/ubuntu/ precise multiverse
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise multiverse
deb http://ro.archive.ubuntu.com/ubuntu/ precise-updates multiverse
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://ro.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://ro.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu precise-security main restricted
deb-src http://security.ubuntu.com/ubuntu precise-security main restricted
deb http://security.ubuntu.com/ubuntu precise-security universe
deb-src http://security.ubuntu.com/ubuntu precise-security universe
deb http://security.ubuntu.com/ubuntu precise-security multiverse
deb-src http://security.ubuntu.com/ubuntu precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
deb http://extras.ubuntu.com/ubuntu precise main
deb-src http://extras.ubuntu.com/ubuntu precise main
deb http://archive.canonical.com/ precise partner
deb-src http://archive.canonical.com/ precise partner

Tuesday, September 10, 2013

Install Firefox, Chrome, Opera, Safari Internet Browsers in Ubuntu

http://www.guguncube.com/694/ubuntu-installation-of-firefox-chrome-opera-safari-internet-browsers-in-ubuntu-12-04-12-10-13-10

Friday, August 30, 2013

make Ruby 1.9 to be default


1. sudo apt-get update
2. sudo apt-get install ruby1.9.1 ruby1.9.1-dev rubygems1.9.1 irb1.9.1 ri1.9.1 rdoc1.9.1 build-essential libopenssl-ruby1.9.1 libssl-dev zlib1g-dev
3. sudo update-alternatives --install /usr/bin/ruby ruby /usr/bin/ruby1.9.1 400 --slave /usr/share/man/man1/ruby.1.gz ruby.1.gz /usr/share/man/man1/ruby1.9.1.1.gz --slave /usr/bin/ri ri /usr/bin/ri1.9.1 --slave   /usr/bin/irb irb /usr/bin/irb1.9.1 --slave   /usr/bin/rdoc rdoc /usr/bin/rdoc1.9.1
4. sudo update-alternatives --config ruby (choose 1st variant /usr/bin/ruby1.9.1)
5. sudo update-alternatives --config gem (choose last variant with /usr/bin/gem1.9.1)

If you try 'ruby --version' will have the ruby1.9.1 version

Tuesday, April 9, 2013

Count the occurrences of a string per line in a file


cat exampleFile
text 1 text
text text 4 text 56
2
4
test
345
87 text

Count the occurrences of 'text' in exampleFile per line
awk -F "text" '{print NF-1}' exampleFile
2
3
0
0
0
0
1

Friday, March 29, 2013

Sort,uniq (string, int) columns in csv files


cat file

text1,5
text4,3
text3,1
text1,4
text7,2
text3,7
text2,9


1. less file | sort -u -t, -k1,1 - sort the first column and get unique texts
text1,5
text2,9
text3,1
text4,3
text7,2

2. less file | sort -u -t, -k1,1 | sort -t, -k2 -nr - get unique texts on first column and reverse order for the second column using -r option
text2,9
text1,5
text4,3
text7,2
text3,1