Answers:
","/.,{{.2$<{\}*}*]}*","*
冒泡排序的直接实现。
在Web GolfScript中在线尝试。
","/ # Split the input string at commas.
., # Get the number of chunks.
{ # Do that many times:
{ # Reduce; for each element but the first:
.2$< # Push 1 if the last two strings are in descending order, 0 if not.
{\}* # Swap these strings that many times.
}*] # Collect the strings dumped by reduce in an array.
}* #
","* # Join, separating by commas.
可能并没有真正实现问题的精神。在k中,没有内置的排序运算符。<x
以排序顺序返回x的项目索引列表。
{x@<x}[","\:0:0]
a=gets.scan /\w+/
p=1
while a[p]
a[p]>a[p-1]?p+=2:(a[p],a[p-1]=a[p-1],a[p])
p-=1if p>1
end
$><<a*?,
这几乎不超过我的冒泡排序实现:
s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,
这会进行list.length
迭代,因为最坏的情况下需要list.length - 1
迭代,而再进行一次则无关紧要,并节省了2个字符。
只是为了好玩,Quicksort版本:
q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,
import Data.List
m=minimum
s[]=[]
s l=m l:s(l\\[m l])
t[]=[]
t s=let(a,b)=span(/=',')s in a:t(drop 1 b)
main=interact$intercalate",".s.t.init
至少这是...... 排序的效率。
m=minimum
s[]=[]
s l=m l:(s$l\\[m l])
将这些行2–4替换为这些行)。
init
似乎并不一样,没有拖尾是必要的,
,也没有一个结尾的新行。t s=let(a,b)=span(/=',')s in a:t(drop 1 b)
可以通过以下方式缩短:使用模式卫士,(>',')
并减少之间的距离1 b
:t s|(a,b)<-span(>',')s=a:t(drop 1b)
。
Sub q()
c=","
s=InputBox("?")
Z=Split(s, c)
t=UBound(Z)
For i=1 To t-1
For j=i To t
If Z(i)>Z(j) Then a=Z(i):Z(i)=Z(j):Z(j)=a
Next
Next
Debug.Print Join(Z,c)
End Sub
Split
。
c=","
和调用c
两次实际上会增加字节数,从而为字节数贡献7个字节,而就像使用“,”两次将贡献6个字节。您可以通过直接从子调用(sub q(s)
)中获取输入并假定s为variant \ string类型来降低字节码。更改For i=1 to
为可以再丢失一个字节for i=1To
。通过更改Debug.Print Join...
为Debug.?Join...
.permutations.filter(_.sliding(2).map(w=>w(0)<w.last).fold(true)((a,b)=>a&&b)).toSeq(0)
(它将仅通过进行排序list.permutations.fil...
)
println(readLine.split(",").toSeq.permutations.filter(_.sliding(2).map(w=>w(0)<w.last).fold(true)((a,b)=>a&&b)).toSeq(0))
如果您想从标准输入中读取更长的版本。
这会遍历给定列表的所有排列,直到偶然发现排序的列表为止。它不是很快,因为对10个元素的列表进行排序大约需要12秒,而对11个元素的列表要花费一分钟以上的时间。
[编辑]项必须是唯一的或<
可以替换为<=
。此外,对不起,死灵。
a=prompt().split(',');b=[];for(i in a){b.push(a[i]);k=0;for(j in b){j=k;b[j]>a[i]?[c=b[j],b.splice(j,1),b.push(c)]:k++}}alert(b)
示范摆弄。
我正在寻找一种消除的方法b
。
[]
后删除周围的部分?
SyntaxError: missing : in conditional expression
因为?:;
(速记if/else
)只应该使用两段代码来执行(即true?b++:b--;
)使用[
,这]
是一个hack,我仍然不确定为什么会工作,我认为其理解为空数组声明,例如将随机字符串或数字作为独立命令放置。但您仍然可以随意投票。
?:
运算符的优先级低于,
l=input().split(',')
m=[]
while l:m+=[l.pop(l.index(min(l)))]
print(','.join(m))
这是while陈述的等长变体:
while l:x=min(l);m+=[x];l.remove(x)
Row[#[[Ordering@#]]&[InputString[]~StringSplit~","],","]
其他一些没有内置符号的解决方案Ordering
:
NestWhile[RandomSample,InputString[]~StringSplit~",",!OrderedQ@#&]~Row~","
Row[InputString[]~StringSplit~","//.{x___,i_,j_,y___}/;j~Order~i==1:>{x,j,i,y},","]
#~Row~","&/@Permutations[InputString[]~StringSplit~","]~Select~OrderedQ;
这从Steven Rumbalski的答案中获得启发,但使用列表理解而不是while循环。迭代次数来自复制的列表l
,这就是需要附加拆包概括和Python 3.5的原因
l=input().split(',');print(*[l.pop(l.index(min(l)))for _ in[*l]],sep=',')
perl -F"," -lape "$m=$m<length()?length():$m for@F;$_{10**(2*$m)*sprintf'0.'.'%02d'x$m,map-96+ord,split//}=$_ for@F;$_=join',',map$_{$_+0},grep exists$_{$_+0},'0'.1..'0'.10**100"
从来没有机会赢过,但是决定分享它,因为即使逻辑很混乱我也喜欢逻辑:)它背后的想法是将每个单词转换成整数(使用ord函数完成),我们保存数字作为哈希中的键,并将字符串作为值,然后我们逐步遍历所有整数(在这种情况下为1.0.10 ** 100),然后对字符串进行排序。
警告:请勿在您的计算机上运行此代码,因为它会循环遍历数万亿个整数。如果要测试,可以降低范围上限并输入非冗长的字符串。如果出于任何原因这违反规则,请告诉我,我将删除该条目!
func s(a:[String])->[String]{return a.count<2 ? a:(s(a.filter{$0<a[0]})+[a[0]]+s(a.filter{$0>a[0]}))}
取消高尔夫:
//quicksort
func sort(a:[String]) -> [String]
{
//return the array if its length is less than or equal to 1
if a.count <= 1
{
return a
}
//choose the first element as pivot
let pivot = a[0]
//retrieve all elements less than the pivot
let left = a.filter{ $0 < pivot }
//retrieve all elements greater than the pivot
let right = a.filter{ $0 > pivot }
//sort the left partition, append a new array containing the pivot,
//append the sorted right partition
return sort(left) + Array<String>(arrayLiteral: pivot) + sort(right)
}
ï⇔Ĕ⍪;↻ïꝈ)ΞÿѨŗ ï,⇀$≔МƵï;Ξ
使用选择排序!
ï⇔Ĕ⍪;↻ïꝈ)ΞÿѨŗ ï,⇀$≔МƵï;Ξ // implicit: ï=input, Ξ=[]
ï⇔Ĕ⍪; // split ï along commas and set it to ï
↻ïꝈ) // while ï's length > 0
Ξÿ // push to Ξ:
Ѩŗ ï,⇀$≔МƵï; // removed minimum item(s) from ï using builtin
Ξ // get sorted array
基本上以递归方式从输入中删除最小值并将其推入另一个数组。
String s(String i)=>",".join([*i.split(','.equals)].permutations.select((p)=>!any{for([x,y]in p.paired)y<x})[0]else[]);
我找到了permutations
方法,最终得到了Bogosort(一种非随机变体)。
格式和评论:
// a function `s` mapping a String `i` to a String
String s(String i) =>
// the end result is created by joining the iterable in (...).
",".join(
// take the input, split it on commas, make the result a sequence.
[*
i.split(','.equals) // → {String+}
] // → [String+]
// get the iterable of all permutations of this sequence.
// Yes, this is an iterable of O(n!) sequences (though likely
// lazily computed, we don't need all in memory at once).
.permutations // → {[String+]*}
// filter this iterable for ordered sequences.
// Using select instead of filter makes this
// eager instead of lazy, so we are actually iterating
// through all n! sequences, and storing the ordered
// ones. (All of those are equal.)
.select(
// this is our function to check whether this sequence
// is ordered in ascending order.
(p)=>
// return if none of the following iterable of booleans is true.
!any {
// This is a for-comprehension. Inside an named argument list
// (what we have here, although there is no name) for a
// function which wants an iterable, this becomes an iterable,
// lazily built from the existing iterable p.paired,
// which is just an iterable with all pairs of subsequent
// elements.
for([x,y] in p.paired)
// for each such pair, we evaluate this expression, which
// is true when the sequence is not ordered correctly.
y < x // → Boolean
// → {Boolean*}
} // → Boolean
// → Boolean([String+])
) // → [[String+]*]
// we now have a sequence of (correctly sorted) sequences.
// just take the first one.
// If we had used `.filter` before, this would have to be `.first`.
[0] // → [String+]|Null
// in case this is null, which can only happen if the original array was
// empty, so there were no permutations, just use the empty sequence
// again. (Actually, split never returns an empty array, so this can't
// happen, but the type checker can't know that.)
else [] // → [String*]
// so that is what we pass to the join method.
) // → String
;
如果不进行格式化和解析,它将变成90字节:
String[]s(String[]i)=>i.permutations.select((p)=>!any{for([x,y]in p.paired)y<x})[0]else[];
ruby -plaF,
,70字节o=[]
$F.map{|s|i=o;s.bytes{|b|i=i[b]=[*i[b]]};i[0]=s<<?,}
$_=o*''
chop
O(n),如果您假装调整数组大小和压缩是免费的(这不是免费的)。
我们o
通过在位置o [b 1 ] [b 2 ] ... [b n ] 处将字节为b 1,b 2 ... b n的字符串放入数组中,来创建一个深层且嵌套不均匀的数组。结果看起来像[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,["a,",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ["abc,"], ["abd,"], ["abe,"]], ["ac,"], ["ad,"]],, ["c,"]]
然后我们将其展平并输出。