Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Worst code ever

Fri Apr 24, 2015 7:55 pm

This is meant to be a (relatively) light-hearted thread, but does touch on a topic which has serious implications for software quality. Please describe (or post code fragments if you wish) some examples of the worst code you've ever encountered. A few of mine:

1. In college I spent a couple of semesters as a grader for a sophomore-level CS data structures course. By midterm, there were still quite a few students in the class who couldn't get their assignments to compile. In one case, it was because the person insisted on coding in a different programming language than the compiler being used for the course (in spite of being informed repeatedly that he really needed to code in Pascal, not PL/1, if he wanted to pass).

2. I was once assigned to debug a sizable body of C++ code that had been written by others. It was full of memory leaks and wild pointer dereferences, and crashed/misbehaved seemingly at random. I discovered that the original coders had used C++ new/delete and C malloc/free more or less interchangeably (e.g. sometimes an object would be allocated with new, but deallocated with free... or vice-versa). After about 6 months of beating my head against that wall I quit that job.

3. Code that returns the address of local (stack) variables as a return value. When queried, author of said code argued that the code was correct because "it worked when I tested it".

4. Code where values of C pointers are saved to a file or database, then reloaded on subsequent runs of the program and used. That'll sort of work if the thing being pointed to is a global/static variable or a function, provided you're always running the exact same version of the code. Change the code at all, and all hell is guaranteed to break loose.

The really scary thing is, in all except #1 the perps were (allegedly) professional software developers. #1 was still moderately scary in that these people had all chosen Computer Science as their career path... I guess some of them probably went on to be the sort of developers who do things like #2 thru #4...
Nostalgia isn't what it used to be.
 
Redocbew
Minister of Gerbil Affairs
Posts: 2495
Joined: Sat Mar 15, 2014 11:44 am

Re: Worst code ever

Fri Apr 24, 2015 10:01 pm

Shortly after I moved into the ecommerce world I inheireted a site which had been hastily put together and had little to no code review done for the last few months before its release into production. The site was riddled with bugs and the kind of odd decisions that can drive developers crazy. Among other things the site allowed for overselling available inventory by not checking inventory levels during checkout. Awesome.

My favorite bug were the shipping estimates of -1 dollar. -1 was being sent back as a sentinel value for addresses which were bogus or for some reason invalid when attempting to calculate an estimate. The site did not account for that before continuing with checkout, so there ended up being 20ish orders placed where they paid the customer for shipping.

One person even called the customer service line asking if everything had gone alright with his order, because seeing a shipping charge of -1 freaked him out.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
chuckula
Minister of Gerbil Affairs
Posts: 2109
Joined: Wed Jan 23, 2008 9:18 pm
Location: Probably where I don't belong.

Re: Worst code ever

Fri Apr 24, 2015 11:27 pm

Maybe not the worst code ever written but at my office we have an intern who is in school and did some basic programming classes. One of them used Python, which is an old favorite of mine. He showed me a structured assignment where the Prof. laid out basic data structures for a card deck and the assignment was for the students to shuffle the deck, deal two hands of cards in a 5 card draw game and print out the results in a human-readable form.

OK, so the problem itself was nothing too crazy and I'm not even going to rag on the intern at my office (he's actually an econ major and the coding is just a useful elective). Instead, I'm going to rag on the professor who clearly took a very low-level C approach to writing a Python program. The professor's "card deck" data structure was just an array of integers from 0 to 51 for a 52 card non-jokered deck. That's it! No types, nothing. To tell cards apart, he used two different sets of hard coded enumerations for the suites and the actual card faces that you would have to use to index into the array (e.g. if Hearts have enum index 2 and you want a jack (index 10) then it's deck [2 * 13 + 10]).

Oh, and the way the assignment was structured, you never actually manipulated or shuffled the deck. Instead, you were just supposed to "extract" cards at random using a simple random number generator.. but guess what? The recommended approach was (I think intentionally) designed to cause bugs since it's most definitely possible (and probably pretty common) to hit the same random number two or more times when drawing 10 cards from a 52 card deck. So the assignment required you to kludge together an inefficient set-inclusion check to make sure you weren't double-drawing the same card from the deck. The original integer array was not supposed to be altered and the "drawn" card were just supposed to sit in a smaller array for each hand.

