Last active
September 21, 2019 08:11
-
-
Save lbvf50mobile/185fddd2b73bfe9f25a5c18227afab1b to your computer and use it in GitHub Desktop.
Pendulum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kata | |
import "sort" | |
// https://www.codewars.com/kata/the-poet-and-the-pendulum/train/go | |
func Pendulum(values []int) []int { | |
srt := append([]int{},values...) | |
answer := make([]int,0,0) | |
sort.Ints(srt) | |
for key, value := range srt { | |
if 0 == key%2 { | |
answer = append([]int{value}, answer...) | |
}else{ | |
answer = append(answer, value) | |
} | |
} | |
return answer | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.codewars.com/kata/the-poet-and-the-pendulum/train/javascript | |
function pendulum(values) { | |
let sorted = [...values].sort((a,b) => a - b); | |
let x = values.length % 2 ? parseInt(values.length/2) : parseInt((values.length - 1)/2); | |
values[x] = sorted[0] | |
for(var i=1, delta = i; i < sorted.length; i+=1){ | |
values[x + delta] = sorted[i] | |
delta = delta > 0 ? -delta : -delta + 1 | |
} | |
return values | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.codewars.com/kata/the-poet-and-the-pendulum/train/python | |
# Did not passed by time bounds | |
def pendulum(v): | |
answer = [] | |
v.sort() | |
for key, value in enumerate(v): | |
answer.insert(0,value) if 0 == key%2 else answer.append(value) | |
return answer | |
# http://book.pythontips.com/en/latest/ternary_operators.html | |
# https://www.python-course.eu/python3_list_manipulation.php | |
# https://stackoverflow.com/a/34211482/8574922 | |
# Passed | |
# https://www.codewars.com/kata/the-poet-and-the-pendulum/train/python | |
def pendulum(v): | |
srt = v.copy() | |
srt.sort() | |
x = int(len(v)/2) if 1 == len(v)%2 else int((len(v) - 1)/2) | |
v[x] = srt[0] | |
i,delta = 1,1 | |
while i < len(v): | |
v[x + delta] = srt[i] | |
delta = -delta if delta > 0 else -delta + 1 | |
i += 1 | |
return v | |
# http://book.pythontips.com/en/latest/ternary_operators.html | |
# https://www.python-course.eu/python3_list_manipulation.php | |
# https://stackoverflow.com/a/34211482/8574922 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.codewars.com/kata/the-poet-and-the-pendulum/train/ruby | |
def pendulum v | |
l,r = [],[] | |
v.sort.each_with_index do |value, index| | |
index.even? ? l.push(value) : r.push(value) | |
end | |
l.reverse + r | |
end | |
# https://www.codewars.com/kata/the-poet-and-the-pendulum/train/ruby | |
# https://www.codewars.com/users/c0nspiracy | |
def pendulum v | |
cycle = %i[unshift push].cycle | |
v.sort.reduce([]) do |memo, value| | |
memo.send(cycle.next, value) | |
end | |
end |
https://www.codewars.com/users/FArekkusu
def pendulum(a):
a = sorted(a)
return a[::2][::-1] + a[1::2]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.codewars.com/users/cave.on