Polar coordinates are an alternative way of representing Cartesian coordinates or Complex Numbers.

A complex number $z$ 

$z=x+yj$
is completely determined by its real part $\boldsymbol{x}$ and imaginary part $y$. 

Here, $j$ is the imaginary unit.

A polar coordinate ($r,\varphi$)

is completely determined by modulus $\textbf{r}$ and phase angle $\mbox{p}$.

If we convert complex number $z$ to its polar coordinate, we find:

$\textbf{r}$: Distance from $z$ to origin, i.e., $\sqrt{x^2+y^2}$

$\mbox{p}$: Counter clockwise angle measured from the positive $\boldsymbol{x}$-axis to the line segment that joins $z$ to the origin.

Python's cmath module provides access to the mathematical functions for complex numbers.

$c\textit{math.phase}$ 

This tool returns the phase of complex number $z$ (also known as the argument of $z$).

>>> phase(complex(-1.0, 0.0))
3.1415926535897931

$\boldsymbol{abs}$ 

This tool returns the modulus (absolute value) of complex number $z$.

>>> abs(complex(-1.0, 0.0))
1.0

Task 

You are given a complex $z$. Your task is to convert it to polar coordinates.

Input Format

A single line containing the complex number $z$. 
 Note: complex() function can be used in python to convert the input as a complex number.

Constraints

Given number is a valid complex number

Output Format

Output two lines:

 The first line should contain the value of $\textbf{r}$.

 The second line should contain the value of $\mbox{p}$.

Sample Input
  1+2j

Sample Output
 2.23606797749979 
 1.1071487177940904

Note: The output should be correct up to 3 decimal places.

Source: hackerrank | Difficulty: EASY
Tags: Mathematics


Solutions:
import cmath
txt = str(input()).strip()
c = complex(txt)
r = abs(complex(c.real, c.imag))
p = cmath.phase(complex(c.real, c.imag))
print('{0:.3f}'.format(r))
print('{0:.3f}'.format(p))


import cmath
z = input()
k = z[1:len(z)]
if '+' in z:
	(r, i) = z.split('+')
	r = int(r)
	im = int(i[0:len(i) - 1])
	print(abs(complex(r, im)))
	print(cmath.phase(complex(r, im)))
else:
	i = k.index('-')
	(r, i) = (z[0:i + 1], z[i + 1:])
	r = int(r)
	im = int(i[0:len(i) - 1])
	print(abs(complex(r, im)))
	print(cmath.phase(complex(r, im)))


from cmath import *
z = polar(complex(input()))
print(z[0])
print(z[1])


from cmath import *
a = polar(complex(input()))
print(a[0])
print(a[1])


import sys
import cmath
data = sys.stdin.readlines()

def randphi(n):
	n = complex(n)
	print(abs(n))
	print(cmath.phase(n))
for i in data:
	randphi(i)


import cmath
x = complex(input())
print(abs(x))
print(cmath.phase(x))


import cmath
a = complex(input())
print(abs(complex(a.real, a.imag)))
print(cmath.phase(complex(a.real, a.imag)))


import re, cmath
ip = input()
print(abs(complex(ip)))
print(cmath.phase(complex(ip)))


import cmath
import re
complex_string = input()
m = re.search('(-?\\d+)(\\+*)(-?\\d+)', complex_string)
x = float(m.group(1))
y = float(m.group(3))
print(abs(complex(x, y)))
print(cmath.phase(complex(x, y)))


import cmath
import re

def getComplexType(data):
	r = re.search('[-|\\d]+(?=[+|-])', data)
	i = re.search('[+|-]\\d+(?=j$)', data)
	if r:
		r = int(r.group())
	if i:
		i = int(i.group())
	return (r, i)

def getPolar(data):
	(r, i) = getComplexType(data)
	result = cmath.polar(complex(r, i))
	return result
data = input()
print(getPolar(data)[0])
print(getPolar(data)[1])


import cmath
r = complex(input())
print(abs(r))
print(cmath.phase(r))


import cmath
keylist = list(input())
keylist.pop()
flag1 = 1
flag2 = 1
if keylist[0] == '-':
	flag1 = -1
	del keylist[0]
if '-' in keylist:
	keyindex = keylist.index('-')
	num1 = keylist[:keyindex]
	num2 = keylist[keyindex + 1:]
	flag2 = -1
