Advertisement
Guest User

Untitled

a guest
Apr 5th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. import re
  2. import statistics
  3.  
  4. # Get the markdown table from the input field
  5. markdown_table = pd['steps']['trigger']['data']['markdown_table']
  6.  
  7. ratings = []
  8. types_of_result = []
  9.  
  10. # Match the type of result and information gain rating using a regular expression
  11. pattern = re.compile(r'\|\s*(.*?)\s*\|\s*(.+?)\s*\|\s*(\d+)\s*\|\s*(.+?)\s*\|')
  12.  
  13. # Find all matches and add the rating and type of result to the lists
  14. for match in pattern.findall(markdown_table):
  15.     types_of_result.append(match[0])
  16.     ratings.append(int(match[2]))
  17.  
  18. # Calculate the average of the ratings and round to the nearest integer
  19. if ratings:
  20.     avg_rating = round(statistics.mean(ratings))
  21. else:
  22.     avg_rating = 0
  23.  
  24. # Count the number of rows in the table (excluding headings)
  25. num_rows = markdown_table.count('\n') - 3
  26.  
  27. # Determine the most common type(s) of result
  28. if types_of_result:
  29.     freq_dict = {}
  30.     for type in types_of_result:
  31.         if type in freq_dict:
  32.             freq_dict[type] += 1
  33.         else:
  34.             freq_dict[type] = 1
  35.     max_freq = max(freq_dict.values())
  36.     freq_types = [type for type, freq in freq_dict.items() if freq == max_freq]
  37.     median_type = ','.join(freq_types)
  38. else:
  39.     median_type = ''
  40.  
  41. # Define the output dictionary
  42. output_data = {
  43.     'ratings': ','.join(map(str, ratings)),
  44.     'average_rating': avg_rating,
  45.     'num_rows': num_rows,
  46.     'median_type': median_type
  47. }
  48.  
  49. # Return the output dictionary
  50. return output_data
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement