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 - 11 years, 1 month ago
One liner... this is fair, right? :)
reply permalink
Adam - 11 years, 1 month ago
Pretty easy in Python:
Closest I could get in C:
reply permalink
Adam - 11 years, 1 month 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 - 11 years, 1 month ago
Check this method out, it might solve your problem :)
http://www.cplusplus.com/reference/cmath/fma/
reply permalink
Pearce Keesling - 11 years, 1 month ago
Had to go digging through the documentation for that one. So far cmath has never let me down :)
reply permalink
Carlos - 11 years, 1 month 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 - 11 years, 1 month ago
may seem wrong at first*
reply permalink
Eli - 11 years, 1 month 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 - 11 years, 1 month ago
More ways than one to represent "/" :)
int divide(float x, float y) { return Math.round(x \u002F y); }
reply permalink
gulliver - 11 years, 1 month ago
Here is what I came up with:
reply permalink
gulliver - 11 years, 1 month ago
whoops, ignore the line with starting_length_list2 - meant to delete that...
reply permalink
gulliver - 11 years, 1 month 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 - 11 years, 1 month ago
Javascript is so cheating...
reply permalink
Hueho - 11 years, 1 month ago
In Ruby it's a one liner:
In C it's a extremely complex operation, very low level and stuff... =)
reply permalink
Hueho - 11 years, 1 month 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 - 11 years, 1 month ago
Very impressive on the C version. I like that you went for implementing your own add and subtract functionality.
reply permalink
Isi - 11 years, 1 month 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 - 11 years, 1 month 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 - 11 years, 1 month ago
reply permalink