elif '+' in keylist:
	keyindex = keylist.index('+')
	num1 = keylist[:keyindex]
	num2 = keylist[keyindex + 1:]
str1 = ''.join(num1)
str2 = ''.join(num2)
f1 = float(str1) * flag1
f2 = float(str2) * flag2
print(abs(complex(f1, f2)))
print(cmath.phase(complex(f1, f2)))


complexNumber = complex(input())
from cmath import phase
print(abs(complexNumber))
print(phase(complexNumber))


import sys
import cmath
my_number = complex(input())
my_r = abs(my_number)
my_phase = cmath.phase(my_number)
print(my_r)
print(my_phase)


import cmath
z = complex(input())
p = cmath.polar(z)
print(p[0])
print(p[1])


import cmath
a = input()
print(abs(complex(a)))
print(cmath.phase(complex(a)))


from cmath import *
from re import *
s = input()
l = findall('-?[0-9]+[.]?[0-9]*j?', s)
c = 0
d = 0
if len(l) == 1:
	if l[0][-1] == 'j':
		c = 0
		d = float(l[0][:-1])
	else:
		d = 0
		c = float(l[0])
else:
	c = float(l[0])
	d = float(l[1][:-1])
print(abs(complex(c, d)))
print(phase(complex(c, d)))


import cmath
c = complex(input())
(r, phi) = cmath.polar(c)
print(r)
print(phi)


import cmath
z = complex(input())
(a, b) = (z.real, z.imag)
r = abs(complex(a, b))
theta = cmath.phase(complex(a, b))
print(r)
print(theta)


import cmath
z = complex(input())
x = z.real
y = z.imag
r = abs(complex(x, y))
ph = cmath.phase(complex(x, y))
print('%.3f' % r)
print('%.3f' % ph)


import cmath
for i in cmath.polar(complex(input())):
	print(i)


import cmath
z = complex(input())
print(abs(complex(z)))
print(cmath.phase(complex(z)))


import cmath

def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
c = complex(input())
print(abs(c))
print(cmath.phase(c))


import cmath
polar = cmath.polar(complex(input()))
print(polar[0])
print(polar[1])


from cmath import polar
num = complex(input())
pol = polar(num)
print(pol[0])
print(pol[1])


from cmath import phase
c = eval(input())
print(abs(c))
print(phase(c))


from cmath import phase
complexNum = complex(input())
print(abs(complexNum))
print(phase(complexNum))


import cmath
z = complex(input())
print(abs(z))
print(cmath.phase(z))


import cmath
z = complex(input())
(r, t) = cmath.polar(z)
print(r)
print(t)


import cmath, sys
line = next(sys.stdin)
cval = complex(line)
print(abs(cval))
print(cmath.phase(cval))


import cmath
cnum = complex(input())
print(abs(cnum))
print(cmath.phase(cnum))


import cmath
z = complex(input())
print(abs(complex(z.real, z.imag)))
print(cmath.phase(complex(z.real, z.imag)))


import cmath
a = complex(input(''))
b = cmath.polar(a)
print(b[0])
print(b[1])


from cmath import phase
a = input().strip('j')
if '+' in a:
	b = list(map(int, a.split('+')))
else:
	b = list(map(int, a.strip('-').split('-')))
	if a[0] == '-':
		b[0] = b[0] - 2 * b[0]
	b[1] = b[1] - 2 * b[1]
c = b[0] * b[0] + b[1] * b[1]
c = pow(c, 1 / 2)
print(c)
print(phase(complex(b[0], b[1])))


import cmath
ka = complex(input())
print(abs(ka))
print(cmath.phase(ka))


import cmath
N = input()
if N[0] == '-':
	if '-' in N[1:]:
		N = N.split('-')
		Re = -float(N[1])
		Im = -float(N[2][:-1])
	else:
		N = N.split('+')
		Re = float(N[0])
		Im = float(N[1][:-1])
elif '-' in N:
	N = N.split('-')
	Re = float(N[0])
	Im = -float(N[1][:-1])
else:
	N = N.split('+')
	Re = float(N[0])
	Im = float(N[1][:-1])
C = complex(Re, Im)
print(abs(C))
print(cmath.phase(C))


import cmath
n = input()
c = complex(n)
print('{0:0.3f}'.format(abs(c)))
print('{0:0.3f}'.format(cmath.phase(c)))


