I'm trying to make an easy script in Python which takes a number and saves in a variable, sorting the digits in ascending and descending orders and saving both in separate variables. Implementing Kaprekar's constant.
我正在尝试在Python中创建一个简单的脚本,它接受一个数字并保存在一个变量中,按升序和降序对数字进行排序,并将两者保存在单独的变量中。实施Kaprekar的常数。
It's probably a pretty noobish question. But I'm new to this and I couldn't find anything on Google that could help me. A site I found tried to explain a way using lists, but it didn't work out very well.
这可能是一个相当愚蠢的问题。但我是新手,我在Google上找不到任何可以帮助我的东西。我发现的一个网站试图解释一种使用列表的方式,但它并没有很好地解决问题。
5 个解决方案
#1
12
Sort the digits in ascending and descending orders:
按升序和降序对数字排序:
ascending = "".join(sorted(str(number)))
descending = "".join(sorted(str(number), reverse=True))
Like this:
>>> number = 5896
>>> ascending = "".join(sorted(str(number)))
>>>
>>> descending = "".join(sorted(str(number), reverse=True))
>>> ascending
'5689'
>>> descending
'9865'
And if you need them to be numbers again (not just strings), call int()
on them:
如果你需要它们再次成为数字(而不仅仅是字符串),请在它们上调用int():
>>> int(ascending)
5689
>>> int(descending)
9865
#2
3
>>> x = [4,5,81,5,28958,28] # first list
>>> print sorted(x)
[4, 5, 5, 28, 81, 28958]
>>> x
[4, 5, 81, 5, 28958, 28]
>>> x.sort() # sort the list in place
>>> x
[4, 5, 5, 28, 81, 28958]
>>> x.append(1) # add to the list
>>> x
[4, 5, 5, 28, 81, 28958, 1]
>>> sorted(x)
[1, 4, 5, 5, 28, 81, 28958]
As many others have pointed out, you can sort a number forwards like:
正如许多其他人所指出的那样,你可以对前面的数字进行排序,例如:
>>> int(''.join(sorted(str(2314))))
1234
That's pretty much the most standard way.
这几乎是最标准的方式。
Reverse a number? Doesn't work well in a number with trailing zeros.
扭转一个数字?在尾随零的数字中不能很好地工作。
>>> y = int(''.join(sorted(str(2314))))
>>> y
1234
>>> int(str(y)[::-1])
4321
The [::-1]
notation indicates that the iterable is to be traversed in reverse order.
[:: - 1]表示法表示将以相反的顺序遍历iterable。
#3
2
As Mark Rushakoff already mentioned (but didn't solve) in his answer, str(n)
doesn't handle numeric n
with leading zeros, which you need for Kaprekar's operation. hughdbrown's answer similarly doesn't work with leading zeros.
正如Mark Rushakoff在他的回答中已经提到过(但没有解决),str(n)不处理带有前导零的数字n,这是Kaprekar操作所需要的。 hughdbrown的答案同样不适用于前导零。
One way to make sure you have a four-character string is to use the zfill
string method. For example:
确保使用四字符字符串的一种方法是使用zfill字符串方法。例如:
>>> n = 2
>>> str(n)
'2'
>>> str(n).zfill(4)
'0002'
You should also be aware that in versions of Python prior to 3, a leading zero in a numeric literal indicated octal:
您还应该知道,在3之前的Python版本中,数字文字中的前导零表示八进制:
>>> str(0043)
'35'
>>> str(0378)
File "<stdin>", line 1
str(0378)
^
SyntaxError: invalid token
In Python 3, 0043
is not a valid numeric literal at all.
在Python 3中,0043根本不是有效的数字文字。
#4
1
I don't know the python syntax, but thinking the generically, I would convert the input string into a character array, they do a sort on the character array, and lastly pipe it out.
我不知道python的语法,但一般来说,我会将输入字符串转换为字符数组,它们对字符数组进行排序,最后将其输出。
#5
0
Here's an answer to the title question in Perl, with a bias toward sorting 4-digit numbers for the Kaprekar algorithm. In the example, replace 'shift' with the number to sort. It sorts digits in a 4-digit number with leading 0's ($asc is sorted in ascending order, $dec is descending), and outputs a number with leading 0's:
这是Perl中标题问题的答案,偏向于对Kaprekar算法的4位数字进行排序。在示例中,将“shift”替换为要排序的数字。它将4位数字中的数字排序为前导0($ asc按升序排序,$ dec下降),并输出前导0的数字:
my $num = sprintf("%04d", shift);
my $asc = sprintf("%04d", join('', sort {$a <=> $b} split('', $num)));
my $dec = sprintf("%04d", join('', sort {$b <=> $a} split('', $num)));
#1
12
Sort the digits in ascending and descending orders:
按升序和降序对数字排序:
ascending = "".join(sorted(str(number)))
descending = "".join(sorted(str(number), reverse=True))
Like this:
>>> number = 5896
>>> ascending = "".join(sorted(str(number)))
>>>
>>> descending = "".join(sorted(str(number), reverse=True))
>>> ascending
'5689'
>>> descending
'9865'
And if you need them to be numbers again (not just strings), call int()
on them:
如果你需要它们再次成为数字(而不仅仅是字符串),请在它们上调用int():
>>> int(ascending)
5689
>>> int(descending)
9865
#2
3
>>> x = [4,5,81,5,28958,28] # first list
>>> print sorted(x)
[4, 5, 5, 28, 81, 28958]
>>> x
[4, 5, 81, 5, 28958, 28]
>>> x.sort() # sort the list in place
>>> x
[4, 5, 5, 28, 81, 28958]
>>> x.append(1) # add to the list
>>> x
[4, 5, 5, 28, 81, 28958, 1]
>>> sorted(x)
[1, 4, 5, 5, 28, 81, 28958]
As many others have pointed out, you can sort a number forwards like:
正如许多其他人所指出的那样,你可以对前面的数字进行排序,例如:
>>> int(''.join(sorted(str(2314))))
1234
That's pretty much the most standard way.
这几乎是最标准的方式。
Reverse a number? Doesn't work well in a number with trailing zeros.
扭转一个数字?在尾随零的数字中不能很好地工作。
>>> y = int(''.join(sorted(str(2314))))
>>> y
1234
>>> int(str(y)[::-1])
4321
The [::-1]
notation indicates that the iterable is to be traversed in reverse order.
[:: - 1]表示法表示将以相反的顺序遍历iterable。
#3
2
As Mark Rushakoff already mentioned (but didn't solve) in his answer, str(n)
doesn't handle numeric n
with leading zeros, which you need for Kaprekar's operation. hughdbrown's answer similarly doesn't work with leading zeros.
正如Mark Rushakoff在他的回答中已经提到过(但没有解决),str(n)不处理带有前导零的数字n,这是Kaprekar操作所需要的。 hughdbrown的答案同样不适用于前导零。
One way to make sure you have a four-character string is to use the zfill
string method. For example:
确保使用四字符字符串的一种方法是使用zfill字符串方法。例如:
>>> n = 2
>>> str(n)
'2'
>>> str(n).zfill(4)
'0002'
You should also be aware that in versions of Python prior to 3, a leading zero in a numeric literal indicated octal:
您还应该知道,在3之前的Python版本中,数字文字中的前导零表示八进制:
>>> str(0043)
'35'
>>> str(0378)
File "<stdin>", line 1
str(0378)
^
SyntaxError: invalid token
In Python 3, 0043
is not a valid numeric literal at all.
在Python 3中,0043根本不是有效的数字文字。
#4
1
I don't know the python syntax, but thinking the generically, I would convert the input string into a character array, they do a sort on the character array, and lastly pipe it out.
我不知道python的语法,但一般来说,我会将输入字符串转换为字符数组,它们对字符数组进行排序,最后将其输出。
#5
0
Here's an answer to the title question in Perl, with a bias toward sorting 4-digit numbers for the Kaprekar algorithm. In the example, replace 'shift' with the number to sort. It sorts digits in a 4-digit number with leading 0's ($asc is sorted in ascending order, $dec is descending), and outputs a number with leading 0's:
这是Perl中标题问题的答案,偏向于对Kaprekar算法的4位数字进行排序。在示例中,将“shift”替换为要排序的数字。它将4位数字中的数字排序为前导0($ asc按升序排序,$ dec下降),并输出前导0的数字:
my $num = sprintf("%04d", shift);
my $asc = sprintf("%04d", join('', sort {$a <=> $b} split('', $num)));
my $dec = sprintf("%04d", join('', sort {$b <=> $a} split('', $num)));