This is a pretty silly question I came up with.
So here's the solution. Again, beware of integer overflows.
The idea here is if we negate the number, we can use the max() function to find the minimum number! Nifty eh?
We don't need to use the auxiliary variables, those were added for clarity. Do you have an alternate solution? I'd love to see it. Leave a Comment.
Problem : You are given 3 integers a,b,c. You are given 2 functions, "int max(int,int)" and "void print(int)". Using these 2 function and these only, print the numbers in increasing order. You're allowed to use math operators, but NOT programming language constructs like "if", "for", "while", including the ternary operation etc
So here's the solution. Again, beware of integer overflows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
a,b,c = [random.randint(-100,100) for i in range(3)] | |
print a,b,c | |
#the actual solution | |
mx = max(a,max(b,c)) # max of a,b,c | |
mn = -max(-a,max(-b,-c)) # min of a,b,c, lol! :D | |
mid = a+b+c-mn-mx # middle term | |
print mn,mid,mx |
The idea here is if we negate the number, we can use the max() function to find the minimum number! Nifty eh?
We don't need to use the auxiliary variables, those were added for clarity. Do you have an alternate solution? I'd love to see it. Leave a Comment.
Comments
Post a Comment