After that, actually printing out the cards in your hand required you to go back to those enums and do some more kludgy integer to suite & face conversion to get back a human-readable string corresponding to an integer.

AGGGH!!! As an aside, I actually took the problem statement for the program, completely ignored the data structures from the professor, and re-implemented a better version of the program in a fraction of the number of lines using real Python. I get the educational aspect of the program BUT... if you are going to use Python, then you ought to be using real honest to goodness data structures like dictionaries and sets that are deep and important parts of almost all Python programs. Instead, the Prof took the dumbest low-level C approach and said "Yeah all that, but in Python!". Heck, even in C I could have used some much more logical data structures to make the program run much better and avoid the boneheaded bugs that the assignment almost wanted you to trigger intentionally.
4770K @ 4.7 GHz; 32GB DDR3-2133; Officially RX-560... that's right AMD you shills!; 512GB 840 Pro (2x); Fractal Define XL-R2; NZXT Kraken-X60
--Many thanks to the TR Forum for advice in getting it built.
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 9:31 am

@chuckula -

As they say, "Those who can, do; those who can't, teach!"

And yes, there's a mindset you need to get used to if you want to be an effective Python coder. Having used C/C++ for most of my career, learning Python really messed with my head. Also, if I do a project in Python then switch back to C (which is still the language I use the most at work) I keep leaving the damn semicolons off of everything; the first compilation of the first new module I write after switching back comes up with dozens of syntax errors due to missing semicolons! :lol:
Nostalgia isn't what it used to be.
 
chuckula
Minister of Gerbil Affairs
Posts: 2109
Joined: Wed Jan 23, 2008 9:18 pm
Location: Probably where I don't belong.

Re: Worst code ever

Sat Apr 25, 2015 9:49 am

just brew it! wrote:
@chuckula -

As they say, "Those who can, do; those who can't, teach!"

And yes, there's a mindset you need to get used to if you want to be an effective Python coder. Having used C/C++ for most of my career, learning Python really messed with my head. Also, if I do a project in Python then switch back to C (which is still the language I use the most at work) I keep leaving the damn semicolons off of everything; the first compilation of the first new module I write after switching back comes up with dozens of syntax errors due to missing semicolons! :lol:


Think of the semicolons!! :P
P.S. --> You actually can put semicolons in python, they just don't do anything for you. If it makes it easier to stay consistent with C and if you don't mind snobby comments from us Python types* then you could go that way.

* I've don't plenty of C too, and I've done C-code that interfaces with Python in the past.
4770K @ 4.7 GHz; 32GB DDR3-2133; Officially RX-560... that's right AMD you shills!; 512GB 840 Pro (2x); Fractal Define XL-R2; NZXT Kraken-X60
--Many thanks to the TR Forum for advice in getting it built.
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 10:00 am

chuckula wrote:
* I've don't plenty of C too, and I've done C-code that interfaces with Python in the past.

Yeah, the C/Python interface is surprisingly straightforward. I've done a fair bit of hybrid Python/C too -- in some cases for talking to I/O devices, and in other cases for doing number crunching where there was a need to efficiently leverage multiple cores.
Nostalgia isn't what it used to be.
 
chuckula
Minister of Gerbil Affairs
Posts: 2109
Joined: Wed Jan 23, 2008 9:18 pm
Location: Probably where I don't belong.

Re: Worst code ever

Sat Apr 25, 2015 10:03 am

just brew it! wrote:
chuckula wrote:
* I've don't plenty of C too, and I've done C-code that interfaces with Python in the past.

Yeah, the C/Python interface is surprisingly straightforward. I've done a fair bit of hybrid Python/C too -- in some cases for talking to I/O devices, and in other cases for doing number crunching where there was a need to efficiently leverage multiple cores.


Yeah, IIRC, you do quite a bit of low-level hardware stuff that obviously is not Python's strong suite. If you can get the lower-level functionality done in C and still have the benefits of Python for higher-level stuff, then it sounds like a win.
4770K @ 4.7 GHz; 32GB DDR3-2133; Officially RX-560... that's right AMD you shills!; 512GB 840 Pro (2x); Fractal Define XL-R2; NZXT Kraken-X60
--Many thanks to the TR Forum for advice in getting it built.
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Worst code ever

Sat Apr 25, 2015 10:37 am

I've come across and had to argue with developers who had Java code where one thread ended up in an infinite loop - it looked like there were exit conditions from the loop but they were all commented out. Similarly the C code for an Ethernet network interface had a few busy spins "for synchronisation as this can run on multicore processors" :evil:

My latest fight is with the hardware manufacturers over their Wifi drivers. They are all paranoid and NDA'd up the wazoo but their code all looks the same and is similarly terrible. One of my favourites was code that read beyond the buffer length for no reason at all - it didn't actually use the extra data. Most of the time it worked because there would be some other buffer after it in memory, but occasionally it would be at the end of a page and SIGSEGV :evil:

The last block of Wifi driver code I looked at tried to find the id of the device by setting a variable to the id and then later comparing if it was set to the id value - it didn't matter anyway because when they called the actual attach function they passed no parameters and just hard coded the id in the attach function. I don't know if they are employing drug addled monkeys to write the code or whether they accidentally fried their brains with too much RF. What I do know is that they can't code for toffee. :roll:
 
Thrashdog
Gerbil XP
Posts: 344
Joined: Wed Nov 03, 2004 1:16 pm
Location: Kansas City

Re: Worst code ever

Sat Apr 25, 2015 10:41 am

chuckula wrote:
Think of the semicolons!!
P.S. --> You actually can put semicolons in python, they just don't do anything for you. If it makes it easier to stay consistent with C and if you don't mind snobby comments from us Python types* then you could go that way.

* I've don't plenty of C too, and I've done C-code that interfaces with Python in the past.


I'm a low-grade script monkey at best, but to my thinking, ditching all the myriad spurious punctuation is the best part of Python. But then again, maybe you shouldn't be taking my opinion, since I tried on three separate occasions to learn languages from the C family (C, C++, and C#, respectively) and failed spectacularly each time. :lol: Brackets and semicolons and "void", oh my!
 
srg86
Gerbil Team Leader
Posts: 262
Joined: Tue Apr 25, 2006 7:57 am
Location: Madison, WI

Re: Worst code ever

Sat Apr 25, 2015 11:41 am

just brew it! wrote:
2. I was once assigned to debug a sizable body of C++ code that had been written by others. It was full of memory leaks and wild pointer dereferences, and crashed/misbehaved seemingly at random. I discovered that the original coders had used C++ new/delete and C malloc/free more or less interchangeably (e.g. sometimes an object would be allocated with new, but deallocated with free... or vice-versa). After about 6 months of beating my head against that wall I quit that job.


Yeah I've seen this sort of thing as well. The trouble was the guy who wrote it was more used to writing Java and Lisp. Java of course has garbage collection. In this case, as well as mixing up new and free, on this GUI program, the guy never deleted the various windows in the program. If you wanted to re-display the window, he would close (but not destroy) the window, create a new one and display it. That and other problems meant that this thing was taking a significant amount of resources on a large 2 socket Sandy Bridge E system.
Intel Core i7 4790K, Z97, 16GB RAM, 128GB m4 SSD, 480GB M500 SSD, 500GB WD Vel, Intel HD4600, Corsair HX650, Fedora x64.
Thinkpad T460p, Intel Core i5 6440HQ, 8GB RAM, 512GB SSD, Intel HD 530 IGP, Fedora x64, Win 10 x64.
 
anotherengineer
Gerbil Jedi
Posts: 1688
Joined: Fri Sep 25, 2009 1:53 pm
Location: Northern, ON Canada, Yes I know, Up in the sticks

Re: Worst code ever

Sat Apr 25, 2015 12:14 pm

just brew it! wrote:
As they say, "Those who can, do; those who can't, teach!"


I can do, but I would rather tech if I could get a teaching job. Better pay, better benefits, better security, way way better pension, and way, way, way, way more time off!!

(edit - at least last years of high school or community college)

Also I took Fortran 90 and C+ in university and I was in mechanical engineering, was interesting, but never used, touched or seen either since then.
Also did some Matlab, which could make some problems really simple, once you got the basics of writing it into matlab.

My question what is the most common mainstream programming language today?? C#??
Life doesn't change after marriage, it changes after children!
 
Deanjo
Graphmaster Gerbil
Posts: 1212
Joined: Tue Mar 03, 2009 11:31 am

Re: Worst code ever

Sat Apr 25, 2015 12:26 pm

anotherengineer wrote:
My question what is the most common mainstream programming language today?? C#??

Java, followed by C, then C++, then Python then C#.
http://spectrum.ieee.org/static/interac ... -languages
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 12:47 pm

Deanjo wrote:
anotherengineer wrote:
My question what is the most common mainstream programming language today?? C#??

Java, followed by C, then C++, then Python then C#.
http://spectrum.ieee.org/static/interac ... -languages

Link appears to be paywalled.

I'm surprised PHP isn't in the top 3, given its ubiquity in the web app space. I'm also surprised that C is still that close to the top.

Edit:

Found a non-paywalled link: http://spectrum.ieee.org/computing/soft ... -languages

Including VHDL in a list of programming languages seems a bit odd to me?
Nostalgia isn't what it used to be.
 
UnfriendlyFire
Gerbil Team Leader
Posts: 285
Joined: Sat Aug 03, 2013 7:28 am

Re: Worst code ever

Sat Apr 25, 2015 1:35 pm

At my work, we have a handful of 1970's industrial controls systems, with 4 to 16 kilobytes of system memory/storage and no way of expanding the storage capacity. Because the memory and storage are on the same volatile memory, turning off the systems will wipe the entire memory. Replacing those control systems cost several million dollars and a few months of no operation, so upgrades are out of the question.

Those 1970's systems are connected to newer controls systems in order to be connected to the Ethernet network. (Comparison: an IBM PC supported up to 640 KB of system RAM)

There aren't any comments in those 1970's systems due to lack of memory, and sometimes engineers have to "compress" the existing coding because their coding won't fit within the kilobytes of memory. Oh, and nobody ever updates the documentation for those machines.

It was no surprise when someone was trying to troubleshoot a machine, but ended up causing it to operate out of sequence and break lots of mechanical stuff.

There's also a limitation of how much information that can be passed to the newer systems and then to the Ethernet network, due to the equally-ancient system-to-system connection having less bandwidth than a 28.8 kbit/s dial-up connection.



just brew it! wrote:
1. In college I spent a couple of semesters as a grader for a sophomore-level CS data structures course. By midterm, there were still quite a few students in the class who couldn't get their assignments to compile. In one case, it was because the person insisted on coding in a different programming language than the compiler being used for the course (in spite of being informed repeatedly that he really needed to code in Pascal, not PL/1, if he wanted to pass).


Hm. The programming courses I took required the codes to compile or no points will be awarded. Though there were people who didn't submit their homework at all, and later failed the course or barely made it with a D-. They could've at least typed in this piece of code:

int main()
{
  printf("Sorry I couldn't finish this assignment\n");
  return 0;
}


And get like 1-3 points out of 100.
 
Redocbew
Minister of Gerbil Affairs
Posts: 2495
Joined: Sat Mar 15, 2014 11:44 am

Re: Worst code ever

Sat Apr 25, 2015 1:57 pm

Most of my courses were like that also. If it didn't compile the best you could get was something like 15 or 20 out of 100.

just brew it! wrote:
I'm surprised PHP isn't in the top 3, given its ubiquity in the web app space. I'm also surprised that C is still that close to the top.

Edit: Found a non-paywalled link: http://spectrum.ieee.org/computing/soft ... -languages

Including VHDL in a list of programming languages seems a bit odd to me?


Yeah that's weird. I always thought VHDL was just for modeling.

I wonder if PHP is in the process of getting edged out by Ruby. I see a lot more postings including Ruby than I do PHP despite how much PHP code is around these days.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
Chuckaluphagus
Gerbil Elite
Posts: 906
Joined: Fri Aug 25, 2006 4:29 pm
Location: Boston area, MA

Re: Worst code ever

Sat Apr 25, 2015 2:03 pm

UnfriendlyFire wrote:
At my work, we have a handful of 1970's industrial controls systems, with 4 to 16 kilobytes of system memory/storage and no way of expanding the storage capacity. Because the memory and storage are on the same volatile memory, turning off the systems will wipe the entire memory. Replacing those control systems cost several million dollars and a few months of no operation, so upgrades are out of the question.

This strikes me as a company-obliterating disaster waiting to happen. I'd be a freaking nervous wreck if I had to interact with or be responsible for those.

[Edited because prepositions are important.]
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 2:07 pm

Redocbew wrote:
just brew it! wrote:
Including VHDL in a list of programming languages seems a bit odd to me?

Yeah that's weird. I always thought VHDL was just for modeling.

Well... it *can* be compiled directly to an FPGA or ASIC design. But I still don't consider it to be a programming language in the traditional sense since the end result isn't code that gets executed per se, it is a hardware design (i.e. an interconnection pattern of logic gates and wires).

Redocbew wrote:
I wonder if PHP is in the process of getting edged out by Ruby. I see a lot more postings including Ruby than I do PHP despite how much PHP code is around these days.

I'd heard recently that Ruby may have peaked as well. According to the linked chart, Python is now more widely used than either one of them.
Nostalgia isn't what it used to be.
 
BIF
Minister of Gerbil Affairs
Posts: 2458
Joined: Tue May 25, 2004 7:41 pm

Re: Worst code ever

Sat Apr 25, 2015 2:18 pm

Chuckaluphagus wrote:
UnfriendlyFire wrote:
At my work, we have a handful of 1970's industrial controls systems, with 4 to 16 kilobytes of system memory/storage and no way of expanding the storage capacity. Because the memory and storage are on the same volatile memory, turning off the systems will wipe the entire memory. Replacing those control systems cost several million dollars and a few months of no operation, so upgrades are out of the question.

This strikes me as a company-obliterating disaster waiting to happen. I'd be a freaking nervous wreck if I had to interact with or be responsible for those.

[Edited because prepositions are important.]


I agree. This is like having no emergency plan. If a disaster ever struck them, they'd be out of business and maybe could find themselves sued for breach of ... everything. :-?
 
srg86
Gerbil Team Leader
Posts: 262
Joined: Tue Apr 25, 2006 7:57 am
Location: Madison, WI

Re: Worst code ever

Sat Apr 25, 2015 2:25 pm

just brew it! wrote:
Deanjo wrote:
anotherengineer wrote:
My question what is the most common mainstream programming language today?? C#??

Java, followed by C, then C++, then Python then C#.
http://spectrum.ieee.org/static/interac ... -languages

Link appears to be paywalled.

I'm surprised PHP isn't in the top 3, given its ubiquity in the web app space. I'm also surprised that C is still that close to the top.

Edit:

Found a non-paywalled link: http://spectrum.ieee.org/computing/soft ... -languages

Including VHDL in a list of programming languages seems a bit odd to me?


Another source, that has data going back at least 30 years i the TIOBE Index.

http://www.tiobe.com/index.php/content/ ... index.html
Intel Core i7 4790K, Z97, 16GB RAM, 128GB m4 SSD, 480GB M500 SSD, 500GB WD Vel, Intel HD4600, Corsair HX650, Fedora x64.
Thinkpad T460p, Intel Core i5 6440HQ, 8GB RAM, 512GB SSD, Intel HD 530 IGP, Fedora x64, Win 10 x64.
 
dmjifn
Gerbil First Class
Posts: 103
Joined: Thu May 15, 2003 4:21 pm
Location: Indianapolis

Re: Worst code ever

Sat Apr 25, 2015 3:11 pm

Some of the worst code I've seen recently is in interviews where the candidate and I whiteboard a couple softball code problems. I ask a question that's pretty much Project Euler #1. This is tough exercise for most just because you're on the spot and out of your element. But we take pains to ease candidates into it. And I'm super liberal in accepting reasonable pseudocode, even letting them assume operators and API functions that don't exist but really probably could. Everyone we interview has a good resume listing very-likely-sounding accomplishments in the area of C# and .NET. Shockingly, less than half can even work through the logic of the problem, much less write the code.
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 5:26 pm

dmjifn wrote:
Some of the worst code I've seen recently is in interviews where the candidate and I whiteboard a couple softball code problems. I ask a question that's pretty much Project Euler #1. This is tough exercise for most just because you're on the spot and out of your element. But we take pains to ease candidates into it. And I'm super liberal in accepting reasonable pseudocode, even letting them assume operators and API functions that don't exist but really probably could. Everyone we interview has a good resume listing very-likely-sounding accomplishments in the area of C# and .NET. Shockingly, less than half can even work through the logic of the problem, much less write the code.

That is truly scary. Although given some of the code I've seen "out in the wild" I guess I must acknowledge that professional developers with that shocking a lack of ability do exist. The solution is a half-dozen-ish lines of C (no external APIs/libraries required other than the ability to write to stdout), or, quite literally, a single line of Python. :roll:

FWIW the 1-line Python solution is:
print sum(set(range(3,1000,3)+range(5,1000,5)))
Nostalgia isn't what it used to be.
 
UnfriendlyFire
Gerbil Team Leader
Posts: 285
Joined: Sat Aug 03, 2013 7:28 am

Re: Worst code ever

Sat Apr 25, 2015 5:42 pm

BIF wrote:
Chuckaluphagus wrote:
UnfriendlyFire wrote:
At my work, we have a handful of 1970's industrial controls systems, with 4 to 16 kilobytes of system memory/storage and no way of expanding the storage capacity. Because the memory and storage are on the same volatile memory, turning off the systems will wipe the entire memory. Replacing those control systems cost several million dollars and a few months of no operation, so upgrades are out of the question.

This strikes me as a company-obliterating disaster waiting to happen. I'd be a freaking nervous wreck if I had to interact with or be responsible for those.

[Edited because prepositions are important.]


I agree. This is like having no emergency plan. If a disaster ever struck them, they'd be out of business and maybe could find themselves sued for breach of ... everything. :-?


The controls systems are still in good conditions. The problem is that changing the coding is always a pain, and the compiler that converts the code that we type on our computers and write it to the control systems isn't guaranteed to work with Windows 8/10. The compiler uses Windows 7's compatibility mode.

The newer controls systems' compiler still gets software updates, but someone high up doesn't see a need to tear out a "perfectly fine" +40 years old equipment and replace it with modern ones.
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Worst code ever

Sat Apr 25, 2015 5:51 pm

dmjifn wrote:
I'm assuming that the preferred solution is to solve this mathematically rather than through iteration?
The count of multiples of 3 is int((max-1)/3). Let's call that C3.
The average of those multiples is C3 x 3/2. Let's call that A3.
The sum is (C3+1) x A3.
Programming it, you'd have to be careful about data types, but when I do it in Excel, it treats everything as a real. :P
· R7-5800X, Liquid Freezer II 280, RoG Strix X570-E, 64GiB PC4-28800, Suprim Liquid RTX4090, 2TB SX8200Pro +4TB S860 +NAS, Define 7 Compact, Super Flower SF-1000F14TP, S3220DGF +32UD99, FC900R OE, DeathAdder2
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 5:58 pm

JustAnEngineer wrote:
dmjifn wrote:
I'm assuming that the preferred solution is to solve this mathematically rather than through iteration?
The count of multiples of 3 is int((max-1)/3). Let's call that C3.
The average of those multiples is C3 x 3/2. Let's call that A3.
The sum is (C3+1) x A3.
Programming it, you'd have to be careful about data types, but when I do it in Excel, it treats everything as a real. :P

Not sure how you knock out the duplicates without iterating (at least implicitly, as in the Python solution... the set() function removes any duplications from the two range() functions).
Nostalgia isn't what it used to be.
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Worst code ever

Sat Apr 25, 2015 6:06 pm

You could get rid of duplicates by plugging in the product (3x5) into the formula, then subtracting it from the sum of the result for 3 and the result for 5.
· R7-5800X, Liquid Freezer II 280, RoG Strix X570-E, 64GiB PC4-28800, Suprim Liquid RTX4090, 2TB SX8200Pro +4TB S860 +NAS, Define 7 Compact, Super Flower SF-1000F14TP, S3220DGF +32UD99, FC900R OE, DeathAdder2
 
meerkt
Gerbil Jedi
Posts: 1754
Joined: Sun Aug 25, 2013 2:55 am

Re: Worst code ever

Sat Apr 25, 2015 6:07 pm

UnfriendlyFire wrote:
the compiler that converts the code that we type on our computers and write it to the control systems isn't guaranteed to work with Windows 8/10. The compiler uses Windows 7's compatibility mode.
So a VM or DOSBox?
 
anotherengineer
Gerbil Jedi
Posts: 1688
Joined: Fri Sep 25, 2009 1:53 pm
Location: Northern, ON Canada, Yes I know, Up in the sticks

Re: Worst code ever

Sat Apr 25, 2015 6:10 pm

dmjifn wrote:
Some of the worst code I've seen recently is in interviews where the candidate and I whiteboard a couple softball code problems. I ask a question that's pretty much Project Euler #1. This is tough exercise for most just because you're on the spot and out of your element. But we take pains to ease candidates into it. And I'm super liberal in accepting reasonable pseudocode, even letting them assume operators and API functions that don't exist but really probably could. Everyone we interview has a good resume listing very-likely-sounding accomplishments in the area of C# and .NET. Shockingly, less than half can even work through the logic of the problem, much less write the code.


Hmmm checked out that problem and I didn't even understand the question, had to wiki natural numbers, a.k.a. whole numbers which is what I know/familiar with the term, maybe they are just taught as whole numbers up here??

And have little to no coding experience if I had to pick a language to that problem it would be MatLab, which surprisingly is #10 in rank!! Although JBI 1-liner in python is pretty nice.

I would have to question whether is was did they not understand what the question was asking or just suck at programming?!?!?
Life doesn't change after marriage, it changes after children!
 
just brew it!
Administrator
Topic Author
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Worst code ever

Sat Apr 25, 2015 6:14 pm

JustAnEngineer wrote:
You could get rid of duplicates by plugging in the product (3x5) into the formula, then subtracting it from the sum of the result for 3 and the result for 5.

Unless I am misunderstanding you, that only knocks out the first duplicate.

Edit: Oh, wait I think I see where you're going, never mind.
Nostalgia isn't what it used to be.
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Worst code ever

Sat Apr 25, 2015 6:25 pm

I'd already discarded the spreadsheet (which I had to use because my calculator is in the other room) but I am certain that anything that is divisible by both 3 and 5 is divisible by 15. This is the union of the two sets.
For 100, 3 and 5, I get 1683 + 950 - 315 = 2318.
For 1000, 3 and 5, I get 166833 + 99500 - 33165 = 233168.
· R7-5800X, Liquid Freezer II 280, RoG Strix X570-E, 64GiB PC4-28800, Suprim Liquid RTX4090, 2TB SX8200Pro +4TB S860 +NAS, Define 7 Compact, Super Flower SF-1000F14TP, S3220DGF +32UD99, FC900R OE, DeathAdder2
 
dmjifn
Gerbil First Class
Posts: 103
Joined: Thu May 15, 2003 4:21 pm
Location: Indianapolis

Re: Worst code ever

Sat Apr 25, 2015 6:30 pm

just brew it! wrote:
FWIW the 1-line Python solution is:
print sum(set(range(3,1000,3)+range(5,1000,5)))

Ha. The C# / LINQ 1-line solution is similar - something like:
Enumerable.Range(1,1000).Where(x => (x % 3 == 0 || x % 5 == 0)).Sum()

JustAnEngineer wrote:
dmjifn wrote:
I'm assuming that the preferred solution is to solve this mathematically rather than through iteration?

That may be true for actual Project Euler but, for me, any working solution is a winner. Frankly, I just use it as a glorified Fizz Buzz problem. I want to see if they can code and how they solve code problems. I'm up there with them. If they want to break for daylight and just write it, cool. If they want to talk it through, cool. I ask questions if they get quiet and thinky, and I try to co-solve it without getting "teachy".

Literally what I do is this:
Myself in the interview wrote:
Say we're looking at the numbers 1 through 10. (Write them on the board.) If we consider the numbers that are multiples of 3 or 5, we would have 3, 5, 6, 9, and 10. (Draw short arrows from those numbers.) The sum of those is 33. (Write "3 + 5 + 6 + 9 + 10 = 33" at the end of those arrows.) Would you write a function to do this. So if I give you "10" you give me "33". (Circle 10 and 33.)

Then I write the function stub, assuming C#:
public int SumIt(int n) {
    // Your code here

    return sum;
}

Some candidates don't get that "10" goes in "n", even if I clarify I want to call "SumIt(10)" and get back "33". Some don't know how to figure out whether a number is a multiple of another. So I will even say "Let's say we have a function called "bool IsMultiple(int x, int y)" that tells us if X is a multiple of Y. So IsMultiple(6, 3) is true and IsMultiple(7, 3) is false. This helped about a third of the people struggling. One guy even said he didn't know how to loop without using foreach over a List, so I told him he could assume I passed in a list of numbers instead and he still couldn't solve the problem.

And now anyone who reads TR and came in for our interview knows exactly who I am and why they weren't called back!

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On