No this isn't a reference to that Linkin Park song. Today's your Introduction to Computer Science mid-term. You're doing alright on the test and the last question is "implement a function that takes in 2 integers and returns the division of the 2 numbers rounded to the nearest integer".
Piece of cake you say. As you're typing it up you exclaim "what the heck", which gets you some really weird stares. You've just learned that your "/" key is broken. Some of you that are having a really bad day learn that your "/", "*", "+", and "-" are all broken. How can we solve this problem without using any of your broken keys? Note: You're using a pirated version of Windows 2015 so your copy and paste functionality is also broken.
Comments:
Steve Mostovoy - 10 years, 9 months ago
One liner... this is fair, right? :)
reply permalink
Adam - 10 years, 9 months ago
Pretty easy in Python:
Closest I could get in C:
reply permalink
Adam - 10 years, 9 months ago
Whoops, the Python needs to be "return round(operator.div(float(a), float(b)))". This only works in Python 2. Python 3 would use operator.truediv.
reply permalink
Pearce Keesling - 10 years, 9 months ago
Check this method out, it might solve your problem :)
http://www.cplusplus.com/reference/cmath/fma/
reply permalink
Pearce Keesling - 10 years, 9 months ago
Had to go digging through the documentation for that one. So far cmath has never let me down :)
reply permalink
Carlos - 10 years, 9 months ago
This doesn't work for negative numbers i believe, the results on the floored version may seem at first glance but, if you run the not floored version with the same parameters, sometimes the java rounds downs by a few nano units or something
reply permalink
Carlos - 10 years, 9 months ago
may seem wrong at first*
reply permalink
Eli - 10 years, 9 months ago
Maybe I always look for a solution that is too easy/lazy. I'd just pop up the virtual keyboard and use it to enter the characters. In fact, I've had to do this before.
reply permalink
Andy - 10 years, 9 months ago
More ways than one to represent "/" :)
int divide(float x, float y) { return Math.round(x \u002F y); }
reply permalink
gulliver - 10 years, 9 months ago
Here is what I came up with:
reply permalink
gulliver - 10 years, 9 months ago
whoops, ignore the line with starting_length_list2 - meant to delete that...
reply permalink
gulliver - 10 years, 9 months ago
cleaned it up a little... def no_divide(int1,int2): temp_list = [0 for i in range(int2)] counter = [] while (int1 >= len(temp_list)): for i in range(int2): temp_list.append(0) counter.append(0) return len(counter)
reply permalink
John - 10 years, 9 months ago
Javascript is so cheating...
reply permalink
Hueho - 10 years, 9 months ago
In Ruby it's a one liner:
In C it's a extremely complex operation, very low level and stuff... =)
reply permalink
Hueho - 10 years, 9 months ago
Oh, the C version only works with positive numbers, but cmon, the teacher was going too hard asking for the whole integer division.
reply permalink
Max Burstein - 10 years, 9 months ago
Very impressive on the C version. I like that you went for implementing your own add and subtract functionality.
reply permalink
Isi - 10 years, 9 months ago
Hey, it's the first time I see you post so I just wanted to thank you for making this site - it has been my motivation to program again for a while now. One of my recent favorite was the Vigenere Cipher, got any similar tasks? It was really fun experimenting with it after I coded it, and I even learned about something new while playing around.
reply permalink
Max Burstein - 10 years, 9 months ago
Glad you're enjoying the site. The vigenere cipher was one of my favorites too. I like those encryption/cracking the code problems. I have an idea for one that may end up on here next week so stay tuned!
reply permalink
Peter - 10 years, 9 months ago
reply permalink