length of the line segment
length-of-the-line-segment¶
Problem¶
In [4]:
# package로 인식을 못해서 어쩔 수 없이 이렇게 import함
%cd ..
from helper.war import Kata
%cd kata/
In [5]:
kata = Kata()
In [6]:
challenge_url = 'http://www.codewars.com/kata/length-of-the-line-segment/python'
description = kata.get_description_in_code_challenge(challenge_url)
print(description)
In [2]:
abs(-2-(-7))
Out[2]:
In [3]:
abs(8-(-5))
Out[3]:
Logic¶
-
좌표평면에서 두 점 사이의 거리 | 수학방
- 영어로 써져 있어서 엄청 어려워 보이는데 별거 없다. 그냥 중학교때 배운 좌표평면에서 두 점 사이의 거리 구하는 공식이다...
- 첫번째 x값 - 두번째 x값
- 첫번째 y값 - 두번째 y값
- 그 결과값을 더하고
- root 취해준다.
- 이게 뭘 하는건지는 확실하지 않다...
- 일단 로직이 이렇게 된다는 것을 알 수 있다.
In [6]:
array = [[0,0],[1,1]]
array
Out[6]:
In [7]:
a, b = array
a
Out[7]:
In [8]:
b
Out[8]:
In [10]:
abs(0 - (1)) ** 2
Out[10]:
In [11]:
5 ** 2
Out[11]:
In [14]:
from math import sqrt
In [15]:
sqrt(5 ** 2)
Out[15]:
In [17]:
sqrt(2)
Out[17]:
In [31]:
'{:1.3}'.format(sqrt(2))
Out[31]:
In [39]:
'{:1.2f}'.format(18.0555333)
Out[39]:
Source¶
My Solution¶
In [1]:
from math import sqrt
def length_of_line_my1(array):
# print(array)
a, b = array
# print(a)
# print(b)
tmp = sum(abs(a[i] - b[i]) ** 2 for i in range(len(array)))
tmp = '{:1.2f}'.format(sqrt(tmp))
# print(tmp)
return tmp
Refactoring¶
In [2]:
from math import sqrt
def length_of_line_my2(array):
a, b = array
tmp = sum(abs(a[i] - b[i]) ** 2 for i in range(len(array)))
result = '{:1.2f}'.format(sqrt(tmp))
return result
In [3]:
from math import sqrt
def length_of_line_my3(array):
a, b = array
tmp = sum((a[i] - b[i]) ** 2 for i in range(len(array)))
result = '{:.2f}'.format(sqrt(tmp))
return result
Realize¶
- abs: 해줄 필요가 없네. 어차피 제곱이 되면 -가 모두 +로 바뀌니. 괜히 opcode만 소모하니 빼주자
- format: 1이 안 들어가도 괜찮겠다.
Best Practice¶
In [4]:
from math import sqrt
def length_of_line_best(array):
x1, y1, x2, y2 = array[0][0], array[0][1], array[1][0], array[1][1]
return '{:.2f}'.format((sqrt((x2-x1)**2 + (y2-y1)**2)))
Clever¶
In [ ]:
Time Complexity¶
In [5]:
%timeit length_of_line_my1([[0,0],[1,1]])
In [6]:
%timeit length_of_line_my2([[0,0],[1,1]])
In [7]:
%timeit length_of_line_my3([[0,0],[1,1]])
In [8]:
%timeit length_of_line_best([[0,0],[1,1]])
Graph¶
- seaborn subplot 문제를 먼저 해결하고 적용할 수 있을듯
In [ ]:
In [ ]:
In [ ]:
TestCase¶
In [1]:
# 현재 디렉토리 확인
%pwd
Out[1]:
In [7]:
# py.test를 실행시키기 위해서 잠시 프로젝트 폴더로 갔다가 다시 돌아오기
# 특정 파일을 지정해주는 이유는 tests에 많은 파일들이 쌓이게 되면 혼란스럽게 되니 딱 내가 작성했던 파일만 정확히 작동하는지 확인
%cd ..
!py.test tests/test_length_of_the_line_segment.py
%cd kata
In [ ]:
# %load ../tests/test_length_of_the_line_segment.py
import pytest
from kata.length_of_the_line_segment import (length_of_line_my1, length_of_line_my2,
length_of_line_my3, length_of_line_best)
test_data = [
([[0, 0], [1, 1]], "1.41"),
([[0, 0], [-5, -6]], "7.81"),
([[0, 0], [10, 15]], "18.03"),
([[0, 0], [5, 1]], "5.10"),
([[0, 0], [5, 4]], "6.40"),
([[0, 0], [-7, 4]], "8.06"),
([[0, 0], [0, 0]], "0.00"),
([[-3, 4], [10, 5]], "13.04"),
]
class TestClass(object):
@pytest.mark.parametrize('input_, expected', test_data)
def test_length_of_the_line_segment_my1(self, input_, expected):
assert length_of_line_my1(input_) == expected
@pytest.mark.parametrize('input_, expected', test_data)
def test_length_of_the_line_segment_my2(self, input_, expected):
assert length_of_line_my2(input_) == expected
@pytest.mark.parametrize('input_, expected', test_data)
def test_length_of_the_line_segment_my3(self, input_, expected):
assert length_of_line_my3(input_) == expected
@pytest.mark.parametrize('input_, expected', test_data)
def test_length_of_the_line_segment_best(self, input_, expected):
assert length_of_line_best(input_) == expected
Profile¶
In [ ]:
Disassemble¶
In [ ]:
In [ ]:
In [ ]: