A lot of people seem unexperienced with the string libary, so I thought I'd open a thread where people can find nice examples to practice.
Feel free to add challenges of your own, or give a better soloution to a previously solved puzzle.
Preferably make puzzles that have "real world application", so people can see how useful knowledge of the string libary is.
I'll go first:
Let's say you have a camera that saves your pictures in the following naming convention:
IMG_20151023_133742.jpg
Now create a program that would print out a string like this for every image file in a folder:
Picture taken at 13:37:42 on 23/10/2015.
Not that you could also easily make a program that renames all the files like this using os.rename.
Give it a shot!
Lua String Libary Quiz
Re: Lua String Libary Quiz
Code: Select all
local str = 'IMG_20151023_133742.jpg'
result = str:gsub( 'IMG_(%d%d%d%d)(%d%d)(%d%d)_(%d%d)(%d%d)(%d%d)%.jpg', 'Picture taken at %4:%5:%6 on %3/%2/%1' )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: Lua String Libary Quiz
For me personally I don't have a problem learning some string functions, the thing that bothers me a lot is using Regular Expressions
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Re: Lua String Libary Quiz
Now what if the camera appends a letter to the end of the original string? For example, I have a DSLR camera with rapid capture functionality. It snaps 5 pictures per second and appends letters only to images that occur during the same second (with the first picture that second as an exception).
Example:
20151207220631.jpeg
20151207220631a.jpeg
20151207220631b.jpeg
20151207220632.jpeg
20151207220632a.jpeg
20151207220632b.jpeg
20151207220632c.jpeg
EDIT **
Or keep the same format as the first example just because:
IMG_20151207_220631.jpeg
IMG_20151207_220631a.jpeg
etc
Example:
20151207220631.jpeg
20151207220631a.jpeg
20151207220631b.jpeg
20151207220632.jpeg
20151207220632a.jpeg
20151207220632b.jpeg
20151207220632c.jpeg
EDIT **
Or keep the same format as the first example just because:
IMG_20151207_220631.jpeg
IMG_20151207_220631a.jpeg
etc
Re: Lua String Libary Quiz
Yeah, what then?
What's the desired output?
What's the challenge?
What's the desired output?
What's the challenge?
Re: Lua String Libary Quiz
Ummmmmmmm... Its basically the same as what you had originally asked, just added a final character to only certain files. The challenge I guess would be to do the same thing but to include that. Example output:undef wrote:Yeah, what then?
What's the desired output?
What's the challenge?
Picture taken at 22:05:35 on 12/07/2015
Picture taken at 22:06:31 on 12/07/2015
Picture taken at 22:06:31 on 12/07/2015 - (a)
Picture taken at 22:06:31 on 12/07/2015 - (b)
Picture taken at 22:06:32 on 12/07/2015
Picture taken at 22:06:34 on 12/07/2015
Picture taken at 22:06:34 on 12/07/2015 - (a)
Who is online
Users browsing this forum: No registered users and 3 guests