티스토리 뷰
A zigzag sorted sequence of integers is a sequence where
In other words, the sequence alternates between larger and smaller integers: the first is larger than or equal to the second, the second is smaller than or equal to the third, the third is larger than or equal to the fourth, and so on. Here's an example of such a zigzag sorted sequence:
![zigzag zigzag](https://dodona.ugent.be/exercises/2038783829/media/zigzag.png)
Given an unsorted sequence of integers, a simple way to rearrange the integers into a zigzag sorted sequence is to use sorting. First sort the integers in the sequence in increasing order, then swap each non-overlapping pair of adjacent integers. For example, let the original sequence be [10, 90, 49, 2, 1, 5, 23]. After sorting, we get [1, 2, 5, 10, 23, 49, 90]. After swapping adjacent elements, we get [2, 1, 10, 5, 49, 23, 90].
![zigzag sort (slow) zigzag sort (slow)](https://dodona.ugent.be/exercises/2038783829/media/zigzag_slow.png)
However, there's a faster way to zigzag sort a given sequence of integers that only requires a single traversal over the elements of the sequence. The idea is based on the fact that if we make sure that all even-positioned elements (at indices 0, 2, 4, …) are greater than or equal to their adjacent odd-positioned elements, we do no longer need to worry about the odd-positioned elements. All it requires is to traverse the even positions from left to right, and on each position execute the following two steps one after the other:
if the current element at the even position is less than the element on the previous odd position (if that position exists), swap both elements
if the current element at the even position is less than the element on the next odd position (if that position exists), swap both elements
If we apply this method to the above example, we get
![zigzag sort (fast) zigzag sort (fast)](https://dodona.ugent.be/exercises/2038783829/media/zigzag_fast.png)
Assignment
Write a function isZigzag that takes a list or a tuple of integers. The function must return a Boolean value that indicates whether or not the given list or tuple of integers is zigzag sorted.
Write a function zigzagSlow that takes a list of integers. The function must zigzag sort the given list of integers using the slow method described above. The function should not return a value, but must modify the given list in place.
Write a function zigzagFast that takes a list of integers. The function must zigzag sort the given list of integers using the fast method described above. The function should not return a value, but must modify the given list in place.
Example
>>> isZigzag([10, 5, 6, 3, 2, 20, 100, 80]) False >>> isZigzag((10, 5, 6, 2, 20, 3, 100, 80)) True >>> isZigzag([20, 5, 10, 2, 80, 6, 100, 3]) True >>> seq = [10, 90, 49, 2, 1, 5, 23] >>> zigzagSlow(seq) >>> seq [2, 1, 10, 5, 49, 23, 90] >>> seq = [10, 90, 49, 2, 1, 5, 23] >>> zigzagFast(seq) >>> seq [90, 10, 49, 1, 5, 2, 23]
def isZigzag(nums):
for i, num in enumerate(nums):
if i % 2 == 0:
idx_prev = i - 1
idx_next = i + 1
is_prev = idx_prev >= 0
is_next = idx_next < len(nums)
if is_prev and num < nums[idx_prev]:
return False
if is_next and num < nums[idx_next]:
return False
return True
def zigzagSlow(nums):
nums.sort()
for i, num in enumerate(nums):
if i % 2 == 0:
idx_next = i + 1
is_next = idx_next < len(nums)
if is_next and num < nums[idx_next]:
nums[i], nums[idx_next] = nums[idx_next], nums[i]
def zigzagFast(nums):
SIZE = len(nums)
for _ in range(SIZE - 1):
for i in range(SIZE):
if i % 2 == 0:
idx_prev = i - 1
idx_next = i + 1
is_prev = idx_prev >= 0
is_next = idx_next < SIZE
if is_prev and nums[i] < nums[idx_prev]:
nums[i], nums[idx_prev] = nums[idx_prev], nums[i]
if is_next and nums[i] < nums[idx_next]:
nums[i], nums[idx_next] = nums[idx_next], nums[i]
seq = [10, 90, 49, 2, 1, 5, 23]
zigzagFast(seq)
print(seq)
'python lecture > algorism' 카테고리의 다른 글
[edu] Colorful fruits (0) | 2018.10.24 |
---|---|
[eud] bubble sorting (0) | 2018.10.15 |
[edu] Kaprekar series (0) | 2018.10.14 |
[edu] Transitions and transversions (0) | 2018.10.08 |
[edu] Word sums (0) | 2018.10.08 |
- Total
- Today
- Yesterday
- 면접답변
- Tistory
- 파이썬 독학
- 모바일 테마 적용
- django
- 파이썬 프로그래밍
- 파이썬
- 문과 코딩
- pycrypto
- 장고 플러스친구 자동응답
- 문서 비교
- gitlab
- virtualenv
- gitignore
- django chatbot
- 장고
- 파이썬 입문
- PuTTYGen
- GIT
- wsgi
- admin.py
- 플러스친구 자동응답
- 장고 카톡 자동응답
- chatbot
- Python
- 이미지 비교
- 면접정답
- 파이썬 강좌
- 엑셀 비교
- 모바일 스킨 적용
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |