Write a function with arguments r, dr that will return a 2D array of size (r/dr, r/dr). In the array "draw" two circles and assign them a value of 1. Separate the two circles by adding a nan circle of equal width.

I thought this would be a nice example of a 5 minute exercise for beginners/intermediate Python coders as it tests several Python skills (looping, masking, axes...).
def Circles(r, dr):       n  = int(x*1E-2/dx)  x  = np.arange(0, r / dr)   #Same reasoning as above  y  = np.arange(0, r / dr) arr = np.full((y.size, x.size), np.nan)
  cx = int(x.size/2)     #centre of the circle on x axis  cy = int(y.size/2)     #centre of the circle on y axis
  for i in range(4):    mask = (x[np.newaxis,:] - cx) ** 2 + (y[:,np.newaxis] - cy) ** 2 < r ** 2 #from standard equation of a circle    arr[mask] = 0 if i % 2 == 0 else np.nan         r -= r/4     #effective width of each circle

Answered by Davor D. Python tutor

1077 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Implement a rocket launch countdown.


Which function is ran when an object is instantiated?


Write a program that can convert between celcius and farenheit temperature scales


Demonstrate a recursive solution to calculate the factorial of a number


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy