Record an index-specific value and treat the values

Asked

Viewed 27 times

2

The number of Likes is cumulative, that is, if the post has 0 Likes and receives 5 Likes, it will have 0 + 5 =5 Likes. If soon, this same post receives 7 Likes, will have 5 + 7 = 12 Likes.

  • If the original posting receives between 1 and 10 Ikes, then your neighbors receive 1 like bonus.
  • If the original posting receives more than 10 Ikes, then your neighbors receive half of Ikes received by the original post.
  • Error when assigning Likes. If by mistake there is an attempt to assign Likes to a not existing posting, in addition to showing an error message (not existing posting), these Ikes should be assigned to a random posting, without benefiting (deliver bonus) to the neighbors of that post.

I have already been able to assign Ikes to the original publication, but not to their neighbor

My code so far:

postagem = [5, 6, 9]

print('2) Dar likes a um post')
alternativa = int(input())

if(alternativa == 2):
     qtd_like = int(input())
     post = int(input())
     if(qtd_like >= 1) and (qtd_like <= 10):
         postagem[post] = postagem[post] + qtd_like

     if(qtd_like > 10):
         postagem[post] = postagem[post] + qtd_like

1 answer

2


By "neighbor", I am assuming that it is the elements that are immediately next to it. For example, if the list is [5, 6, 9, 10], the neighbors of 6 are the 5 and the 9, the neighbors of 9 are the 6 and the 10, the 5 has only one neighbor (the 6), and the 10 has only one neighbor (the 9).

Assuming also that post is the position of the element in the list, just take post - 1 and post + 1, taking care of cases where it is the first or the last element.

To choose a random post, just use the module random. Would look like this:

import random

postagens = [5, 6, 9]
total = len(postagens) # quantidade total de postagens
if alternativa == 2:
    qtd_like = int(input())
    post = int(input())

    if post < 0 or post >= total:
       print(f'postagem não existente: {post}')
       post = random.randrange(0, total) # escolher postagem aleatória
       print(f'escolher postagem aleatória: {post}')
       bonus = 0 # não tem bônus
    elif 1 <= qtd_like <= 10:
       bonus = 1
    elif qtd_like > 10:
       bonus = qtd_like // 2

    postagens[post] += qtd_like
    if bonus > 0:
        if post >= 1: # se não é o primeiro, dá bônus para o anterior
            postagens[post - 1] += bonus
        if post < total - 1: # se não é o último, dá bônus para o próximo
            postagens[post + 1] += bonus

In case the bonus is half the amount of Likes, I used the whole room (//), rounded off the result (that is, if 13 Likes, the bonus will be 6). I did so because it doesn’t seem to make sense to have something like 6.5 Likes.

Notice I changed the name of the list to postagens (plural), after all, if she’s going to save more than one post, it’s best that the name makes that clear. It might seem "fresh", but giving better names helps a lot when programming.

Also I did not treat the case of the amount of Likes to be negative (probably do not have to do anything in this case, at most display an error message).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.