Новый (в 2014 году) tidyr
пакет также делает это просто, при этом gather()
/ spread()
является слагаемыми для melt
/ cast
.
library(tidyr)
spread(dat1, key = numbers, value = value)
Из github ,
tidyr
- это перерисовкаreshape2
, предназначенная для сопровождения аккуратной структуры данных и работающая рука об руку сmagrittr
иdplyr
, чтобы построить сплошной конвейер для анализа данных.Так же, как
blockquote>reshape2
сделал меньше, чем изменение,tidyr
делает меньше, чемreshape2
. Он разработан специально для сбора данных, а не для общей перестройки, которую делаетreshape2
, или общего преобразования, которое изменилось. В частности, встроенные методы работают только для кадров данных, аtidyr
не содержит полей или агрегации.
Вот несколько ошибок в вашем коде, и я переписал код для вас. Вы можете обратиться к комментарию и выполнить тест, чтобы выяснить это.
import random
# two pointers solution, choose the last one as pivot
def partition(nums, left, right, pivot):
while left < right:
# here should change < to <=, because if pivot is larger than all in nums[left:right+1], should swap nums[left] with nums[pivot]
# here I change if to while, because you should traverse the pointer to find the place which is invalid in partition
while left <= right and nums[left] <= nums[pivot]:
left += 1
# here I change if to while, same reason above
while left < right and nums[right] > nums[pivot]:
right -= 1
# you should not use elif here, because you have two ifs, the first if does not work here
if left < right:
nums[left], nums[right] = nums[right], nums[left]
nums[left], nums[pivot] = nums[pivot], nums[left]
return left
def quicksort(nums, low, high, pivot):
if low < high:
pos = partition(nums, low, high, pivot)
quicksort(nums, low, pos - 2, pos - 1)
# here is another problem: notice that nums[pivot] is the last element, not the nums[high]
quicksort(nums, pos + 1, high, high + 1)
return nums
if __name__ == '__main__':
for _ in range(100):
nums = [random.randrange(1, 100) for _ in range(10000)]
n = len(nums)
if sorted(nums) != quicksort(nums, 0, n - 2, n - 1):
print('incorrect')
print('success')
Я старался изо всех сил, чтобы помочь вам, и надеюсь, вам понравится.