Exercise 12.2. In this example, ties are broken by comparing words, so words with the same length appear in reverse alphabetical order.
For other applications you might want to break ties at random.
Modify this example so that words with the same length appear in random order.
Hint: see the random function in the random module.
1
manoon 2015-02-08 12:13:03 +08:00 via Android 1
words with the same length appear in random order.
Hint: see the random function in the random module. 上一节是练习使用升序/降序 这一节,你就先找出len相同的words,然后随机排序就行啦。 (愚见,仅供参考) |
2
zeroday OP @manoon 谢谢,不理解 ties are broken by comparing words 和For other applications you might want to break ties at random. 主要是不理解 ties break 的意思。
|
3
zeroday OP @manoon 奇怪的是,上一节练习的例子是根据列表中单词的长度,进行降序排列,代码是这样的。
https://gist.github.com/arcticlion/f75f89fd1c0ff4ef1cde 运行结果是这样的: wooooo applee Harlod Finich world tuple hello apple nba ibm 对于相同的长度的单词,打印出现的顺序本来就是随机。 |
4
zeroday OP 突然明白了,上一例在相同长度上的输出虽然不是按字母表顺序,但是每次输出都是固定的。练习需要我修改上一例的代码,让每次在相同长度上的输出是不固定的。
|