from cmath import polar
comp = complex(input())
(r, phi) = polar(comp)
print(r)
print(phi)


from cmath import *
c = input()
print(abs(complex(c)))
print(phase(complex(c)))


from cmath import phase
a = complex(input())
print(abs(a))
print(phase(a))


import cmath
c = eval(input())
print(abs(c))
print(cmath.phase(c))


import cmath
x = 0.0
y = 0.0
a = complex(input())
x = float(a.real)
y = float(a.imag)
print(abs(complex(x, y)))
print(cmath.phase(complex(x, y)))


from cmath import phase
a = input()
d = a[:len(a) - 1]
if '+' in d:
	b = list(map(int, d.split('+')))
	c = b[0] * b[0] + b[1] * b[1]
elif '-' in d:
	b = d.split('-')
	b = [x for x in b]
	if b[0] == '':
		b[1] = '-' + b[1]
		b[2] = '-' + b[2]
	else:
		b[1] = '-' + b[1]
	b = [int(x) for x in b if x != '']
	c = b[0] * b[0] + b[1] * b[1]
c = pow(c, 1 / 2)
print(abs(c))
print(phase(complex(b[0], b[1])))


import cmath
n = cmath.polar(complex(input()))
print(n[0])
print(n[1])


from cmath import *
num = polar(complex(input().strip()))
print(num[0])
print(num[1])


import cmath
t = input()
t = complex(t)
x = cmath.polar(t)
print(x[0])
print(x[1])


import cmath
import math
__author__ = 'namii'
inputs = input()[:-1]
flipX = False
if inputs[0] == '-':
	flipX = True
	inputs = inputs[1:]
if '-' in inputs:
	(x, y) = map(int, inputs.split('-'))
	y = -y
elif '+' in inputs:
	(x, y) = map(int, inputs.split('+'))
if flipX:
	x = -x
print(math.sqrt(x ** 2 + y ** 2))
print(cmath.phase(complex(x, y)))


import cmath
C = complex(input())
print(cmath.sqrt(C.real ** 2 + C.imag ** 2).real)
print(cmath.phase(C))


from cmath import phase
s = complex(input())
print(abs(s))
print(phase(s))


import cmath
tmp = complex(input())
p = cmath.polar(tmp)
print(p[0])
print(p[1])


import cmath
z = input()
i = 0
x = str()
if z[i] == '-':
	x += '-'
	i += 1
while z[i] != '+' and z[i] != '-':
	x += z[i]
	i += 1
y = str()
if z[i] == '-':
	y += '-'
i += 1
while z[i] != 'j':
	y += z[i]
	i += 1
if y == '' or y == '-':
	y += '1'
(x, y) = (int(x), int(y))
print(abs(complex(x, y)))
print(cmath.phase(complex(x, y)))


from cmath import phase
b = input()
b = b[0:len(b) - 1]
j = len(b) - 1
while j >= 0:
	if b[j:j + 1] == '+' or b[j:j + 1] == '-':
		break
	j -= 1
a = int(b[0:j])
k = int(b[j:])
c = a * a + k * k
c = pow(c, 1 / 2)
print(c)
print(phase(complex(a, k)))


from cmath import phase
z = complex(input())
print(abs(z))
print(phase(z))


import cmath
cplx = complex(input())
print(abs(cplx))
print(cmath.phase(cplx))


import cmath

def __starting_point():
	cnum = complex(input().strip())
	print(abs(cnum))
	print(cmath.phase(cnum))
__starting_point()


from cmath import phase
number = complex(input())
print(pow(pow(number.real, 2) + pow(number.imag, 2), 1 / 2.0))
print(phase(number))


import cmath
z = complex(input())
print(round(abs(z), 3))
print(round(cmath.phase(z), 3))


import cmath
import re
line = input()
(real, imag) = map(int, re.findall('([\\-\\+]?\\d+)([\\-\\+]\\d+)j', line)[0])
c = complex(real, imag)
print(abs(c))
print(cmath.phase(c))


from cmath import phase
import re
complexNum = input()
mult = 1
x = 0
y = 0
positionOfMinus = complexNum.find('-', 1)
positionOfPlus = complexNum.find('+')
if positionOfPlus == -1 and positionOfMinus == -1:
	if complexNum.find('j') == -1:
		x = float(complexNum)
	else:
		y = float(complexNum[:-1])
