Hackerrank 第 8 天:字典和地图问题(使用 Python)
Hackerrank Day 8: Dictionaries and Maps problem (Python used)
Objective
今天,我们将学习使用 Map 或 Dictionary 数据结构的键值对映射。查看“教程”选项卡以获取学习材料和教学视频!
任务
给定名字和 phone 号码,assemble 一本 phone 书将朋友的名字映射到他们各自的 phone 号码。然后你会得到一个未知数量的名字来查询你的 phone 书。对于每个查询,以 name=phoneNumber 的形式在新行上打印 phone 书中的相关条目;如果找不到条目,则打印 Not found。
注意:你的phone书应该是Dictionary/Map/HashMap数据结构。
输入格式
第一行包含一个整数,表示 phone 本书中的条目数。
随后的每一行都在一行中以 space 分隔值的形式描述了一个条目。第一个值是朋友的名字,第二个值是一个-digit phone 数字。
在phone 行的书条目之后,有未知行数的查询。每行(查询)包含一个要查找的,您必须继续读取行,直到没有更多输入。
注意:名称由小写英文字母组成,仅为名字。
输出格式
如果该名称在 phone 书中没有相应的条目,则在每个查询的新行上打印 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 语句。
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)
我的最终代码有效,没有 EOF 错误,因为我使用 try except 块处理它:
# 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")
Objective
今天,我们将学习使用 Map 或 Dictionary 数据结构的键值对映射。查看“教程”选项卡以获取学习材料和教学视频!
任务
给定名字和 phone 号码,assemble 一本 phone 书将朋友的名字映射到他们各自的 phone 号码。然后你会得到一个未知数量的名字来查询你的 phone 书。对于每个查询,以 name=phoneNumber 的形式在新行上打印 phone 书中的相关条目;如果找不到条目,则打印 Not found。
注意:你的phone书应该是Dictionary/Map/HashMap数据结构。
输入格式
第一行包含一个整数,表示 phone 本书中的条目数。
随后的每一行都在一行中以 space 分隔值的形式描述了一个条目。第一个值是朋友的名字,第二个值是一个-digit phone 数字。
在phone 行的书条目之后,有未知行数的查询。每行(查询)包含一个要查找的,您必须继续读取行,直到没有更多输入。
注意:名称由小写英文字母组成,仅为名字。
输出格式
如果该名称在 phone 书中没有相应的条目,则在每个查询的新行上打印 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 语句。
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)
我的最终代码有效,没有 EOF 错误,因为我使用 try except 块处理它:
# 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")