반응형
Description:
Create a function named divisors/Divisors that takes an integer n > 1 and returns an array with all of the integer's divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string '(integer) is prime' (null in C#) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust).
Example:
divisors(12); #should return [2,3,4,6]
divisors(25); #should return [5]
divisors(13); #should return "13 is prime"
Solution:
1. Divide the incoming number from 1 to the incoming number.
2. If there are divisible numbers, put them in an array.
3. Exclude 1 and the last number in the array.
4. Return the remaining array.
5. If the number is not divisible, a string is returned.
def divisors(integer):
return [i for i in range(1,integer+1) if integer % i == 0][1:-1] or f"{integer} is prime"
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
How many pages in a book? (0) | 2022.03.26 |
---|---|
Is n divisible by x and y? (0) | 2022.03.25 |
Sum of Digits / Digital Root (0) | 2022.03.23 |
Dashatize it (0) | 2022.03.22 |
Data Reverse (0) | 2022.03.21 |