How to Revert Git Commit Remote

First, There are serveral revert situation: Local:

1
2
3
# this will detach your HEAD, i.e. leave you with no branch checked out.
git checkout 0d1d7fc32
git checkout -b old-state 0d1d7fc32
1
2
3
4
5
6
7
8
9
10
# Don't do it if you have uncommitted work you want to keep
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to
1
2
git add . && git checkout master -f
git add . && git reset --hard HEAD

Remote

1
2
3
git push mathnet +dd61ab32^:master
git reset HEAD^ --hard
git push mathnet -f

.

Comments

Happy Number and Prime

A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers[1]).

More formally, given a number , define a sequence , , … where is the sum of the squares of the digits of . Then n is happy if and only if there exists i such that . If a number is happy, then all members of its sequence are happy; if a number is unhappy, all members of the sequence are unhappy. For example, 19 is happy, as the associated sequence is: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1. The happy numbers below 500 are: 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496

calculation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@dead_list = []
@cache = []
def happy? num
  result = num.to_s.split('').inject(0){|sum,x| sum + x.to_i ** 2}
  return false if @dead_list.include? result
  if result == 1
    @cache = []
    return true
  end
  if @cache.include? result
    @dead_list << result
    return false
  end
  @cache << result
  return happy?(result)
end

def prime? num
  return false if (num % 2 == 0) or (num % 3 == 0) or (num % 5 == 0)
  end_num = Math.sqrt(num).ceil.to_i
  (2..end_num).to_a.each{|x| return false if num % (x+1) == 0}
end

and we can also embed to String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Fixnum
  class << self;  attr_accessor :dead_list; end
  attr_accessor :cache
  load 'happy_prime.rb'

  def is_happy?
    @cache = []
    @dead_list ||= []
    happy? self
  end

  def is_prime?
    prime? self
    #or
    #require 'mathn'
    #self.prime?
  end
end
Comments

Triangle Problem

Starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

   3
  7 4
 2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below: …–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Triangle

  def initialize (file)
    @file = file
  end

  def get_array
    File.open(@file, 'r') do |f|
      f.each_line.collect do |line|
        line.split(' ').collect { |num| num.to_i }
      end
    end
  end

  def calculate
    result = get_array.reverse.inject do |sum_line, line|
      line.collect.with_index do |num, i|
        num + [sum_line[i], sum_line[i + 1]].max
      end
    end
    result[0]
  end
end

puts Triangle.new('triangle.txt').calculate
Comments

Learn by Practice Extend and Include

Here is some examples to help understand extend and include

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module M_A
  def test
    puts true
  end
end

module M_B
  def test
    puts false
  end
end

class A
  include M_A
  def meta_class
    class << A; self; end;
  end
end
A.test
A.new.test
A.instance_methods.grep /test/
A.new.meta_class.singleton_methods.grep /test/
class A
  include M_B
end
A.new.test

class B
  def B.meta_class
    class << B; self; end;
  end
  class << self
    def meta_class2
      class << B; self; end;
    end
  end
end

B.extend M_A
B.test
B.new.test
B.singleton_methods.grep /test/
B.meta_class.instance_methods.grep /test/

So class have singleton_methods, that should live in the meta_class and that method should be a instance method there extend just put test method to meta class. then later one will cover the first one

result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1.9.3p327 :048 > A.test
NoMethodError: private method `test' called for A:Class
    from (irb):48
    from /home/rharrington/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :049 > A.new.test
false
 => nil 
1.9.3p327 :050 > A.instance_methods.grep /test/
 => [:test] 
1.9.3p327 :051 > A.new.meta_class.singleton_methods.grep /test/
 => [] 
1.9.3p327 :052 > B.extend M_A
 => B 
1.9.3p327 :053 > B.test
true
 => nil 
1.9.3p327 :054 > B.new.test
NoMethodError: private method `test' called for #<B:0x0000000088c5e8>
    from (irb):54
    from /home/rharrington/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :055 > B.singleton_methods.grep /test/
 => [:test] 
1.9.3p327 :056 > B.meta_class.instance_methods.grep /test/
 => [:test] 
Comments

Some Command Line Tools

Some frequently use:

1
2
3
4
du -sh `ls .`

for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
dir | gvim -

How to find - Size of a directory & Free disk space

1
2
3
4
5
6
7
8
9
10
11
12
13
du -sh `ls .`
du -h | tail -n 1
du -h | grep -v '/' | awk '{print $1}'
ls | wc -l
ls -l | grep ^- | wc -l
ls -l | grep ^d | wc -l
ls -l | grep ^l | wc -l
for t in files links directories; do echo `find . -type ${t:0:1} | wc -l` $t; done 2> /dev/null

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr
find . -type f | wc -l
ind . -type d | wc -l
find . -maxdepth 1 -type d | wc -l
1
2
3
4
5
6
#output to vim 
#the - option tells vim to open stdin
dir | gvim -
#inside vim
:r !<cmd>
:r !dir
Comments

How to Use Submodule Brief Instruction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# get the submodule initially
git submodule add ssh://bla submodule_dir
git submodule init

# time passes, submodule upstream is updated
# and you now want to update

# change to the submodule directory
cd submodule_dir

# checkout desired branch
git checkout master

# update
git pull

# get back to your project root
cd ..

# now the submodules are in the state you want, so
git commit -am "Pulled down update to submodule_dir"


git submodules update --init

git submodule foreach git pull origin master

refer git wiki git-scm

Comments