Sunday, 4 November 2018

Note For Ethernaut#2 - Fallout

Solidity

pragma solidity ^0.4.18;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';

contract Fallout is Ownable {

  mapping (address => uint) allocations;

  /* constructor */
  function Fal1out() public payable {
    owner = msg.sender;
    allocations[owner] = msg.value;
  }

  function allocate() public payable {
    allocations[msg.sender] += msg.value;
  }

  function sendAllocation(address allocator) public {
    require(allocations[allocator] > 0);
    allocator.transfer(allocations[allocator]);
  }

  function collectAllocations() public onlyOwner {
    msg.sender.transfer(this.balance);
  }

  function allocatorBalance(address allocator) public view returns (uint) {
    return allocations[allocator];
  }
}

What I learned From It

  • careless could cause some harm mistake
tags: ethernaut solidity

Note For Ethernaut#1 - Fallback

Solidity

pragma solidity ^0.4.18;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';

contract Fallback is Ownable {

  mapping(address => uint) public contributions;

  function Fallback() public {
    contributions[msg.sender] = 1000 * (1 ether);
  }

  function contribute() public payable {
    require(msg.value < 0.001 ether);
    contributions[msg.sender] += msg.value;
    if(contributions[msg.sender] > contributions[owner]) {
      owner = msg.sender;
    }
  }

  function getContribution() public view returns (uint) {
    return contributions[msg.sender];
  }

  function withdraw() public onlyOwner {
    owner.transfer(this.balance);
  }

  function() payable public {
    require(msg.value > 0 && contributions[msg.sender] > 0);
    owner = msg.sender;
  }
}

What I learned From It

Fallback Function

每個合約裡最多能有一個未命名的公開 funtion,沒有參數、回傳值。
在幾種情況下會執行:
  1. 呼叫合約的函式不在 abi 裡。
  2. 對合約轉帳 (不帶 data 參數)。
只有 payable 的 Function 才能接收 Ether.

Reference:

tags: ethernaut solidity

Note For Ethernaut#0 - Hello

Solidity

pragma solidity ^0.4.18;

contract Instance {

  string public password;
  uint8 public infoNum = 42;
  string public theMethodName = 'The method name is method7123949.';
  bool private cleared = false;

  // constructor
  function Instance(string _password) public {
    password = _password;
  }

  function info() public pure returns (string) {
    return 'You will find what you need in info1().';
  }

  function info1() public pure returns (string) {
    return 'Try info2(), but with "hello" as a parameter.';
  }

  function info2(string param) public pure returns (string) {
    if(keccak256(param) == keccak256('hello')) {
      return 'The property infoNum holds the number of the next info method to call.';
    }
    return 'Wrong parameter.';
  }

  function info42() public pure returns (string) {
    return 'theMethodName is the name of the next method.';
  }

  function method7123949() public pure returns (string) {
    return 'If you know the password, submit it to authenticate().';
  }

  function authenticate(string passkey) public {
    if(keccak256(passkey) == keccak256(password)) {
      cleared = true;
    }
  }

  function getCleared() public view returns (bool) {
    return cleared;
  }
}
在判斷參數時用到 keccak256,文件提到 keccak256 是 “tight pakced”,而 “tightly packed” 是指不會對參數進行 padding 處理。

What I learned From It

  • 參數會先經過 keccak256 pack
  • 取 public 變數與參數方式一樣
tags: ethernaut solidity

Friday, 5 October 2018

Music HW#1 - 3度音程

比起一個8度內的3度音程,更喜歡高8度的3度的聲響,2個是完全不同的感覺。

3度音在指板上的相對位置

1. 一個8度的3


  • Root on 6th String

  • Root on 5th String

  • Root on 4th String

  • Root on 3th String

  • Root on 2th String


2. 高8的3


  • Root on 6th String

  • Root on 5th String

  • Root on 4th String


例子

1. 一個8度的3


  • 金雅中 - 瑪麗亞

分析:
 Key: B
 3度在前奏/主歌部份:運用 I VII VI VII 的3度和聲

2. 高8的3


  • RHCP - Scar Tissue



這首是讓我發現原來這麼好聽的聲音,其實只是3度 的歌。

分析:
 Key: F
 3度在前奏/主歌部份:運用 I V VI 的 R & 3 單音製造出遼闊的感覺。

  • James Bay - Let It Go



分析:
 Key: A#m
 

  • The Beatles - Blackbird



分析:
 這首是例子裡面最令人驚奇的,非常大量運用3度,除了前幾首的手法外,與根音的距離也是3。

Thursday, 13 July 2017

Reference to a Pointer

Notes for reading C++ Primer 4/e.
void ptrswap(int *&v1, int *&v2) {
    int *tmp = v2;
    v1 = v2;
    v2 = tmp;
}

int main() {
    int i = 10;
    int j = 20;
    int *pi = &i;
    int *pj = &j;

    cout << *pi << "  " << *pj << endl;
    ptrswap(pi, pj);
    cout << *pi << " " << *pj << endl;

    return 0;
}
Result:
10 20
20 10

Reference

Notes for reading C++ Primer 4/e.

Reference

nonconst reference can only reference to the same nonconst object.
int incr(int val) {
  return ++val;
}

int incrRef(int &val) {
  return ++val;
}

int incrRefConst(const int &val) {
  cout << val << endl;

  return val;
}

int main() {
  short v1 = 0;
  const int v2 = 42;
  double v4 = 3.4;

  int v3 = incr(v1);
  v3 = incrRef(v1);  // X: no matching function for call to 'incrRef'
  v3 = incrRefConst(v1);  // 0

  int v5 = incr(v4);
  v5 = incrRef(v4);  // X: no matching function for call to 'incrRef'
  v5 = incrRefConst(v4);  // 3
  cout << v4 << " " << v5 << endl;  // 3.4 3

  return 0;
}


[note]:

  • nonconst refernce parameter 的彈性較低,不能以 const object 初始化,也不能以字面常數 (ex. 42, 'a', "Hello", 3.14...) 或 rvalue 做為 argument.

Monday, 10 July 2017

Knockin' on Heaven's Door

-Original-

Key: G

|    I    |    V    |    ii    |    ii    |
|    I    |    V    |   IV   |   IV   |



-Gun's N' Roses' Version-
Key: Gb

|    I    |    V    |   IV   |   IV   |

Solo
| - 1 2#2 12 | 2 5 2#2 1 | 1--- | --- 61 |
| 6 5- 43 | 45-- 43 | 4 36-- | -- 34 14 |
| 3- 6 1 23 | 2- 3 3 | 1--- | --- 45 |
| - 43 43 45 | - 43 4 46 | --- 56 | 565 23232 1 | 1--- |


-Solo Cover-

Tuesday, 4 July 2017

Key Signature

Note for reading Key Signature.

Circle of Fifths


Key Signature

  • 0#b: C Dm Em F G Am Bdim
  • 1#: G Am Bm C D Em F#dim
  • 2#: D Em F#m G A Bm C#dim
  • 3#: A Bm C#m D E F#m G#dim
  • 4#: E F#m G#m A B C#m D#dim
  • 5#: B C#m D#m E F# G#m A#dim
  • 6#: F# G#m A#m B C# D#m E#dim
  • 6b: Gb Abm Bbm Cb Db Ebm Fdim
  • 5b: Db Ebm Fm Gb Ab Bbm Cdim
  • 4b: Ab Bbm Cm Db Eb Fm Gdim
  • 3b: Eb Fm Gm Ab Bb Cm Ddim
  • 2b: Bb Cm Dm Eb F Gm Adim
  • 1b: F Gm Am Bb C Dm Edim

Reference:

Wednesday, 28 June 2017

Array

Notes for reading C++ Primer 4/e.

Dynamic Array

value-initialize


In dynamic array, it could only initial w/ default value, not like static array, could initial as different values.
int *pia = new int[10]();  // 10 ints w/ default value 0

const

Notes for reading C++ Primer 4/e.

const Reference

Reference to a constant object.
const int ival = 1024;
const int &refVal = ival;
const int &r = 42;
const int &r2 = r + 3;


const_iterator

for (vector<string>::const_iterator iter = text.begin();
iter != text.end(); ++iter) {
    *iter = " ";  // error: *iter is const
}
vector<int> nums(10);
const vector<int>::iterator cit = nums.begin();
*cit = 1;  // OK
++cit;  // error: cit is const


Pointers & const

Pointers to const

const double pi = 3.14;
double *ptr = &pi;  // error: nonconst pointer
const double *cptr = &pi;  // OK

double dval = 2.99;
cptr = &dval;  // OK

const Pointer

int errNumber = 0;
int *const curErr = &errNumber;
curErr = curErr;  // error: curErr is const

if (*curErr) {
    errorHandler();
    *curErr = 0;  // OK
}

const Pointer Point To A const Object

const double pi = 3.14159;
const double *const pi_ptr = &pi;
pi_ptr is a constant pointer, point to a const double object.


Pointers & Typedefs

typedef string *pstring;
const pstring cstr;
Q: What is cstr?
A: Constant point, point to string.

Tuesday, 27 June 2017

Minor (Aeolian)

大調小調



