Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. # Pokemon problem
  2.  
  3. '''
  4.    You are given 2 lists:
  5.    - pokemon_names: a list off all the known pokemon names (will refer as generic name)
  6.    - card_names: a list of all the pokemon card names
  7.  
  8.    You recently found out that a pokemon card can have multiple pokemon in it.
  9.    You would like to be able to show what pokemon_names (generic pokemon) are in each pokemon card.
  10.  
  11.    Pokemon cards can include anywhere from 0, 3 generic pokemon within it.
  12.    Pokemon cards can also have false names inside () indicating promotion cards, etc.
  13.  
  14.    Your goal is for every pokemon card map all the generic pokemon within it
  15. '''
  16.  
  17. import argparse
  18. import csv
  19. import re
  20. from itertools import zip_longest
  21.  
  22. """
  23. Parse an input file to a list of names separated by commas
  24. """
  25. def parse_file_to_names(filename):
  26.     names = []
  27.     with open(filename, 'r') as csvfile:
  28.         csvreader  = csv.reader(csvfile, delimiter=',')
  29.         for row in csvreader:
  30.             names.append(row[0])
  31.     return names
  32.  
  33. def get_head(arr):
  34.     return arr[:5]
  35.  
  36. '''
  37.    My solution. This was originally part of a pandas DataFrame, so I maintain the order
  38. '''
  39. def map_pokemon_names_to_pokemon_cards(card_names, pokemon_names):
  40.     card_names_clean = [re.sub(r'(\(.*\))', '', card) for card in card_names] # removes the (filler card info) out of cards that may have unrelated pokemon in it
  41.     card_names_clean_list = [card.lower().split(' ') for card in card_names_clean]
  42.     pokemon_teams = []
  43.     generic_name = pokemon_names # contains full name of all pokemon
  44.     for card_name in card_names_clean_list:
  45.         pokemon_team = set()
  46.          for ele1, ele2 in zip_longest(card_name[:], card_name[1:]):
  47.             if ele1 in generic_name: # a string returns true in a list if exact match. so mr.is not in a list with ['mr. mime']
  48.                 pokemon_team.add(ele1)
  49.             elif ele2 in generic_name:
  50.                 pokemon_team.add(ele2)
  51.             elif str(ele1) + ' ' + str(ele2) in generic_name: # this deals with mr. mime/ mime jr. and other pokemon with 2 words combinations
  52.                 pokemon_team.add(str(ele1) + ' ' + str(ele2))
  53.         pokemon_teams.append(pokemon_team)    
  54.        
  55.     for card, pokemons in zip(card_names, pokemon_teams):  
  56.         print(f'{card}: {pokemons}')
  57.     return pokemon_teams
  58.  
  59. if __name__ == "__main__":
  60.     parser = argparse.ArgumentParser(description='enter description')
  61.     parser.add_argument('--pokemon_names_file', action="store")
  62.     parser.add_argument('--card_names_file', action="store")
  63.  
  64.     args = parser.parse_args()
  65.  
  66.     # argparse stuff
  67.     pokemon_names = parse_file_to_names(args.pokemon_names_file)
  68.     card_names = parse_file_to_names(args.card_names_file)
  69.  
  70.     print('pokemon names head: ', get_head(pokemon_names))
  71.     print('card names head: ', get_head(card_names))
  72.  
  73.     pokemon_teams = map_pokemon_names_to_pokemon_cards(card_names, pokemon_names)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement