• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 07:16
CEST 13:16
KST 20:16
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL21] Ro24 Preview Pt2: News Flash10[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy21ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book20
Community News
$5,000 WardiTV TLMC tournament - Presented by Monster Energy3GSL CK: More events planned pending crowdfunding7Weekly Cups (May 30-Apr 5): herO, Clem, SHIN win0[BSL22] RO32 Group Stage5Weekly Cups (March 23-29): herO takes triple6
StarCraft 2
General
Team Liquid Map Contest #22 - Presented by Monster Energy Quebec Clan still alive ? BGE Stara Zagora 2026 cancelled Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Weekly Cups (May 30-Apr 5): herO, Clem, SHIN win
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament RSL Revival: Season 5 - Qualifiers and Main Event GSL CK: More events planned pending crowdfunding $5,000 WardiTV TLMC tournament - Presented by Monster Energy Sea Duckling Open (Global, Bronze-Diamond)
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
The PondCast: SC2 News & Results Mutation # 520 Moving Fees Mutation # 519 Inner Power Mutation # 518 Radiation Zone
Brood War
General
JD's Ro24 review BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ ASL21 General Discussion [BSL22] RO32 Group Stage
Tourneys
Escore Tournament StarCraft Season 2 [Megathread] Daily Proleagues [ASL21] Ro24 Group F [BSL22] RO32 Group B - Sunday 21:00 CEST
Strategy
Any training maps people recommend? Fighting Spirit mining rates Muta micro map competition What's the deal with APM & what's its true value
Other Games
General Games
Battle Aces/David Kim RTS Megathread Stormgate/Frost Giant Megathread General RTS Discussion Thread Starcraft Tabletop Miniature Game Nintendo Switch Thread
Dota 2
The Story of Wings Gaming Official 'what is Dota anymore' discussion
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread The China Politics Thread European Politico-economics QA Mega-thread Trading/Investing Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece [Req][Books] Good Fantasy/SciFi books Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] Tokyo Olympics 2021 Thread
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
How Streamers Inspire Gamers…
TrAiDoS
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Iranian anarchists: organize…
XenOsky
ASL S21 English Commentary…
namkraft
StarCraft improvement
iopq
Electronics
mantequilla
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2097 users

A programming lesson I won't forget

Blogs > Barbiero
Post a Reply
Barbiero
Profile Blog Joined September 2010
Brazil5259 Posts
July 09 2014 05:52 GMT
#1
So, I'm a CS student. My parents own a radio company(they are lit. Motorola resellers) and they have their share of needs in the world of computing, but they never actually asked me to do something until now. That's because only now have I been actually learning how to develop stuff properly.

Queue the request. It's a fairly simple system to create deadlines for certain jobs in the company, and show who is delaying or not. It's kinda cool.

The issues are, of course, the details. The request was simple: "we want to provide a time window in hours for a job, and the program has to give a date as the deadline". Simple right? Except that the deadline has to account for work days and hours.

Since I have little experience with the various programming languages, my language of choice is Java. Reason being that I'm more used to it and because I know it's very easy to find well documented libraries for almost everything in Java. And alas, there is joda-time as a sweeet date/time language. Love it.

However, It obviously does not include the concept of work days/hours. So how the f* do I implement that?


Man, have I learned a lot of lessons today. First I tried to implement a new class called WorkDateTime to simulate LocalDateTime. Too complicated and most of the functions were duplicates. Scratched.

I ended up making a function that converts a LocalDateTime to the next work hour compatible: so something at 12:20 would be converted to 13:30 on the same day, and Friday 19:00 would become Monday 08:00 of the next week. Joda-time is really good at that.

But the issue was with addition. Since I had a number of hours to serve as a base for my deadline, I needed to find a way to add these hours into a base time(the time of assignment for the job) and not count the non-working hours. Do I add and then count backwards? Do I try to predict it? Fuck this isn't trivial at all.

While trying to think of the solution, one came to mind that made me chuckle: add the hours one by one. For each addition, verify if the time "overflowed" past work hours. If so, convert the time to the next work hour and add the overflow. Finish the loop when there are neither hours nor overflow to add anymore.

I chuckled because that's one hell of an inefficient algorithm. It runs on more-than-linear time compared to the size of the deadline. I thought "that's stupid!" and went back to trying to figure out an efficient algorithm.

I spent literally 8 hours doing that(with a break to watch Liquid win and Brazil get destroyed by Germany). I still haven't figured out how would I implement the algorithm to consider the work hours when adding the new hour.

What did I do? I implemented the "inefficient" one. Not because I gave up, but because it wasn't worth to go to the next day thinking about it when the "inefficiency" isn't THAT costly: the calculation is done once per part of job(so about 150 times a day tops) and most of the deadlines are 2 to 5 hours, maybe one or another with a 20 hour deadline. When you translate this to computer processing, it's a ridiculously small value. The time I waste trying to figure out the efficient one will never be worth the smudge of efficiency I would harvest.

So I literally brute forced my way through this one. Lesson learned: just because it may not be the most efficient, if the efficiency is negligible the development cost isn't worth it.


On a side note, the IntelliJ Idea is awesome. Now to figure out how to work with databases... *shrug*
♥ The world needs more hearts! ♥
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
July 09 2014 06:03 GMT
#2
5 star for a programming blog.
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
SilverSkyLark
Profile Blog Joined April 2008
Philippines8437 Posts
July 09 2014 09:09 GMT
#3
What IDE are you using?
"If i lost an arm, I would play w3." -IntoTheWow || "Member of Hyuk Hyuk Hyuk cafe. He's the next Jaedong, baby!"
LightTemplar
Profile Blog Joined August 2011
Ireland481 Posts
July 09 2014 09:12 GMT
#4
To quote Knuth:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Before you ever try to come up with a better algorithm to solve the problem you are facing its a really good idea to do some back of the envelope calculations and figure out how much your inefficiency is actually going to cost in real world terms. Its probably a little late to point that out now but hey you learned the lesson. Best of luck continuing with your development!
"Thoughts are always there, the mind can't stop" - Grubby
Noyect
Profile Blog Joined July 2010
Sweden129 Posts
July 09 2014 12:26 GMT
#5
Cool to see a programming blog
Been practicing on and off myself aswell and the algorithms really are the challenging part :D
This question is more intended as quick brainstorming from a student looking to learn rather
than advice from someone knowledgeable :D

Instead of adding the hours one by one and checking to see if they overflow to the next day, could you
not "re-label" the hours so that hours 1-9 are monday, 10-18 are tuesday and so on?
If a job takes 22 hours you'ld know that it falls into the scope of wednesday.
Stegosaurus
Profile Joined March 2011
United States52 Posts
Last Edited: 2014-07-09 14:59:12
July 09 2014 14:58 GMT
#6
On July 09 2014 18:09 SilverSkyLark wrote:
What IDE are you using?



Since he mentions IntelliJ IDEA, I would guess that.

Good on you for jumping on IDEA, it's an incredible IDE.
If you're writing in Java, and trying to use databases, you should definitely consider using Hibernate. It's pretty awesome.
*insert witty comment here*
Barbiero
Profile Blog Joined September 2010
Brazil5259 Posts
July 09 2014 15:42 GMT
#7
On July 09 2014 21:26 Noyect wrote:
Cool to see a programming blog
Been practicing on and off myself aswell and the algorithms really are the challenging part :D
This question is more intended as quick brainstorming from a student looking to learn rather
than advice from someone knowledgeable :D

Instead of adding the hours one by one and checking to see if they overflow to the next day, could you
not "re-label" the hours so that hours 1-9 are monday, 10-18 are tuesday and so on?
If a job takes 22 hours you'ld know that it falls into the scope of wednesday.

This falls off when dealing with holidays, company breaks etc. Basically, later I'll need to have a holiday database as well.

Also yeah I'm using intellij idea
♥ The world needs more hearts! ♥
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
Last Edited: 2014-07-09 23:12:40
July 09 2014 23:11 GMT
#8
On July 09 2014 18:12 LightTemplar wrote:
To quote Knuth:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Before you ever try to come up with a better algorithm to solve the problem you are facing its a really good idea to do some back of the envelope calculations and figure out how much your inefficiency is actually going to cost in real world terms. Its probably a little late to point that out now but hey you learned the lesson. Best of luck continuing with your development!

To be honest, it's really hard to know just how efficient your code has to be, because you don't know if you're slowing database queries or writing inefficient code that occasionally results in crashes/spikes to your services.

I think if you don't know how efficient your code needs to be it can be worthwhile to take the time to think of a way to optimize it slightly.

(I just need to really have a massive failure before I agree with Knuth).
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
CatNzHat
Profile Blog Joined February 2011
United States1599 Posts
July 10 2014 04:55 GMT
#9
How would you go about figuring out 23 work hours from now, starting at 2pm today, as a human, with pen and paper.

This is how you should start when you develop an algorithm.

I would start with breaking it into 2 separate pieces, how many days from now, and how many hours into that day.

23 hours is 2 days (8 hrs each) and 7 hours (23 - (2*8) = 7)
Simply advance current time by 2 days and 7 hours.
But wait, what if it's 12:00pm right now, that would mean I've already worked 3 hours today, so if I go 7 hours forward I'll end up with 10 hours. Delegate back to the same logic, 10 hours is 1 day and 2 hours. Advance a day, start 2 hours in.


We can see a pattern starting to form here, you have a concept of hours in a work day, and you have a concept of how that maps to hours in a day. The same pattern holds for days of work in a week.

If it's the 5th work day in the week and the last hour in that day, adding two hours will have to advance the day, which will advance the week.

The class could be laid out with a configuration something like this:
hours_in_workday: 8
start_of_week: monday
start_of_weekend: saturday
length_of_weekend: 2 days
start_of_day: 9:00am
start_of_lunch: 12:00pm
lunch_duration: 1:00hr

With this information you can construct a pretty simple time keeping system, and configure it as needed. Insert national holidays, etc.. etc..

While this solution is definitely more efficient than yours, I never once considered performance. All I was thinking of was the simplest solution that was easy to understand. I start with how I would solve the problem by hand with pen and paper. It's rather straight forward and kind of trivial when you really think about it. The only 'logic' is a switch to simply say 'is this number of hours larger than the work day' and 'is this number of days larger than the work week'.

Code clarity and simplicity is what allows you to build and maintain complex code bases.

There is a unique skillset for figuring out how you're really solving a problem, I mean if I told you to figure out what time you'd be done with something next week if you start on a wednesday afternoon at 2pm if it were to take you exactly 34 hours and you worked exactly 40 hours a week.

You'd be able to figure it out, and you wouldn't iterate through each hour to do it. You might even optimize your solution without even realizing it, subtracting the number of hours left in the current week since you know it will be some time next week, and then subtracting from that in 8 hour chunks to determine how many days into the next week it would be, and then subtracting 4 to determine how many hours after your lunch break it will be.

Converting that to code is a separate matter, and can be done many different ways, and that is where the real performance issues can be benchmarked and addressed.
rebdomine
Profile Blog Joined October 2010
6040 Posts
July 11 2014 09:22 GMT
#10
On July 09 2014 23:58 Stegosaurus wrote:
Show nested quote +
On July 09 2014 18:09 SilverSkyLark wrote:
What IDE are you using?



Since he mentions IntelliJ IDEA, I would guess that.

Good on you for jumping on IDEA, it's an incredible IDE.
If you're writing in Java, and trying to use databases, you should definitely consider using Hibernate. It's pretty awesome.


