Home > Mobile >  Is there a way to print a graph in a format?
Is there a way to print a graph in a format?

Time:11-29

I am forming a fictional report that:

  1. Calculate the average grade
  2. Print a small graph of similarity scores and, if applicable, list the students under investigation.

Data form

The input in the data form is set up like this:

StudentA___5 6 7 4 5 6
5=20=22=10=2=0=0=1=0=1;StudentB, StudentC
StudentB___7 8 6 6
2=30=15=8=4=3=2=0=0=0;

In the first line the final grade is calculated. All the grades have the same weight and a grade that is >= 5.5 AND <6 is noted as 6. Otherwise is is just rounded to the nearest half if it isn't already.

In the second line 10 numbers separated by '=' and 0 or more names separated by ','

  • The first 10 numbers are similarity scores that represent the number of programs matching a certain percentage of the current program in steps of 10%. This means the first numbers indicates the matches from 1%-10% and the last number indicates the matches from 91%-100%.

Since this is not very readable, the professor would like a simple graph according to these rules:

  • if there are zero matches, display an underscore: _
  • if there are less than 20 matches, display a minus sign: -
  • if there are 20 or more matches, display a caret: ∧

The names of the students after the semicolon are the names of the students with matches in the final 3 categories. The names of these students should be printed under the graph. If there are no matches, the program should print "No matches found".

Output of the report I want to be

StudentA has an average of 6-
-^^--__-_-
StudentB
StudentC

StudentB has an average of 7.0
-^-----___
No matches found

My Code

I already have my code for calculating the average grade

import sys

for line in sys.stdin.readlines():
  split = line.split("_")
  name = split[0]
  grade = split[-1].split(" ")
  grade2 = [float(x) for x in grade]
  average = "%.1f" % round(sum(grade2) / len(grades2), 2)
  
  if average == "5.5":
     average = average   0.5, "-"
  else:
    pass

  print(str(name)   "has an average grade of "   str(average))

But I don't have much of a clue on how to start with the graph function.

CodePudding user response:

Maybe this will be helpful.

datas = [
    ('StudentA___5 6 7 4 5 6',
    '5=20=22=10=2=0=0=1=0=1;StudentB, StudentC'),
    ('StudentB___7 8 6 6',
    '2=30=15=8=4=3=2=0=0=0;'),
]

def print_graph(similarity_scores):
    graph = []
    for sim_score in similarity_scores:
        if sim_score >= 20:
            graph.append('^')
        elif sim_score > 0:
            graph.append('-')
        else:
            graph.append('_')
    print(''.join(graph))


def get_avg_grade(grades):
    avg_grade = sum(grades) / len(grades)
    mod = avg_grade % 1
    if mod == 0:
        sign = ''
    elif mod >= 0.5:
        sign = '-'
    else:
        sign = ' '
    return round(avg_grade), sign


for data in datas:
    name, *_, grades_str = data[0].split('_')
    grades = [float(grade) for grade in grades_str.split()]
    avg_grade, sign = get_avg_grade(grades)
    print(f'{name} has an average grade of {avg_grade}{sign}')
    
    sim_scores_str, names = data[1].split(';')
    sim_scores = [int(s) for s in sim_scores_str.split('=')]
    print_graph(sim_scores)
    
    if names:
        for name in [n.strip() for n in names.split(',')]:
            print(name)
    else:
        print('No matches found.')
  • Related