Sort Circles By Area (OOP)

Sort Circles By Area (OOP)

Medium Programming Interview OOP 27 views
Explanation Complexity

Problem Statement

Input provides {x}. Create Circle class with area() and sort circles by area ascending. Output sorted radii.

Input Format

First line n. Second line n radii.

Output Format

One line sorted radii.

Example

4
3 1 2 2
1 2 2 3

Constraints

1

Input / Output Format

Input Format
First line n. Second line n radii.
Output Format
One line sorted radii.
Constraints
1

Examples

Input:
4 3 1 2 2
Output:
1 2 2 3

Example Solution (Public)

Programming Interview
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
r=list(map(int,p[1:1+n]))
pi=3.141592653589793
class Circle:
  def __init__(self,r):
    self.r=r
  def area(self):
    return pi*self.r*self.r
arr=[Circle(x) for x in r]
arr.sort(key=lambda c:(c.area(),c.r))
sys.stdout.write(' '.join(str(c.r) for c in arr))

Official Solution Code

import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
r=list(map(int,p[1:1+n]))
pi=3.141592653589793
class Circle:
  def __init__(self,r):
    self.r=r
  def area(self):
    return pi*self.r*self.r
arr=[Circle(x) for x in r]
arr.sort(key=lambda c:(c.area(),c.r))
sys.stdout.write(' '.join(str(c.r) for c in arr))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.