【问题标题】:Some test cases are failing. Tell me what went wrong一些测试用例失败了。告诉我出了什么问题
【发布时间】:2020-02-10 04:38:31
【问题描述】:

给定物理课学生中每个学生的姓名和成绩,将它们存储在嵌套列表中,并打印任何成绩第二低的学生的姓名。

注意:如果有多个年级相同的学生,请按字母顺序排列他们的姓名并将每个姓名打印在一个新行上。

输入格式

第一行包含一个整数,学生人数。 随后的几行逐行描述了每个学生;第一行包含学生的姓名,第二行包含他们的成绩。

约束

总会有一名或多名学生成绩倒数第二。 输出格式

打印物理成绩第二低的任何学生的姓名;如果有多个学生,请按字母顺序排列他们的姓名,并将每个学生都打印在新行上。

样本输入 0

5 哈利 37.21 浆果 37.21 蒂娜 37.2 阿克里蒂 41 残酷的 39 样本输出 0

贝瑞 哈利 解释0

这个班级有学生的姓名和成绩被汇总以构建以下列表:

python 学生 = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

最低等级的属于蒂娜。排名倒数第二的 属于 Harry 和 Berry,因此我们按字母顺序排列他们的名字并将每个名字打印在新的一行上。

我的代码: **

marksheet=[]
for _ in range(int(raw_input())):
    stud=[]
    name = raw_input()
    score = float(raw_input())
    stud.append(name)
    stud.append(score)
    marksheet.append(stud)
marksheet.sort(key= lambda x:x[0])
minimum = min(marksheet,key = lambda x:x[1])
for x in marksheet:
    if x[1]== minimum[1]:
        marksheet.remove(x)
sec_high=min(marksheet, key= lambda x :x[1]  )
for x in marksheet:
    if x[1] == sec_high[1]:
        print x[0]

**

【问题讨论】:

  • 首先你为什么不做marksheet.append((name, score))你不需要构建一个螺柱列表。进一步 for _ in range(int(raw_input()) 似乎是错误的,这是否有效?很可能你会得到一个 ValueError。
  • 它是 python 2.7,for 循环工作得很好。 10 个测试用例中有 8 个正在工作。问题链接:hackerrank.com/challenges/nested-list/problem
  • 作为初学者,您为什么要使用 python 2.7?它将在 4 个月内变得不受支持并过时。我强烈建议迁移到 python 3.7
  • 好的,先生,感谢您的帮助。

标签: python list nested-lists


【解决方案1】:

可行的解决方案(在 Hackerrank 上测试)

# Gather Inputs
n = int(raw_input())
lst = []
for x in range(n):
    lst.append([raw_input(), float(raw_input())])

# Sort by score (i.e. sublists are [name, score])
lst = sorted(lst, key=lambda x: x[1])

# Find second lowest score
for x in range(1, n):
    if(lst[x][1] != lst[x-1][1]):
        score = lst[x][1]
        break

#Sort by name
lst = sorted(lst)

# Aggregate students whose result was score
# this is the second lowest value
lower_scorers = [x[0] for x in lst if x[1] == score]

print "\n".join(lower_scorers)

测试

输入

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39

输出

Berry
Harry

【讨论】:

    【解决方案2】:

    看看 Python 3.7 中的这个解决方案 ...

    if __name__ == '__main__':
        mark_sheet = []
        _min = 1e99
        for _ in range(int(input())):
            name = input()
            score = float(input())
            if score < _min:
                _min = score
            mark_sheet.append((name, score))
    
        # make a new mark_sheet excluding the one(s) that have the minimum score and 
        # sort on score in ascending order
        mark_sheet = sorted([mark for mark in mark_sheet if mark[1] != _min], key=lambda x: x[1])
    
        # if there is still a list then the first item should have the second best score
        mark_second_best = []
        if mark_sheet:
            second_best_score = mark_sheet[0][1]
    
            # make a list with the second best score and sort on name
            mark_second_best = sorted([mark for mark in mark_sheet if mark[1] == second_best_score],
                key=lambda x: x[0])
    
        # and print the result
        for mark in mark_second_best:
            print(mark[0])
    

    通过了 HackerRank。

    【讨论】:

      猜你喜欢
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2012-01-28
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多