Alice travels a lot for her work. Each time she travels, she visits a single city before returning home.
Someone recently asked her “how many different cities have you visited for work?” Thankfully Alice has kept a log of her trips. Help Alice figure out the number of cities she has visited at least once.
This problem can be solved in a number of ways. The simplest way to solve it is-
1. Take input as how many cities have been visited irrespective of number of times visited
2. Input the names of cities
3. Store the cities in a list only if it is unique
4. Print the length of the list
Python Code-
#input the number times trip taken
n=int(input())
#a list for storing unique cities
list = []
#if name of city is not in the list already, add it to the list
for i in range(n):
city= input()
if city not in list:
list.append(city)
#print the length of the list i.e number or cities visited atleast once
print(len(list))
You can also find the names of cities by printing the list itself using "print(list)" at the end.
Get Answers For Free
Most questions answered within 1 hours.