2022年9月30日 星期五

從燦坤聯名信用卡看會員卡號

在信用卡背面,

從左到右,最左邊單獨一個的數字不用看,

接下來前3個數字不用看,

會員卡號從第4個數字開始看,到倒數第2個數字。

一共8碼,也就是最後一個,最右邊的數字不用看。

2022年9月26日 星期一

平面圓形切割

 在Zero Judge上的

a042: 平面圓形切割

對任意正整數n,平面上的n 個圓最多可將平面切成幾個區域?


我發現跟以下數列有關:



2020年7月8日 星期三

Visual Studio C# 2019, 加入class編譯到的方式

1. (double) click *.sln

 2. Move to namespace and click right button of mouse

3. Create class


4. Click first item to back if possible


2019年9月25日 星期三

In Python 3, m = [i] vs m[0] = i

在Python 3裡, assign是call by value? 還是call by reference呢?
Python自創了一個名詞,說是call by assignment.
但是從C語言學起的我,還是用call by value or call by reference來說明,會比較清楚。

如果i is integer, then:

case I:
m = [i]

case II:
m[0] = i

Q: 這兩者差在哪呢?
A:
case I:
m = [i], 是call by value, 也就是把m指向[i]位子的value。
case II:
m[0] = i, 是call by reference, 也就是把m[0]位子的值,設成i.

也許有人會覺得,出來的值都一樣,那有啥差別呢?
請看以下code:

class Solution:
    def byValue(self, t, m = [0]):
        print('m=', m)
        for i in t:
            m = [i]
        return m
    
    def byRef(self, t, m = [0]):
        print('m=', m)
        for i in t:
            m[0] = i
        return m
        
    def byValue1(self, t, m = 0):
        print('m=', m)
        for i in t:
            m = 1
        return m
    
    def byValue2(self, t, m = 0):
        print('m=', m)
        for i in t:
            m = 2
        return m
        
def main():
    a = Solution()
    
    print('byRef')
    print(a.byRef(['good']))
    print(a.byRef(['afternoon']))
    print('byValue')
    print(a.byValue(['fine.']))
    print(a.byValue(['thanks']))
    print('byValue1')
    print(a.byValue1(['good']))
    print(a.byValue1(['afternoon']))
    print('byValue2')
    print(a.byValue2(['fine.']))
    print(a.byValue2(['thanks']))
    
if __name__ == "__main__":
    main()

#會印出:
byRef
m= [0]
['good']
m= ['good']
['afternoon']
byValue
m= [0]
['fine.']
m= [0]
['thanks']
byValue1
m= 0
1
m= 0
1
byValue2
m= 0
2
m= 0
2


2018年8月26日 星期日

python 3 SWIG on windows 10

For python 3:

/* File : example.c */
 
 #include  <time.h>
 double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
  
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

Interface file

 /* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 

Building a Python module

D:\temp\Documents\Python_call_C\example>swig -python -py3 example.i D:\temp\Documents\Python_call_C\example>gcc -c example.c example_wrap.c -I"D:\Program Files (x86)\Python36-32\include" D:\temp\Documents\Python_call_C\example>gcc -shared example.o example_wrap.o "D:\Program Files (x86)\Python36-32\python36.dll" "C:\Windows\System32\msvcr120.dll" -o _example.pyd
We can now use the Python module:
import example
print(example.fact(5))
print(example.my_mod(7,3))
print(example.get_time())
>>>>>>>>>>>>>>>>>>>>>>
Example Result:
120 1 Sun Aug 26 21:26:01 2018

Lean 4 極簡教學 (Using VS Code on macOS)

 1. Install Lean Environment: https://lean-lang.org/install/ 試著完Step one, two, three 2. 在VS code下,建一新的project: 取名Hello_World 3. 嘗試執行Main.lea...