Instructions
Objective
Write a program to create a covid system and a pokemon game in python.
Requirements and Specifications
Create a management system regarding covid database
Work with csv files and data sets
Create a Pokemon game using instructions and proper methods.
Source Code
COVID
from functools import cmp_to_key
def read(filename):
f = open(filename, 'r')
header = ''
pokemons = []
first = True
for line in f.read().splitlines():
if first:
header = line
first = False
else:
pokemons.append(line.split(','))
f.close()
return header, pokemons
def problem1(covid):
for c in covid:
if '-' in c[1]:
parts = c[1].split('-')
c[1] = str(round((float(parts[0]) + float(parts[1]))/2))
def problem2(covid):
for c in covid:
for i in range(8,11):
parts = c[i].split('.')
c[i] = parts[1] + '.' + parts[0] + '.' + parts[2]
def problem3(covid):
tmp = {}
for c in covid:
province = c[4]
if province not in tmp:
tmp[province] = [[0, 0.0], [0, 0.0]]
if c[6] != 'NaN':
tmp[province][0][0] += 1
tmp[province][0][1] += float(c[6])
if c[7] != 'NaN':
tmp[province][1][0] += 1
tmp[province][1][1] += float(c[7])
for c in covid:
province = c[4]
if c[6] == 'NaN':
if tmp[province][0][0] > 0:
c[6] = str(round(tmp[province][0][1] / tmp[province][0][0], 2))
if c[7] == 'NaN':
if tmp[province][1][0] > 0:
c[7] = str(round(tmp[province][1][1] / tmp[province][1][0], 2))
def problem4(covid):
tmp = {}
for c in covid:
province = c[4]
if province not in tmp:
tmp[province] = {}
if c[3] != 'NaN':
if c[3] not in tmp[province]:
tmp[province][c[3]] = 0
tmp[province][c[3]] += 1
map = {}
for p in tmp:
cities = list(tmp[p].keys())
def compare(city1, city2):
diff = tmp[p][city2] - tmp[p][city1]
if diff != 0:
return diff
if city1 < city2:
return -1
if city1 > city2:
return 1
return 0
cities.sort(key=cmp_to_key(compare))
map[p] = cities[0]
for c in covid:
if c[3] == 'NaN':
c[3] = map[c[4]]
def problem5(covid):
tmp = {}
for c in covid:
province = c[4]
if province not in tmp:
tmp[province] = {}
if c[11] != 'NaN':
if c[11] not in tmp[province]:
tmp[province][c[11]] = 0
tmp[province][c[11]] += 1
map = {}
for p in tmp:
syms = list(tmp[p].keys())
def compare(sym1, sym2):
diff = tmp[p][sym2] - tmp[p][sym1]
if diff != 0:
return diff
if sym1 < sym2:
return -1
if sym1 > sym2:
return 1
return 0
syms.sort(key=cmp_to_key(compare))
map[p] = syms[0]
for c in covid:
if c[11] == 'NaN':
c[11] = map[c[4]]
header, covid = read('covidTrain.csv')
problem1(covid)
problem2(covid)
problem3(covid)
problem4(covid)
problem5(covid)
fout = open('covidResult.csv', 'w')
fout.write(header + '\n')
for c in covid:
fout.write(','.join(c) + '\n')
fout.close()
POKEMON
def read(filename):
f = open(filename, 'r')
header = ''
pokemons = []
first = True
for line in f.read().splitlines():
if first:
header = line
first = False
else:
pokemons.append(line.split(','))
f.close()
return header, pokemons
def problem1(pokemons):
fire_pokemons = []
for p in pokemons:
if p[4] == 'fire':
fire_pokemons.append(p)
total = len(fire_pokemons)
up40 = 0
for p in fire_pokemons:
if float(p[2]) >= 40.0:
up40 += 1
ratio = round(100 * up40 / total)
fout = open('pokemon1.txt', 'w')
fout.write('Percentage of fire type Pokemons at or above level 40 = ' + str(ratio))
fout.close()
def problem2(pokemons):
types_by_weakness = {}
for p in pokemons:
weakness = p[5]
type = p[4]
if weakness not in types_by_weakness:
if type != 'NaN':
types_by_weakness[weakness] = {type : 1}
else:
if type != 'NaN':
if type in types_by_weakness[weakness]:
types_by_weakness[weakness][type] += 1
else:
types_by_weakness[weakness][type] = 1
map = {}
for w in types_by_weakness:
types = list(types_by_weakness[w].keys())
def compare(type1, type2):
diff = types_by_weakness[w][type2] - types_by_weakness[w][type1]
if diff != 0:
return diff
if type1 < type2:
return -1
if type1 > type2:
return 1
return 0
def sort_records(records):
for i in range(len(records) - 1):
for j in range(len(records) - 1 - i):
if compare(records[j], records[j+1]) > 0:
sw = records[j+1]
records[j+1] = records[j]
records[j] = sw
sort_records(types)
map[w] = types[0]
for p in pokemons:
type = p[4]
if type == 'NaN':
p[4] = map[p[5]]
def problem3(pokemons):
threshold = 40.0
attr = ['atk', 'def', 'hp']
comp40 = [False, True]
tmp = {}
for a in attr:
for c in comp40:
tmp[(a,c)] = [0, 0]
for p in pokemons:
level = float(p[2])
for i in range(3):
if p[6+i] != 'NaN':
tmp[(attr[i], (level >= threshold))][0] += 1
tmp[(attr[i], (level >= threshold))][1] += float(p[6+i])
for p in pokemons:
level = float(p[2])
for i in range(3):
if p[6+i] == 'NaN':
p[6+i] = str(round(tmp[(attr[i], (level >= threshold))][1] / tmp[(attr[i], (level >= threshold))][0], 1))
def problem4(pokemons):
personalities_by_type = {}
for p in pokemons:
if p[4] not in personalities_by_type:
personalities_by_type[p[4]] = [p[3]]
else:
if p[3] not in personalities_by_type[p[4]]:
personalities_by_type[p[4]].append(p[3])
types = list(personalities_by_type.keys())
types.sort()
f = open('pokemon4.txt', 'w')
for t in types:
personalities_by_type[t].sort()
f.write(t + ': ')
f.write(', '.join(personalities_by_type[t]))
f.write('\n')
f.close()
def problem5(pokemons):
hp_sum = 0.0
hp_count = 0
for p in pokemons:
if float(p[9]) >= 3.0:
hp_count += 1
hp_sum += float(p[8])
f = open('pokemon5.txt', 'w')
f.write('Average hit point for Pokemons of stage 3.0 = ' + str(round(hp_sum / hp_count)))
f.close()
header, pokemons = read('pokemonTrain.csv')
problem1(pokemons)
problem2(pokemons)
problem3(pokemons)
fout = open('pokemonResult.csv', 'w')
fout.write(header + '\n')
for p in pokemons:
fout.write(','.join(p) + '\n')
fout.close()
problem4(pokemons)
problem5(pokemons)