Challenge 40– When will the sun rise?
Challenge 40 involved computing when the sun rise will rise, based upon the following inputs:
- Calendar Date
- Location
- Latitude
- Longitude
- Elevation above sea level (optional)
Prototype Function
The following is prototype function to compute when the sun will rise. Please fill in the missing information and use any data type you like in order to get the MOST ACCURATE result.
public object GetSunrise(object date, object latitude, object longitude, long elevation = 0) { object time = new object(); // TODO - create algorithm to compute sunrise based upon date, latitude, // TODO - longitude and optionally elevation return time; }
Requirements
- Given a calendar date, and location on the earth measured by Latitude, Longitude, and elevation above sea level (optional) compute sun rise time to the nearest second.
- Any programming language can be used.
- Winner will be judged on accuracy and completeness of the solution.
Winning Response
The following is a good response as submitted by Vishwapriya Sivaraman.
using System; using Innovative.SolarCalculator; namespace SCSCodingChallenge29 { public static class Challenge40 { public static string CalculateDawn(DateTime date, double lat, double lng, long elev = 0) { try { SolarTimes solarTimes = new SolarTimes(date, lat, lng); DateTime sr = solarTimes.Sunrise; DateTime dt = Convert.ToDateTime(sr); string text = dt.ToString("h:mm:ss"); return text; } catch { Console.WriteLine("Enter valid values. Error validation is yet to be written. "); throw; } } } }
Vishwapriya found a library on the internet, Innovative.SolarCalculator, that could do the calculations, sparing her from implementing complex math and code.
The library was found on Github. https://github.com/porrey/Solar-Calculator
Winners (out of approximately 10 respondents)
Thanks to everybody for your responses to SCS Coding Challenge #40!!!
Congratulations to Vishwapriya Sivaraman for her fine effort to come up with a workable algorithm.
Name | Prize Winner | Good Answer |
Vishwapriya Sivaraman | X | X |
John Lauter | X |
Monthly Programming Tidbits
Pay attention to the name of your identifiers
Name of your identifiers is as important as the name of your children. I’m dead serious. No one wants to read someone else’s code with the following identifiers:
od: What is od? Oh Damn? Obsessive Disorder? Overdue Days?
Button1_Click(): What is Button1? How is it different from Button2?
thisAs: What’s that even supposed to mean?!
Use a name that clearly communicates the intent.
https://programmingwithmosh.com/csharp/5-tips-for-junior-c-developers-to-write-cleaner-c-code/
Coding Challenge #41
Please stay tuned, SCS Coding Challenge #41 will be published later this month. Details will follow. The winner will be awarded based upon the best approach.