Tip of the day
News
Instructions
Objective
Write a python assignment program to create a covid system and a pokemon game.
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
COVIDfrom functools import cmp_to_keydef 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, pokemonsdef 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()POKEMONdef 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, pokemonsdef 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)
Related Samples
Discover our Python Assignments Sample Section for expertly crafted solutions. From fundamental concepts to advanced algorithms, explore annotated code examples. Whether you're learning or teaching Python, these samples offer clarity and guidance for mastering assignments effectively. Enhance your programming skills and excel in Python with our comprehensive resources.
Python
Word Count
4091 Words
Writer Name:Walter Parkes
Total Orders:2387
Satisfaction rate:
Python
Word Count
2184 Words
Writer Name:Dr. Jesse Turner
Total Orders:2365
Satisfaction rate:
Python
Word Count
6429 Words
Writer Name:Dr. Olivia Campbell
Total Orders:753
Satisfaction rate:
Python
Word Count
5883 Words
Writer Name:Dr. David Adam
Total Orders:489
Satisfaction rate:
Python
Word Count
5849 Words
Writer Name:Dr. Nicholas Scott
Total Orders:642
Satisfaction rate:
Python
Word Count
6162 Words
Writer Name:Professor Liam Mitchell
Total Orders:932
Satisfaction rate:
Python
Word Count
3640 Words
Writer Name:Professor Daniel Mitchell
Total Orders:465
Satisfaction rate:
Python
Word Count
4343 Words
Writer Name:Prof. Jackson Ng
Total Orders:627
Satisfaction rate:
Python
Word Count
7272 Words
Writer Name:Dr. Jennifer Carter
Total Orders:879
Satisfaction rate:
Python
Word Count
4577 Words
Writer Name:Dr. Sophia Nguyen
Total Orders:900
Satisfaction rate:
Python
Word Count
4145 Words
Writer Name:Dr. Isabella Scott
Total Orders:754
Satisfaction rate:
Python
Word Count
4193 Words
Writer Name:Dr. Isabella Scott
Total Orders:754
Satisfaction rate:
Python
Word Count
4165 Words
Writer Name:Dr. Ashley
Total Orders:685
Satisfaction rate:
Python
Word Count
4176 Words
Writer Name:Prof. Jackson Ng
Total Orders:627
Satisfaction rate:
Python
Word Count
3922 Words
Writer Name:Dr. Chloe Mitchell
Total Orders:957
Satisfaction rate:
Python
Word Count
4091 Words
Writer Name:Glenn R. Maguire
Total Orders:714
Satisfaction rate:
Python
Word Count
4747 Words
Writer Name:Dr. Sophia Nguyen
Total Orders:900
Satisfaction rate:
Python
Word Count
4594 Words
Writer Name:Dr. Samantha Benet
Total Orders:812
Satisfaction rate:
Python
Word Count
6716 Words
Writer Name:Dr. Isabella Scott
Total Orders:754
Satisfaction rate:
Python
Word Count
4347 Words
Writer Name:Prof. James Harper
Total Orders:664
Satisfaction rate:
Get a FREE Quote