2016년 8월 2일 화요일

Julia 연습 6

In [ ]:
#
# 나는 julia 를 사랑하는가.
#
In [ ]:
# Strings
# 문자열
In [ ]:
# Characters
# 문자
In [1]:
'x'
Out[1]:
'x'
In [2]:
typeof(ans)
Out[2]:
Char
In [3]:
# Char 를 정수(code point)로 변환. 

Int('x')
Out[3]:
120
In [4]:
'한'
Out[4]:
'한'
In [5]:
typeof('한')
Out[5]:
Char
In [9]:
code_point = Int('한')
Out[9]:
54620
In [10]:
typeof(code_point)
Out[10]:
Int64
In [11]:
# 정수를 글자로 바꾼다.

Char( code_point)
Out[11]:
'한'
In [12]:
# 정수가 유효한 Unicode code point 인가.

isvalid(Char, code_point)
Out[12]:
true
In [13]:
isvalid(Char, 0x110000)
Out[13]:
false
In [14]:
# 유니코드 한글자를 표시할때 \u 를 쓴다.
# 16진수 최대 4글자까지 쓴다.
# \U 를 쓰면 16진수 최대 8글자까지 쓸수있다.

'\u0'
Out[14]:
'\0'
In [15]:
'\u78'
Out[15]:
'x'
In [16]:
'\u2200'
Out[16]:
'∀'
In [17]:
'\U10ffff'
Out[17]:
'\U10ffff'
In [18]:
'\u54620'
LoadError: syntax: invalid character literal
while loading In[18], in expression starting on line 1
In [19]:
'\U54620'
Out[19]:
'\U54620'
In [20]:
Char('\U54620')
Out[20]:
'\U54620'
In [21]:
print('\U54620')
񔘠
In [22]:
# C 에서 사용하는 escape character

Int('\0')
Out[22]:
0
In [23]:
Int('\t')
Out[23]:
9
In [24]:
Int('\n')
Out[24]:
10
In [25]:
Int('\e')
Out[25]:
27
In [26]:
Int('\x7f')
Out[26]:
127
In [27]:
Int('\177')
Out[27]:
127
In [28]:
Int('\xff')
Out[28]:
255
In [29]:
# 비교

'A' < 'a'
Out[29]:
true
In [30]:
'A' <= 'a' <= 'Z'
Out[30]:
false
In [31]:
'A' <= 'X' <= 'Z'
Out[31]:
true
In [32]:
'x' - 'a'
Out[32]:
23
In [33]:
'A' + 1
Out[33]:
'B'
In [34]:
'한' == '한'
Out[34]:
true
In [35]:
'가' <= '나'
Out[35]:
true
In [36]:
'가' <= '나' <= '다'
Out[36]:
true
In [37]:
'가' + 1
Out[37]:
'각'
In [56]:
char_count = 0

for i in 0:(300-1)
    
    print("$('가' + i)")
    char_count += 1
    
    if(20 <= char_count) 
        print("\n")
        char_count=0
    end
end
가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓
갔강갖갗갘같갚갛개객갞갟갠갡갢갣갤갥갦갧
갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷갸갹갺갻
갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏
걐걑걒걓걔걕걖걗걘걙걚걛걜걝걞걟걠걡걢걣
걤걥걦걧걨걩걪걫걬걭걮걯거걱걲걳건걵걶걷
걸걹걺걻걼걽걾걿검겁겂것겄겅겆겇겈겉겊겋
게겍겎겏겐겑겒겓겔겕겖겗겘겙겚겛겜겝겞겟
겠겡겢겣겤겥겦겧겨격겪겫견겭겮겯결겱겲겳
겴겵겶겷겸겹겺겻겼경겾겿곀곁곂곃계곅곆곇
곈곉곊곋곌곍곎곏곐곑곒곓곔곕곖곗곘곙곚곛
곜곝곞곟고곡곢곣곤곥곦곧골곩곪곫곬곭곮곯
곰곱곲곳곴공곶곷곸곹곺곻과곽곾곿관괁괂괃
괄괅괆괇괈괉괊괋괌괍괎괏괐광괒괓괔괕괖괗
괘괙괚괛괜괝괞괟괠괡괢괣괤괥괦괧괨괩괪괫
In [ ]:
# String
# 문자열

# 문자열은 따옴표(")(double quote) 로 표시한다.
# 삼중 따옴표(""")(triple double quote) 도 제공한다.
In [59]:
str = "Hello, world.\n"
Out[59]:
"Hello, world.\n"
In [58]:
"""Contains "quote" characters"""
Out[58]:
"Contains \"quote\" characters"
In [74]:
# 문자열에서 한글자 가져오기.

str[1]
Out[74]:
'H'
In [61]:
str[6]
Out[61]:
','
In [62]:
str[end]
Out[62]:
'\n'
In [63]:
# end 는 되고, begin 은 안된다.

str[begin]
LoadError: syntax: unexpected "]"
while loading In[63], in expression starting on line 1
In [69]:
# Julia 에서 모든 indexing 은 1-base 이다.
# 0-base 가 아니다.
# index 에 0 을 쓰면 이런꼴을 보게된다.


str[0]
LoadError: BoundsError: attempt to access 14-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f
 0x2c
 0x20
 0x77
 0x6f
 0x72
 0x6c
 0x64
 0x2e
 0x0a
  at index [0]
while loading In[69], in expression starting on line 5

 in getindex at ./ascii.jl:13
In [65]:
str[end-1]
Out[65]:
'.'
In [66]:
str[end÷2]
Out[66]:
' '
In [67]:
# endof 는 된다.

endof(str)
Out[67]:
14
In [68]:
# beginof 는 안된다.

beginof(str)
LoadError: UndefVarError: beginof not defined
while loading In[68], in expression starting on line 1
In [70]:
# end 보다 큰값을 index 에 쓰게되면
# 이런꼴을 보게된다.

str[end+1]
LoadError: BoundsError: attempt to access 14-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f
 0x2c
 0x20
 0x77
 0x6f
 0x72
 0x6c
 0x64
 0x2e
 0x0a
  at index [15]
while loading In[70], in expression starting on line 1

 in getindex at ./ascii.jl:13
In [71]:
# substring
# 부분문자열
# 문자열 일부를 추출하기.

str[4:9]
Out[71]:
"lo, wo"
In [72]:
# 글자를 리턴한다. Char

str[6]
Out[72]:
','
In [73]:
# 문자열을 리턴한다. String

str[6:6]
Out[73]:
","
In [192]:
# Unicode escape sequence \u \U 를 써서 문자열을 표현.

s = "\u2200 x \u2203"
Out[192]:
"∀ x ∃"
In [76]:
# UTF-8 에서 
# 0x80(128) 이하는 ASCII 를 표현하며 한바이트로 표현된다.
# 0x80(128) 이상은 한글자가 여러바이트를 사용하여 표현된다.
# 한글자를 표현하기위해 최대 4bite 까지 사용된다.
# 따라서 
# UTF-8 문자열의 모든 byte index 가 유효한 글자에 대응되는것은 아니라는 사실.

s[1]
Out[76]:
'∀'
In [77]:
# 무효한 byte index 를 사용하면 험한꼴을 보게된다.

s[2]
LoadError: UnicodeError: invalid character index
while loading In[77], in expression starting on line 3

 in next at ./unicode/utf8.jl:65
 in getindex at strings/basic.jl:37
In [78]:
# 무효한 byte index 를 사용하면 험한꼴을 보게된다.

s[3]
LoadError: UnicodeError: invalid character index
while loading In[78], in expression starting on line 1

 in next at ./unicode/utf8.jl:65
 in getindex at strings/basic.jl:37
In [79]:
s[4]
Out[79]:
' '
In [80]:
# 여기서 문자 '∀' 는 3바이트 글자인 경우다.
# 그래서 인덱스 2, 3 은 무효하다.
# 인덱스 4 는 유효하다.

# 다음 유효 인덱스가 어디인가.

nextind(s,1)
Out[80]:
4
In [81]:
# 그다음 유효 인덱스가 어디인가.

nextind(s,4)
Out[81]:
5
In [82]:
# 무효인덱스를 인자로 줘도, 적절히 작동한다.

