本帖最后由 沉默的人e 于 2020-5-19 08:42 编辑
Python advanced quiz (Update from time to time)
The whole quiz is written in English.
The Chinese version is on page 2
Hello everyone. I am "dscilence", your fish oil.(fish oil...)
This is Python Quiz Question written in English. As you see, the article is written in English. (English, I hate English)
And the most important thing is that, this series of Python quiz is a little bit difficult, compared with the post named "Python 练习题(争取月更)"
So if you don't like English, or you find it hard to solve, please move to other exercises, and please don't insult this post.
The question is:
Students are asked to stand in a specific order of heights for an annual photo.
Return the minimum number of students that must move in order for all students to be standing in the order of height.
Notice that when a group of students is selected they can reorder in any possible way between themselves, and the non selected students remain on their seats.
Let me give you some examples to make you know what the specific order is.
Suppose that there are 5 students, and their heights are maked by 1,2,3,4,5.
Once they are standing like this, which is the input value:
[1,2,3,5,4]
You need to sort them and make them stand like this:
[2,4,5,3,1]
Then calculate the minimum students you move,which is the output value.
For this example, the minimum students is 5, the reason is as follow:
on index 0, we have 1 vs 2, so we need to move this student.
on index 1, we have 2 vs 4, so we need to move this student.
on index 2, we have 3 vs 5, so we need to move this student.
on index 3, we have 5 vs 3, so we need to move this student.
on index 4, we have 4 vs 1, so we need to move this student.
本帖最后由 咸鱼c1 于 2020-5-19 16:19 编辑
我之前可能想复杂了   
先排个序然后分成两个列表,一个奇数位置的,一个偶数位置的,然后奇数位置的翻转,再拼接起来就是答案了,最后和源列表对比一下就行
- def fun(list1):
- n=len(list1)
- list2=list1[:]
- list2.sort()
- temp1=list2[0::2]
- temp2=list2[1::2]
- temp1.sort(reverse=True)
- ret=temp2+temp1
- cnt=0
- for i in range(n):
- if ret[i]!=list1[i]:
- cnt+=1
- return cnt
复制代码
|