# Millet Porridge

English version of https://corvo.myseu.cn

0%

Code in this blog has been tested in Python3.7.

## Problem

In a comment’s system, the operator need to dynamic adjust the factor to influence the weight of the comment, and then sort the comments by the weights.

For example, there are many comments in this data structure:

We want to use an algorithm to sort the comments by the weights, the algorithm is:

The `user_weight` is the most important factor, and the `likes` is the second most important factor.

## A simple solution

At least, you may introduced an algorithm using `for-loop` to calculate the comments weights.

## A better solution

Sometimes, the operator want to modify the algorithm, for example: 1. modify the factor `user_weight` to be `user_weight * 2`, and the factor `likes` to be `likes * 2`. 2. Create a new factor specify the expression for every topic.

In this time, the old `for-loop` function can not work anymore. We have to refactor the implementation. The new function need input an expression, and a list of topics, then return the list of topics with the weights.

### How to parse the expression

In my college life, we have been taught with Reverse Polish Notation to get the value of expression. And we also learned `Principles of Compiler Design`. we can parse the expression to an `Abstruct Syntax Tree` (AST). This picture is the AST of the expression `(a+b)*(c-d))`. Once we were being told the exact value of every variable, we could get the result.

Well, it seems we don’t need to parse the expression by ourselves, because the Python has a built-in module called `ast`.

## The final solution

We could ask user to give us a simple expression, and then we can get the weight by this expression for every topic.

## Summary

What we have done to simplify the real problem: we introduced a new input for operator to let him/her to decide the topic weight, and even more, we decoupled the topic weight calculation from the topic data. This could help a lot when we need to add a new property or modify the expression.