nextind(s,2)
Out[82]:
4
In [83]:
nextind(s,6)
Out[83]:
7
In [84]:
# 몇 글자냐.

length(s)
Out[84]:
5
In [87]:
# 몇 바이트냐.

sizeof(s)
Out[87]:
9
In [193]:
# 첫번째 글자의 유효 인덱스

start(s)
Out[193]:
1
In [194]:
# 마지막 글자의 유효 인덱스

endof(s)
Out[194]:
7
In [195]:
s[7]
Out[195]:
'∃'
In [196]:
s[endof(s)]
Out[196]:
'∃'
In [197]:
s[end]
Out[197]:
'∃'
In [199]:
s[start(s)]
Out[199]:
'∀'
In [134]:
# 문자열은 자체적으로 iterator 기능을 가지고있다.

i=1
for c in s
    print( "$i: ")
    println( c)
    i += 1
end
1: ∀
2:  
3: x
4:  
5: ∃
In [130]:
# concatenation
# 문자열 이어붙이기


greet = "Hello"
whom = "world"

string( greet, ", ", whom, ".\n")
Out[130]:
"Hello, world.\n"
In [133]:
# 문자열을 이어붙일때
# 위와 같이 생성자(string)를 직접 호출하는것은
# 귀찮다.

# 그리고, 모양이 안이쁘다.
# 모양이 밉다.

# 미운건 안쓴다.


# Perl 언어에는
# 문자열에 $를 써서 interpolation 하는 기능이 있다.
# Julia 는 Perl 의 $ 를 가져온다.


"$greet, $whom.\n"
Out[133]:
"Hello, world.\n"
In [135]:
"1 + 2 = $(1 + 2)"
Out[135]:
"1 + 2 = 3"
In [136]:
v = [1,2,3]
Out[136]:
3-element Array{Int64,1}:
 1
 2
 3
In [137]:
"v: $v"
Out[137]:
"v: [1,2,3]"
In [138]:
c = 'x'
Out[138]:
'x'
In [139]:
"hi, $c"
Out[139]:
"hi, x"
In [141]:
# 문자열에 $ 문자 자체를 넣으려면.
# \ (backslash) 로 escape 시킨다.

print("I have \$100 in my account.\n")
I have $100 in my account.
In [142]:
# Triple-Quoted String Literals
# 삼중 따옴표 문자열 상수


"""hello"""
Out[142]:
"hello"
In [143]:
# 위 결과와 똑같다.

"""
hello"""
Out[143]:
"hello"
In [144]:
# 위 결과와 똑같다.

ans == """hello"""
Out[144]:
true
In [148]:
# 최소로 indent 된 라인을 기준으로 indentation level 을 결정한다.
# 여기서는 Hello 를 기준으로 indentation level 을 결정한다.

str = """
Hello,
    world.
   """
Out[148]:
"Hello,\n    world.\n   "
In [147]:
# 최소로 indent 된 라인을 기준으로 indentation level 을 결정한다.
# 여기서는 마지막에 있는 닫는 """ 를 기준으로 indentation level 이 결정된다.

str = """
    Hello,
    world.
   """
Out[147]:
" Hello,\n world.\n"
In [149]:
# 모든 문자열에서 line break 는 
# newline(LF) 문자 \n 이다.
# carriage return(CR) 문자 \r 을 포함시키려면
# 명시적 escape \r 을 사용하라.

"a CRLF line ending\r\n"
Out[149]:
"a CRLF line ending\r\n"
In [150]:
# 일반 연산자
# lexicographically compare
# 사전적 비교
# 가나다순서 비교
# ABC 순서 비교

"abracadabra" < "xylophone"
Out[150]:
true
In [151]:
"abracadabra" == "xylophone"
Out[151]:
false
In [152]:
"Hello, world." != "Goodbye, world."
Out[152]:
true
In [154]:
"1 + 2 = 3" == "1 + 2 = $(1 + 2)"
Out[154]:
true
In [165]:
str = "xylophone"
Out[165]:
"xylophone"
In [166]:
# search
# 검색

search("xylophone", 'x')
Out[166]:
1
In [167]:
# 있는것.

search( str, 'p')
Out[167]:
5
In [168]:
# 없는것

