length of the line segment

length-of-the-line-segment

Problem

In [4]:
# package로 인식을 못해서 어쩔 수 없이 이렇게 import함
%cd ..
from helper.war import Kata
%cd kata/
/Users/re4lfl0w/Documents/_git/codewars
/Users/re4lfl0w/Documents/_git/codewars/kata
In [5]:
kata = Kata()
2016-10-04 04:29:07: helper.war - INFO - Start func: set_access_key
INFO:helper.war:Start func: set_access_key
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)
2016-10-04 04:29:08: helper.war - INFO - Start func: get_description_in_code_challenge
INFO:helper.war:Start func: get_description_in_code_challenge
2016-10-04 04:29:08: helper.war - INFO - Start func: set_code_challenge
INFO:helper.war:Start func: set_code_challenge
2016-10-04 04:29:08: helper.war - INFO - Start func: get_cleaned_challenge
INFO:helper.war:Start func: get_cleaned_challenge
2016-10-04 04:29:08: helper.war - INFO - challenge: length-of-the-line-segment
INFO:helper.war:challenge: length-of-the-line-segment
2016-10-04 04:29:09: helper.war - INFO - challenge_json: {'unresolved': {'issues': 0, 'suggestions': 0}, 'createdAt': '2015-09-10T12:32:53Z', 'description': 'Find the length between 2 co-ordinates.  The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:\n\n(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]\n\nThe function must return the answer rounded to 2 decimal places in the form of a string.\n\n```javascript\nlengthOfLine([ [ 0 , 0 ] , [ 5, -7 ] ]); => "8.60"\n```\n```python\nlength_of_line([[0, 0], [5, -7]]) => "8.60"\n```\n```ruby\nlength_of_line([[0, 0], [5, -7]]) # => "8.60"\n```\n\nIf the 2 given co-ordinates are the same, the returned length should be "0.00" \n', 'voteScore': 50, 'totalStars': 14, 'slug': 'length-of-the-line-segment', 'contributorsWanted': True, 'createdBy': {'url': 'https://www.codewars.com/users/JMurphyWeb', 'username': 'JMurphyWeb'}, 'approvedBy': {'url': 'https://www.codewars.com/users/Balkoth', 'username': 'Balkoth'}, 'id': '55f1786c296de4952f000014', 'tags': ['Fundamentals', 'Mathematics', 'Algorithms', 'Logic', 'Numbers', 'Geometry', 'Algebra'], 'category': 'reference', 'languages': ['javascript', 'python', 'ruby'], 'totalCompleted': 785, 'totalAttempts': 1805, 'publishedAt': '2015-09-10T12:32:44Z', 'name': 'Length of the line segment', 'url': 'https://www.codewars.com/kata/length-of-the-line-segment', 'approvedAt': '2015-10-07T18:32:14Z', 'rank': {'id': -7, 'name': '7 kyu', 'color': 'white'}}
INFO:helper.war:challenge_json: {'unresolved': {'issues': 0, 'suggestions': 0}, 'createdAt': '2015-09-10T12:32:53Z', 'description': 'Find the length between 2 co-ordinates.  The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:\n\n(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]\n\nThe function must return the answer rounded to 2 decimal places in the form of a string.\n\n```javascript\nlengthOfLine([ [ 0 , 0 ] , [ 5, -7 ] ]); => "8.60"\n```\n```python\nlength_of_line([[0, 0], [5, -7]]) => "8.60"\n```\n```ruby\nlength_of_line([[0, 0], [5, -7]]) # => "8.60"\n```\n\nIf the 2 given co-ordinates are the same, the returned length should be "0.00" \n', 'voteScore': 50, 'totalStars': 14, 'slug': 'length-of-the-line-segment', 'contributorsWanted': True, 'createdBy': {'url': 'https://www.codewars.com/users/JMurphyWeb', 'username': 'JMurphyWeb'}, 'approvedBy': {'url': 'https://www.codewars.com/users/Balkoth', 'username': 'Balkoth'}, 'id': '55f1786c296de4952f000014', 'tags': ['Fundamentals', 'Mathematics', 'Algorithms', 'Logic', 'Numbers', 'Geometry', 'Algebra'], 'category': 'reference', 'languages': ['javascript', 'python', 'ruby'], 'totalCompleted': 785, 'totalAttempts': 1805, 'publishedAt': '2015-09-10T12:32:44Z', 'name': 'Length of the line segment', 'url': 'https://www.codewars.com/kata/length-of-the-line-segment', 'approvedAt': '2015-10-07T18:32:14Z', 'rank': {'id': -7, 'name': '7 kyu', 'color': 'white'}}
Find the length between 2 co-ordinates.  The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:

(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]

The function must return the answer rounded to 2 decimal places in the form of a string.

```javascript
lengthOfLine([ [ 0 , 0 ] , [ 5, -7 ] ]); => "8.60"
```
```python
length_of_line([[0, 0], [5, -7]]) => "8.60"
```
```ruby
length_of_line([[0, 0], [5, -7]]) # => "8.60"
```

If the 2 given co-ordinates are the same, the returned length should be "0.00" 

In [2]:
abs(-2-(-7))
Out[2]:
5
In [3]:
abs(8-(-5))
Out[3]:
13

Logic

  • 좌표평면에서 두 점 사이의 거리 | 수학방
    • 영어로 써져 있어서 엄청 어려워 보이는데 별거 없다. 그냥 중학교때 배운 좌표평면에서 두 점 사이의 거리 구하는 공식이다...
  • 첫번째 x값 - 두번째 x값
  • 첫번째 y값 - 두번째 y값
  • 그 결과값을 더하고
  • root 취해준다.
  • 이게 뭘 하는건지는 확실하지 않다...
  • 일단 로직이 이렇게 된다는 것을 알 수 있다.

    Read more…

ch01

1. 자료구조와 알고리즘

  • start: 2014-09-30
  • end: 2014-10-02

Python3

정의

  • 파이썬은 list, set, dictionary 같이 유용한 자료 구조 내장
  • 이러한 구조체의 장점은 사용이 편리
  • 검색, 정렬, 순서, 여과 등에 대한 질문이 종종 생김

1.1 시퀀스를 개별 변수로 나누기

문제

  • N개의 요소를 가진 튜플이나 시퀀스가 있다. 이를 변수 N개로 나누어야 한다.

해결

  • 모든 시퀀스(혹은 이터레이팅 가능한 것)는 간단한 할당문을 사용해서 개별 변수로 나눔
  • 주의사항: 변수의 개수가 시퀀스에 일치해야 한다는 것뿐

    Read more…