answer data = [4,6,18,34,97,133] ------code to be refactored------ for x in data: #Main…
- User
answer
data = [4,6,18,34,97,133]
------code to be refactored------
for x in data: #Main Loop To Be Refactored prime = True for y in range(2,x): if x == 0: break if x%y == 0: print(f'%d is divisible by %d.'%(x,y)) prime = False if prime and x != 1: print('%d is prime!'%x)
x += 1 prime = True for y in range(2,x): if x == 0: break if x%y == 0: print(f'%d is divisible by %d.'%(x,y)) prime = False if prime and x != 1: print('%d is prime!'%x)----- end of code block to be refactored ------
----- Option A -----
def my_function(data): for x in data: prime = True for y in range(2,x): if x == 0: break if x%y == 0: print(f'%d is divisible by %d.'%(x,y)) prime = False if prime and x != 1: print('%d is prime!'%x)
x += 1 prime = True for y in range(2,x): if x == 0: break if x%y == 0: print(f'%d is divisible by %d.'%(x,y)) prime = False if prime and x != 1: print('%d is prime!'%x)----- End Option A -----
----- Option B -----
for x in data: #Main loop def check_divisibility(x): prime = True for y in range(2,x): if x == 0: break if x%y ==0: prime = False print(f'%d is divisible by %d.'%(x,y)) if prime and number != 1: print("%d is prime!"%number)
check_divisibility(x) x += 1 check_divisibility(x)----- End Option B -----
----- Option C -----
def check_divisibility(number): if number == 0: return prime = True for divisor in range(2,number): if number%divisor ==0: prime = False print(f'%d is divisible by %d.'%(number,divisor)) if prime and number != 1: print("%d is prime!"%number)
for x in data: #Main Loop check_divisibility(x) x += 1 check_divisibility(x)
---- End Option C ----
Which option is the best way to refactor the code above?
- Assistant