else:
	if positionOfMinus != -1:
		mult = -1
	complexNum1 = []
	if mult == 1:
		complexNum1 = complexNum.split('+')
	else:
		complexNum1.append(complexNum[:positionOfMinus])
		complexNum1.append(complexNum[positionOfMinus + 1:])
	x = float(complexNum1[0])
	yComp = complexNum1[1]
	y = mult * float(yComp[:-1])
print(abs(complex(x, y)))
print(phase(complex(x, y)))


import cmath
z = input()
z = complex(z)
print(abs(z))
print(cmath.phase(z))


import cmath
c = complex(input().strip())
print('%.3f\n%.3f' % cmath.polar(c))


import cmath
a = complex(input())
for i in cmath.polar(a):
	print(i)


import cmath

def modulus(z):
	return cmath.sqrt(z.real ** 2 + z.imag ** 2).real
z = complex(input())
print(modulus(z))
print(cmath.phase(z))


from cmath import *
import re
z = input().replace('j', '')
z = re.findall('[-+]?\\d+[\\.]?\\d*', z)
z = list(map(float, z))
print(abs(complex(z[0] * 1.0, z[1] * 1.0)))
print(phase(complex(z[0] * 1.0, z[1] * 1.0)))


from cmath import phase
x = complex(input())
print(abs(x))
print(phase(x))


import cmath
for x in cmath.polar(complex(input())):
	print(x)


from cmath import phase
Z = complex(input())
A = Z.real
B = Z.imag
print(abs(complex(A, B)))
print(phase(complex(A, B)))


from cmath import polar
output = polar(complex(input()))
print(output[0])
print(output[1])


import cmath
c = complex(input())
print(abs(c))
print(cmath.phase(c))


import cmath
z = complex(input())
print(abs(cmath.sqrt(z.real ** 2 + z.imag ** 2)))
print(cmath.phase(z))


import cmath
s = input()
m = abs(complex(s))
phi = cmath.phase(complex(s))
print(m)
print(phi)


import cmath, re
(x, y) = re.findall('-?\\d+', input())
(r, th) = cmath.polar(complex(int(x), int(y)))
print(str(r) + '\n' + str(th))


import math
import re
s = input()
(a, b) = list(map(float, re.findall('[\\+\\-]?\\d+\\.?\\d*', s)))
r = math.sqrt(a ** 2 + b ** 2)
print(r)
fi = math.acos(a / r)
if b < 0:
	fi *= -1
print(fi)


from cmath import phase
c = input()
print(abs(complex(c)))
print(phase(complex(c)))


from cmath import phase
C = complex(input())
print(abs(C))
print(phase(C))


import cmath
a = input()
if '+' in a:
	a = a.split('+')
else:
	a = a.split('-')
	a[-1] = '-' + a[-1]
	if len(a) == 3:
		a = a[1:]
		a[0] = '-' + a[0]
x = int(a[0])
y = int(a[1][:-1])
print((x ** 2 + y ** 2) ** 0.5)
print(cmath.phase(complex(x, y)))


c = complex(input())
print(abs(c))
import cmath
print(cmath.phase(c))


from cmath import phase
s = input()
L = len(s)
for i in range(L - 1, -1, -1):
	if s[i] == '-' or s[i] == '+':
		break
y = int(s[i + 1:-1])
if s[i] == '-':
	y = -y
x = int(s[:i])
r = abs(complex(x, y))
r2 = phase(complex(x, y))
print('%.3f' % r)
print('%.3f' % r2)


import cmath
import re
num = input()
signs = re.findall('[+-]', num)
c = re.split('[+-]', num)
if c[0] == '':
	c = c[1:]
c[1] = c[1][:-1]
if len(signs) == 1:
	real = round(float(c[0]), 3)
	imag = round(float(signs[0] + c[1]), 3)
else:
	real = round(float(signs[0] + c[0]), 3)
	imag = round(float(signs[1] + c[1]), 3)
ang = cmath.phase(complex(real, imag))
mod = abs(complex(real, imag))
print(mod)
print(ang)


s = complex(input())
import cmath
print(format(abs(s), '.03f'))
print(format(cmath.phase(s), '.03f'))


import cmath
z = input()
print(abs(complex(z)))
print(cmath.phase(complex(z)))


import cmath
z = complex(input())
phi = cmath.phase(z)
r = cmath.polar(z)
print('%.3f' % r[0])
print('%.3f' % phi)