自然/和聲/旋律小音階


  • Major Scale: 1 2 3 4 5 6 7
  • Nature Minor Scale: 1 2 b3 4 5 b6 b7
  • Harmonic Minor Scale: 1 2 b3 4 5 b6 7
  • Melodic Minor Scale: 1 2 b3 4 5 6 7 1 b7 b6 5 4 b3 2 1

Thursday, 22 June 2017

Secondary Dominant

Learning notes of Secondary Dominant.

Secondary Dominant


什麼是大調小調?



Dominant


  • V -> I

讓 Dominant 更漂亮



  • ii -> V -> I or vii7(b5) -> V7 -> i

用 2-5-1


  • ii -> V -> I -> (Key - whole note) ii -> V -> I -> (Key - whole note) ii -> V -> I -> …

Advanced Reading


Wednesday, 21 June 2017

How to Make Chord Progression

Learning how to make chord progression for a melody.

How to Make Chord Progression

如何幫一個旋律配上和弦?


  • 使用和弦內音的方式來配置

用 C, G7, Dm 和 F



  • 使用和弦內音的方式來配置 並 考慮 ii -> V -> I, I -> IV 等慣用進行

用大調 1-6 級



  • Tonic -> Sub-dominant -> Dominant -> Tonic 的感覺考慮進去
  • normally we use Tonic -> Sub-dominant or Tonic -> Dominant (but it’s more comfortable to use Tonic -> Sub-dominant -> Tonic) (1)
  • normally we use Sub-dominant -> Tonic or Sub-dominant -> Dominant (2)
  • normally we use Dominant -> Tonic (3)
  • 先用 I, IV, V 配置,再用 (3), (2), (1) 修飾

用 Secondary Dominant Chord



  • 每個和弦轉換皆可觀查是否能用

Advanced Reading


Major (Ionian)

Learning from key C.

Triads

  • C: 1-3-5 (1 3 5)
  • Dm: 2-4-6 (1 b3 5)
  • Em: 3-5-7 (1 b3 5)
  • F: 4-6-1 (1 3 5)
  • G: 5-7-2 (1 3 5)
  • Am: 6-1-3 (1 b3 5)
  • Bdim: 7-2-4 (1 b3 b5)

Shape



  • 1st Inversion



  • 2nd Inversion


Seventh Chord

  • CM7: 1-3-5-7 (1 3 5 7)
  • Dm7: 2-4-6-1 (1 b3 5 b7)
  • Em7: 3-5-7-2 (1 b3 5 b7)
  • FM7: 4-6-1-3 (1 3 5 7)
  • G7: 5-7-2-4 (1 3 5 b7)
  • Am7: 6-1-3-5 (1 b3 5 b7)
  • Bm7(b5): 7-2-4-6 (1 b3 b5 b7)

[note]:
  1. Bm is 7-2-#4 and Bm7 is 7-2-#4-6.
  2. Half-diminished seventh: 1 b3 b5 b7
  3. Diminished seventh: 1 b3 b5 bb7
Reference:

External Reading:

Wednesday, 18 January 2017

[Class Diagram] Association, Aggregation and Composition


Association: ... has a ...
ex. Professor has books and books belong to one professor.

Aggregation: ... owns a ...
ex. Department has many professors in it, and professors belong to many departments or belong to many schools.

Composition: ... is a part of ...
ex. University has many departments, and departments belong to one university. While University closed, departments won't be existed.

Sunday, 15 January 2017

[C++] return by address/reference


 int *createArrayAddr(int n) {  
   int *a = new int(5);  
     
   cout << (void*) a << endl;  
     
   return a;  
 }  
   
 int &createArrayRef(int &n) {  
     
   cout << &n << endl;  
     
   return n;  
 }  
   
 int main(int argc, const char * argv[]) {  
   int *a = createArrayAddr(5);  
   cout << (void*) a << endl;  
     
   int n = 55;  
   cout << &n << endl;  
   int &n2 = createArrayRef(n);  
   cout << &n2 << endl;  
   
   return 0;  
 }  

RESULT:

 0x1001054d0  
 0x1001054d0  
 0x7fff5fbff7f4  
 0x7fff5fbff7f4  
 0x7fff5fbff7f4  

Thursday, 12 January 2017

Your body language shapes who you are



Self-fulfill, faith, you believed in who you are, and you could be the one you want.

1-Bit Music from Tristan Perich

There's a composer named Tristan Perich composes his album as 1-bit music, more interested thing is when you get his CDs, there's no CD in it, you could just plug your ear phone in and listen to his music.

Try to listen to his CertainMovement in the 1st album.



















Refer http://www.1bitmusic.com/ to get to know him more.