carre.py

Created by nicolas-patrois

Created on April 25, 2018

543 Bytes

Calcule l’image d’un intervalle par la fonction carré.

usage : carre(“]-1,2]”) affiche “[0,4]”


def carre(interval):
 c0,c1=interval[0],interval[-1]
 a,b=map(int,interval[1:-1].split(","))
 inv={"]":"[","[":"]"}

 ch="%s%d,%d%s"
 if a*b>0:
  if a>0:
   print(ch%(c0,a*a,b*b,c1))
  else:
   print(ch%(inv[c1],b*b,a*a,inv[c0]))
 elif a*b==0:
  if a*a>0:
   print(ch%(inv[c1],0,a*a,inv[c0]))
  else:
   print(ch%(c0,0,b*b,c1))
 else:
  if a*a==b*b:
   if c0=="]" and c1=="[":
    print(ch%("[",0,a*a,"["))
   else:
    print(ch%("[",0,a*a,"]"))
  elif a*a<b*b:
   print(ch%("[",0,b*b,c1))
  else:
   print(ch%("[",0,a*a,inv[c0]))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.