← 2023 questions

2023 / TIKTOK / QUESTION 18

Personalised recommendations

Try the question in your own editor, then check your output below.

THE QUESTION

Sofea is a TikTok user. Topics she likes include gym, cooking, travel, gardening and art. Imagine her TikTok feed is a 1D array that starts with 5 elements and can constantly be updated. Make a text-based user interface where a subject she likes will be displayed as a string (simulating a post) with equal chance of being about gym, cooking, travel, gardening or art, Provide functions that allow her to like, scroll past, or refresh when given a new post. Make sure user input is validated.

If she likes the post, append a new element to the 1D array with a 5% increased chance of it being that topic (e.g. if she likes ‘easy cake recipe’, the future post should have an increased chance of it being about cooking), make sure the new probability of that topic is stored. Then, clear the console (not the 1D array) and print the next post.

If she scrolls past the post, append a new element to the 1D array with a 5% decreased chance of being that topic, make sure the new probability of that topic is stored. Then, clear the console and print the next post.

If she refreshes at that post, clear the 1D array and append 5 new elements to it based on the stored probabilities, repeating the process.

(it’s ok if posts are repeated in the feed)

Starter code:

posts = [ [['gym etiquette'], ['shoulder workouts'], ['how to get a six pack fast'], ['stop doing this at the gym'], ['best leg day workouts']] , [['easy cake recipe'], ['essential ingredients for sushi'], ['my life as a 5* cook'], ['making subs in a subway'], ['top 5 chinese takeouts in London']] , [['how i got 9 for my IGCSE art'], ['tips for beginner artists'], ['draw spongebob, win $5'], ['the history of Banksy'], ['how to draw anime eyes']] , [['my trip to Ghana'], ['climbing 10 mountains in 100 days'], ['top 3 travel essentials'], ['how to pack efficiently'], ['top 5 places to visit in Mumbai']] , [['watching grass grow until 1M followers'], ['top 10 fruits to homegrow'], ['how to grow oranges'], ['this is the best fertiliser (not sponsored)'], ['why you should plant aloe vera at home']] ]
feed = []

Question by Yu Shen. Source: Girls In Code SEA — Behind the Screens, 2023, slide 34.

PRACTICE CHECK

Try your answer.

New deterministic checkpoint with an explicit probability-redistribution rule, since “5% increased chance” is ambiguous in the source.

Run your program in your own editor, then paste its output below. This checks the stated output or checkpoint; it does not assess your full program. Practice attempts do not affect competition scores.

Practice convention: start at 20% per topic; a like adds 5 percentage points to that topic and removes 1.25 from each other topic. Then a skip reverses those adjustments. Like gym, then skip gym, then like cooking. Enter gym, cooking, travel, gardening, art percentages.

Reveal worked answer

Updated topic probabilities

[18.75, 25, 18.75, 18.75, 18.75]

The gym like and skip cancel. The final cooking like gives cooking 25% and each other topic 18.75%.