search( str, 'z')
Out[168]:
0
In [169]:
subrange = search( str, "phone")
Out[169]:
5:9
In [170]:
str[5:9]
Out[170]:
"phone"
In [171]:
typeof(subrange)
Out[171]:
UnitRange{Int64}
In [173]:
str[subrange]
Out[173]:
"phone"
In [174]:
typeof(subrange)
Out[174]:
UnitRange{Int64}
In [176]:
# range 가 함수로 정의되어있다.
# 이것을 변수명으로 쓰면, shadowing 이 발생한다.

typeof(range)
Out[176]:
Function
In [180]:
search( "xylophone", 'o')
search( str, 'o')
Out[180]:
4
In [178]:
# 검색 시작 위치를 지정하는 기능.

# 이것이 어디에 쓰는 물건인지.
# 쓰면 무엇이 좋은지.
# 지금은 궁금하지 않다.

# 이런 기능이 존재한다는 사실만 존재한다.


search( str, 'o', 5)
Out[178]:
7
In [179]:
search( str, 'o', 8)
Out[179]:
0
In [182]:
# 시작위치를 지정할때 
# 범위를 벗어나면 험한꼴을 보게된다.


search( str, 'o', 1000)
LoadError: BoundsError: attempt to access "xylophone"
  at index [1000]
while loading In[182], in expression starting on line 5

 in search at ./ascii.jl:24
In [183]:
# contains
# 있냐

contains("Hello, world.", "world")
Out[183]:
true
In [185]:
contains("Xylophone", "a")
Out[185]:
false
In [184]:
# 이건 된다.

contains("Xylophone", "o")
Out[184]:
true
In [186]:
# 이건 안된다.

contains("Xylophone", 'o')
LoadError: MethodError: `contains` has no method matching contains(::ASCIIString, ::Char)
Closest candidates are:
  contains(!Matched::Function, ::Any, !Matched::Any)
  contains(::AbstractString, !Matched::AbstractString)
while loading In[186], in expression starting on line 1
In [187]:
# 존재여부 검사
# contains 로 안되면 이걸 써라.


in( 'o', "Xylophone")
Out[187]:
true
In [188]:
# repeat()
# join()
# 반복
# 연결

repeat("abc ", 10)
Out[188]:
"abc abc abc abc abc abc abc abc abc abc "
In [237]:
# 모든것을 구분자(", ")로 interpolation

join(["사과", "바나나", "포도", "파인애플"], ", ")
Out[237]:
"사과, 바나나, 포도, 파인애플"
In [239]:
# 모든것을 구분자(", ")로 interpolation 하고, 
# 마지막엔 다른 구분자를 사용.


join(["사과", "바나나", "포도", "파인애플"], ", ", " 그리고 ")
Out[239]:
"사과, 바나나, 포도 그리고 파인애플"
In [227]:
s = "Hello world."
Out[227]:
"Hello world."
In [228]:
i = start(s)
while( i <= endof(s))
    c, j = next(s, i)
    i = j
    println( "$c : $j")
end
H : 2
e : 3
l : 4
l : 5
o : 6
  : 7
w : 8
o : 9
r : 10
l : 11
d : 12
. : 13
In [229]:
s = "밥은 먹고 다니냐."
Out[229]:
"밥은 먹고 다니냐."
In [230]:
s[4]
Out[230]:
'은'
In [231]:
start(s)
Out[231]:
1
In [232]:
endof(s)
Out[232]:
24
In [233]:
s[start(s)]
Out[233]:
'밥'
In [234]:
s[endof(s)]
Out[234]:
'.'
In [235]:
i = start(s)
while( i <= endof(s))
    c, j = next(s, i)
    i = j
    println( "$c : $j")
end
밥 : 4
은 : 7
  : 8
먹 : 11
고 : 14
  : 15
다 : 18
니 : 21
냐 : 24
. : 25
In [240]:
for c in s
    println( c)
end
밥
은
 
먹
고
 
다
니
냐
.
In [241]:
# Regular Expressions
# 정규 표현식

r"^\s*(?:#|$)"
Out[241]:
r"^\s*(?:#|$)"
In [242]:
typeof(ans)
Out[242]:
Regex
In [243]:
ismatch(r"^\s*(?:#|$)", "not a comment")
Out[243]:
false
In [244]:
ismatch(r"^\s*(?:#|$)", "# a comment")
Out[244]:
true
In [245]:
match(r"^\s*(?:#|$)", "not a comment")
In [246]:
match(r"^\s*(?:#|$)", "# a comment")
Out[246]:
RegexMatch("#")
In [250]:
# 아무것도 리턴하지 않은것을
# nothing 으로 검사한다.


line = "not a comment"
m = match(r"^\s*(?:#|$)", line)
if m == nothing
    println("주석이 아니다")
else
    println("공백 또는 주석이다")
end
주석이 아니다
In [251]:
m = match(r"^\s*(?:#\s*(.*?)\s*$|$)", "# a comment")
Out[251]:
RegexMatch("# a comment", 1="a comment")
In [252]:
# 검색 시작위치 지정

m = match(r"[0-9]", "aaaa1aaaa2aaaa3", 1)
Out[252]:
RegexMatch("1")
In [253]:
m = match(r"[0-9]", "aaaa1aaaa2aaaa3", 6)
Out[253]:
RegexMatch("2")
In [254]:
m = match(r"[0-9]", "aaaa1aaaa2aaaa3", 11)
Out[254]:
RegexMatch("3")
In [255]:
fieldnames(m)
Out[255]:
5-element Array{Symbol,1}:
 :match   
 :captures
 :offset  
 :offsets 
 :regex   
In [256]:
m.match
Out[256]:
"3"
In [257]:
m.captures
Out[257]:
0-element Array{Union{SubString{UTF8String},Void},1}
In [258]:
m.offset
Out[258]:
15
In [259]:
m.offsets
Out[259]:
0-element Array{Int64,1}
In [260]:
m.regex
Out[260]:
r"[0-9]"
In [261]:
# 이름으로

m=match(r"(?P<hour>\d+):(?P<minute>\d+)", "12:45")
Out[261]:
RegexMatch("12:45", hour="12", minute="45")
In [263]:
# 이름으로

m[:hour]
Out[263]:
"12"
In [264]:
# 이름으로

m[:minute]
Out[264]:
"45"
In [265]:
# 인덱스로

m[1]
Out[265]:
"12"
In [266]:
# 인덱스로

m[2]
Out[266]:
"45"
In [268]:
replace("first second", r"(\w+) (?P<agroup>\w+)", s"\g<agroup> \1")
Out[268]:
"second first"
In [269]:
replace("a", r".", s"\g<0>1")
Out[269]:
"a1"
In [270]:
# flag : i, m, s, x
# i : case-Insensitive pattern matching
# m : treat string as multiple lines
# s : treat string as single line
# x : ignore most whitespace

r"a+.*b+.*?d$"ism
Out[270]:
r"a+.*b+.*?d$"ims
In [271]:
match(r"a+.*b+.*?d$"ism, "Goodby,\nOh, angry,\nBad world\n")
Out[271]:
RegexMatch("angry,\nBad world")
In [272]:
# Byte Array Literals
# 바이트 배열 상수

# 이것은 UInt8 타입의 배열이다.
# 이것은 UTF-8 이 아니다.


b"DATA\xff\u2200"
Out[272]:
8-element Array{UInt8,1}:
 0x44
 0x41
 0x54
 0x41
 0xff
 0xe2
 0x88
 0x80
In [273]:
b"\xff"
Out[273]:
1-element Array{UInt8,1}:
 0xff
In [274]:
b"\uff"
Out[274]:
2-element Array{UInt8,1}:
 0xc3
 0xbf
In [275]:
# Version Number Literals
# 버전 번호 상수

VERSION
Out[275]:
v"0.4.6"
In [276]:
if v"0.2" < VERSION < v"0.3-"
    println( "버전 0.2 에 해당하는 기능 처리")
end
In [277]:
v = VERSION
Out[277]:
v"0.4.6"
In [278]:
typeof(v)
Out[278]:
VersionNumber

Firefly Algorithms

firefly algorithm 001 Firefly Algorithms ¶ 반딧불 알고리즘 번역 요약 ¶ References [1] X. S. Y...