An algorithm to set text in files to arrays
This post took a little too long to get out. It has been a busy week. Every class I had required me to either write an essay or take a test. I have also started to do some multiplayer HOI4 games with friends.Thus I did not have time to write up this post. At least it is still less than two weeks since the last post. Regardless, here is something I have made that was not a computer science project.
With my work in the prior post about prime factorization, I used some of Python's functions to get a list of prime numbers from a text file. This led me to try some things further with the reading/writing functions.
This is a simple function that takes lines from a text file and puts them into lists. You can find it here on Github. The following is the basic steps:
- Check to see what tag is the current line. The line with a tag is indicated by it starting with a hashtag
- Add all lines to the list related to that tag until the current line is a tag
- Check to see what tag is the current line again. Check again if it wasn't found the first time.
- If no tag was found while checking twice, stop checking.
- If the current line is #End, close the file.
As I felt a little wrong being late, I decided to add a little more to the code. I've also added a simple function where I could write in the lines of the file until I put in #End.
The example used for this was the sorting of greetings and goodbyes. I would enter in the tag #Greetings, followed by various greetings one at a time. Next, I would enter in the tag #Goodbyes, followed by various goodbyes one at a time. The algorithm will look for the tags, and put them into their respective list. The algorithm will work even if you switch the order.
This was simply a test of file handling in Python. My next post may possibly be a look back at the idle game I made prior. Until then, see you next time.
Comments
Post a Comment