I think it is worthwhile to learn the basics of SQL though before jumping straight into Hibernate. I see a lot of people struggling with native SQL cause they've been too used to Hibernate.
"Just because you are correct doesn't mean you are right!"
Count9
Profile Blog Joined May 2009
China10928 Posts
July 13 2014 01:38 GMT
#11
Thought this was gonna be about the scheduling problem and algorithms from the beginning. Also, pretty sure your solution is linear time :p
SpeaKEaSY
Profile Blog Joined December 2010
United States1070 Posts
July 14 2014 11:49 GMT
#12
haha I'd do this sort of thing on math tests where they had you add large numbers in series, I'd just write a quick program for the TI-83+ to brute force the problem rather than solve it analytically
Aim for perfection, settle for mediocrity - KawaiiRice 2014
ZeaL.
Profile Blog Joined April 2009
United States5955 Posts
Last Edited: 2014-07-18 20:32:27
July 18 2014 20:32 GMT
#13
oops
Please log in or register to reply.
Live Events Refresh
WardiTV Team League
11:00
Playoffs Day 3
RotterdaM413
ComeBackTV 329
WardiTV275
Rex71
3DClanTV 38
IndyStarCraft 0
Liquipedia
Sparkling Tuna Cup
10:00
Weekly #127
ByuN vs GgMaChineLIVE!
TBD vs Percival
CranKy Ducklings104
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 413
SortOf 134
Rex 71
MindelVK 30
IndyStarCraft 0
StarCraft: Brood War
Britney 12399
Bisu 1582
PianO 1203
Hyuk 461
EffOrt 370
BeSt 320
Last 205
Rush 191
actioN 185
Killer 183
[ Show more ]
Aegong 149
ZerO 146
Hyun 101
ToSsGirL 88
Backho 72
Mind 47
Free 39
Shinee 37
yabsab 23
Movie 22
Noble 20
GoRush 18
Sea.KH 13
Dota 2
Gorgc3879
XaKoH 729
Fuzer 216
League of Legends
JimRising 425
Counter-Strike
zeus598
x6flipin524
edward127
Other Games
gofns34809
singsing1420
B2W.Neo833
Mew2King66
ZerO(Twitch)15
Organizations
Counter-Strike
PGL880
Other Games
BasetradeTV405
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• Berry_CruncH179
• Adnapsc2 29
• CranKy Ducklings SOOP13
• IndyKCrew
• sooper7s
• Migwel
• LaughNgamezSOOP
• intothetv
• Kozan
• AfreecaTV YouTube
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis2004
• Jankos1735
• TFBlade1156
Upcoming Events
OSC
1h 44m
BSL
7h 44m
Sterling vs Azhi_Dahaki
Napoleon vs Mazur
Jimin vs Nesh
spx vs Strudel
IPSL
7h 44m
Artosis vs TBD
Napoleon vs TBD
Replay Cast
21h 44m
Wardi Open
22h 44m
Afreeca Starleague
22h 44m
Soma vs YSC
Sharp vs sSak
Monday Night Weeklies
1d 4h
Afreeca Starleague
1d 22h
Snow vs PianO
hero vs Rain
WardiTV Map Contest Tou…
1d 22h
GSL
2 days
[ Show More ]
Replay Cast
2 days
Kung Fu Cup
3 days
The PondCast
3 days
WardiTV Map Contest Tou…
3 days
Escore
4 days
WardiTV Map Contest Tou…
4 days
Korean StarCraft League
5 days
CranKy Ducklings
5 days
WardiTV Map Contest Tou…
5 days
IPSL
6 days
WolFix vs nOmaD
dxtr13 vs Razz
BSL
6 days
Sparkling Tuna Cup
6 days
WardiTV Map Contest Tou…
6 days
Liquipedia Results

Completed

Escore Tournament S2: W2
RSL Revival: Season 4
NationLESS Cup

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
StarCraft2 Community Team League 2026 Spring
Nations Cup 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026

Upcoming

Escore Tournament S2: W3
Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
RSL Revival: Season 5
WardiTV TLMC #16
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.