Ask coding questions

← Back to all posts
Finding position in a text file

Hi everyone,
In my code, if the program enters number 3 when asked what they would like to do, it will show a leaderboard with top 5 results.
The program asks whether they wish to see their position in the list.. now I'm struggling doing this because the list in the text file isn't ordered.
I tried creating another text file and write the sorted list into there but it doesn't work.
Mind helping me out :(

Answered by Highwayman [earned 5 cycles]
View Answer

what if you gather the data of every single player in the leader board and then just filter out everyone with a lower score and return the length of the filtered array?


Oh God, too many words. Could you explain please?


haha, sorry! XD
of course!

what if you gather the data of every single player in the leader board

So we have to file leaderboard.txt:

This contains two important values for each line:
a number - which I assume is a score.
and a string - which I assume is the name.

Which are separated very clearly by a single dash(-) each time. This enables us to easily split up the individual pieces of data.

We can now think of a line like a little dictionary now:

So basically, "gather the data" => "read each line of file and parse them into dicts or something." what I forgot to mention is that you'd "gather the data" into a list. So, just a list of dicts, each containing a name and score...

...and then just filter out everyone with a lower score

this I think is pretty obvious: I literally meant .filter() out the dicts that contained a dict["score"] that is less than the score of the current user.

... and return the length of the filtered array?

In this final bit, I assume that this is all happening in a function, so your code would look like

It should make some sense why I am saying to return the length:
if you are in first place, then there is only you in the list, making it return 1, if you're in second place there are two people in the array...etc, etc.

hope that is a little bit more understandable. sorry bout that XD



I forgot to tell you that I am still a beginner on this course, so umm:
Am i making the dictionary for every person on the "leaderboard.txt"? People will be playing the music game quiz and more people will be on the leaderboard, so like I'm not sure about this part...
To be honest, I don't get the whole thing, I'm so sorry. Never learnt about dictionaries or the filter command, etc.. any easier way to do this?


Oh. Could you tell me what you do know how to do?



My teacher told me to put it in a list and use the index command to find the position.. but how can i put an unsorted text file into a list?? @Highwayman


You have a bunch of entries.
They each have their index provided.
You have an empty list.
You fill the list with the unsorted data.
You cannot search the list if it is not sorted for whatever reason.
What is the most logical step with which to proceed?


I did it, hehe! Ty for your help


awesome! yw. @FatimaNadeem1