Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua day03 #321

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lua/.luacheckrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
std = "max"

max_line_length = 240

ignore = {
"212"
}
1 change: 1 addition & 0 deletions lua/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
- [reverse-string](./reverse-string/README.md)
- [raindrops](./raindrops/README.md)
- [leap](./leap/README.md)
- [space-age](./space-age/README.md)
5 changes: 5 additions & 0 deletions lua/space-age/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
24 changes: 24 additions & 0 deletions lua/space-age/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"authors": [
"aarti"
],
"contributors": [
"etandel",
"kytrinyx",
"ryanplusplus"
],
"files": {
"solution": [
"space-age.lua"
],
"test": [
"space-age_spec.lua"
],
"example": [
".meta/example.lua"
]
},
"blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
"source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
"source_url": "https://pine.fm/LearnToProgram/?Chapter=01"
}
1 change: 1 addition & 0 deletions lua/space-age/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"lua","exercise":"space-age","id":"f36b747a9a3a4cda932c2b3576e0d5ef","url":"https://exercism.org/tracks/lua/exercises/space-age","handle":"vpayno","is_requester":true,"auto_approve":false}
54 changes: 54 additions & 0 deletions lua/space-age/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Help

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

Refer to the [Installing Lua locally][install] documentation to get Lua and busted installed correctly.

[install]: https://exercism.org/docs/tracks/lua/installation

## Submitting your solution

You can submit your solution using the `exercism submit space-age.lua` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Lua track's documentation](https://exercism.org/docs/tracks/lua)
- The [Lua track's programming category on the forum](https://forum.exercism.org/c/programming/lua)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

There are many great free online resources for Lua including:

1. [lua.org][8]
2. A highly recommended detailed and authoritative introduction to all aspects of Lua programming by Lua's chief architect: [Programming in Lua, by Roberto Ierusalimschy][6]. Note that the first edition (covering Lua 5.0) is [freely available online][9].
3. For an official definition of the Lua language, consult the [Lua 5.4 Reference Manual][7], by R. Ierusalimschy, L. H. de Figueiredo, W. Celes.
4. [Lua Style Guide][4]
5. [Learn Lua in 15 minutes][5]
6. Some options for linting
- [Lua Lint][10]
- [Lua Inspect][11]
- [luacheck][12]
- [Detecting Undefined Variables][13]

[4]: https://github.com/Olivine-Labs/lua-style-guide
[5]: http://tylerneylon.com/a/learn-lua/
[6]: http://www.lua.org/pil/
[7]: http://www.lua.org/manual/5.4/
[8]: http://www.lua.org
[9]: http://www.lua.org/pil/contents.html
[10]: http://lua-users.org/wiki/LuaLint
[11]: http://lua-users.org/wiki/LuaInspect
[12]: https://github.com/luarocks/luacheck
[13]: http://lua-users.org/wiki/DetectingUndefinedVariables
51 changes: 51 additions & 0 deletions lua/space-age/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Space Age

Welcome to Space Age on Exercism's Lua Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Given an age in seconds, calculate how old someone would be on:

- Mercury: orbital period 0.2408467 Earth years
- Venus: orbital period 0.61519726 Earth years
- Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
- Mars: orbital period 1.8808158 Earth years
- Jupiter: orbital period 11.862615 Earth years
- Saturn: orbital period 29.447498 Earth years
- Uranus: orbital period 84.016846 Earth years
- Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should
be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video].

Note: The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year).
The Gregorian calendar has, on average, 365.2425 days.
While not entirely accurate, 365.25 is the value used in this exercise.
See [Year on Wikipedia][year] for more ways to measure a year.

[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs
[year]: https://en.wikipedia.org/wiki/Year#Summary

## Source

### Created by

- @aarti

### Contributed to by

- @etandel
- @kytrinyx
- @ryanplusplus

### Based on

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=01

### My Solution

- [my solution](./space-age.lua)
- [run-tests](./run-tests-lua.txt)
130 changes: 130 additions & 0 deletions lua/space-age/luacov.report.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
==============================================================================
space-age_spec.lua
==============================================================================
* local SpaceAge = require("space-age")

* describe("space-age", function()
* it("age in seconds", function()
* local age = SpaceAge:new(1000000)
* assert.are.equal(1000000, age.seconds)
end)

* it("age in Earth years", function()
* local age = SpaceAge:new(1000000000)
* assert.are.equal(31.69, age.on_earth())
end)

* it("age in Mercury years", function()
* local age = SpaceAge:new(2134835688)
* assert.are.equal(67.65, age.on_earth())
* assert.are.equal(280.88, age.on_mercury())
end)

* it("age in Venus years", function()
* local age = SpaceAge:new(189839836)
* assert.are.equal(6.02, age.on_earth())
* assert.are.equal(9.78, age.on_venus())
end)

* it("age in Mars years", function()
* local age = SpaceAge:new(2329871239)
* assert.are.equal(73.83, age.on_earth())
* assert.are.equal(39.25, age.on_mars())
end)

* it("age in Jupiter years", function()
* local age = SpaceAge:new(901876382)
* assert.are.equal(28.58, age.on_earth())
* assert.are.equal(2.41, age.on_jupiter())
end)

* it("age in Saturn years", function()
* local age = SpaceAge:new(3000000000)
* assert.are.equal(95.06, age.on_earth())
* assert.are.equal(3.23, age.on_saturn())
end)

* it("age in Uranus years", function()
* local age = SpaceAge:new(3210123456)
* assert.are.equal(101.72, age.on_earth())
* assert.are.equal(1.21, age.on_uranus())
end)

* it("age in Neptune year", function()
* local age = SpaceAge:new(8210123456)
* assert.are.equal(260.16, age.on_earth())
* assert.are.equal(1.58, age.on_neptune())
end)
end)

==============================================================================
space-age.lua
==============================================================================
* local SpaceAge = {}
* SpaceAge.__index = SpaceAge

* function SpaceAge:new(seconds)
* local orbital_periods = {
* Mercury = 0.2408467,
* Venus = 0.61519726,
* Earth = 1.0,
* Mars = 1.8808158,
* Jupiter = 11.862615,
* Saturn = 29.447498,
* Uranus = 84.016846,
* Neptune = 164.79132,
* }

* local seconds_in_earth_years = 365.25 * 24 * 60 * 60

* return {
-- one of the tests wants to retreive the seconds
* seconds = seconds,

-- these have to be anonymous functions
* on_mercury = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Mercury * seconds_in_earth_years)))
end,

* on_venus = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Venus * seconds_in_earth_years)))
end,

* on_earth = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Earth * seconds_in_earth_years)))
end,

* on_mars = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Mars * seconds_in_earth_years)))
end,

* on_jupiter = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Jupiter * seconds_in_earth_years)))
end,

* on_saturn = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Saturn * seconds_in_earth_years)))
end,

on_uranus = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Uranus * seconds_in_earth_years)))
end,

on_neptune = function()
* return tonumber(("%.2f"):format(seconds / (orbital_periods.Neptune * seconds_in_earth_years)))
end,
* }
end

* return SpaceAge

==============================================================================
Summary
==============================================================================

File Hits Missed Coverage
---------------------------------------
space-age.lua 32 0 100.00%
space-age_spec.lua 36 0 100.00%
---------------------------------------
Total 68 0 100.00%
3 changes: 3 additions & 0 deletions lua/space-age/luacov.report.out.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
space-age_spec.lua:0 1765
space-age.lua:1765 3800
Summary:3800 4204
Loading
Loading