【问题标题】:Hackerrank Day 8: Dictionaries and Maps problem (Python used)Hackerrank 第 8 天:字典和地图问题(使用 Python)
【发布时间】:2021-04-18 22:01:33
【问题描述】:

目标
今天,我们正在学习使用 Map 或 Dictionary 数据结构的键值对映射。查看教程标签以获取学习材料和教学视频!

任务
给定姓名和电话号码,组装一个电话簿,将朋友的姓名映射到他们各自的电话号码。然后,您将获得未知数量的姓名来查询您的电话簿。对于每个查询,将电话簿中的相关条目以 name=phoneNumber 形式打印在新行上;如果找不到条目,​​则打印 Not found 。

注意:您的电话簿应该是 Dictionary/Map/HashMap 数据结构。

输入格式
第一行包含一个整数,表示电话簿中的条目数。 随后的每一行都以单行上的空格分隔值的形式描述了一个条目。第一个值是朋友的名字,第二个值是一个数字电话号码。
在电话簿条目的行之后,有未知的查询行数。每行(查询)都包含一个要查找的内容,您必须继续阅读行,直到没有更多输入为止。

注意:名称由小写英文字母组成,仅是名字。

输出格式
在每个查询的新行上,如果该名称在电话簿中没有相应的条目,则打印 Not found ;否则,请以 name=phoneNumber 格式打印完整信息。

示例输入

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

样本输出

sam=99912222
Not found
harry=12299933

我的密码:

# No. of dictionary inputs
n = int(input())

# Code to fill the phonebook with entries
phonebook = {}
for _ in range(1, n+1):
  entry = input().split(" ")
    
  phonebook[entry[0]] = entry[1]

# Code for query intake
queries = []
while(1):
  queries.append(input())

  #need to figure out when the user stops input and need to break this loop then
  if (input() == ""):
    break

# Code for query result
for i in range(0, len(queries)):
  if(queries[i] in phonebook):
    print(queries[i] + "=" + phonebook[queries[i]])
    # print(f"{queries[i]}={phonebook[queries[i]]}")
  else:
    print("Not found")

我面临的问题: 当我运行代码时,我输入了示例输入,直到最后一切都运行良好,但是,在打印出结果时,查询“edward”没有得到输出。 “edward”的期望输出将是“Not Found”,但是,每个偶数输入都会丢失,可能是由于 while 循环中的 if 语句。

【问题讨论】:

  • 如果您的问题是针对 Python 2 的,请不要使用 python-3.x 标签。如果它特定于 Python 3,请不要使用 python-2.7 标签(或在您的标题中暗示这是 Python 2 问题)。如果它不是特定于任何一个,请不要使用这些标签中的任何一个。您可以在Python tag wiki 中阅读有关如何标记 Python 问题的更多信息。

标签: python python-3.x python-2.7


【解决方案1】:
while(1):
  queries.append(input())

  #need to figure out when the user stops input and need to break this loop then
  if (input() == ""):
    break

应该只使用一次input(),然后使用append()break

while True:
  line = input()
  if line == "":
    break
  queries.append(line)

【讨论】:

    【解决方案2】:

    我的最终代码有效,在我使用 try except 块处理它时没有出现 EOF 错误:

    # Code to fill the phonebook with entries
    phonebook = dict() #Declare a dictionary
    for _ in range(int(input())):
        key, value = input().split()
        
        phonebook[key] = value
    
    #Trick - If there is no more input stop the program
    try:
        # Code for query intake
        queries = []
        while True:
            line = input()
            if line == "":
                break
            queries.append(line)
    except Exception:
        pass
    
    # Code for query result
    for i in range(0, len(queries)):
        if(queries[i] in phonebook):
            print(queries[i] + "=" + phonebook[queries[i]])
            # print(f"{queries[i]}={phonebook[queries[i]]}")
        else:
            print("Not found")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2018-02-08
      • 2021-08-02
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多