diff --git a/snippets/degrees_to_rads.md b/snippets/degrees_to_rads.md index 8c8b3501e..5ee50cce3 100644 --- a/snippets/degrees_to_rads.md +++ b/snippets/degrees_to_rads.md @@ -8,10 +8,10 @@ Converts an angle from degrees to radians. Use `math.pi` and the degrees to radians formula to convert the angle from degrees to radians. ```py -import math +from math import pi def degrees_to_rads(deg): - return (deg * math.pi) / 180.0 + return (deg * pi) / 180.0 ``` ```py diff --git a/snippets/gcd.md b/snippets/gcd.md index 58e7af97b..190619f7e 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -5,16 +5,16 @@ tags: math,beginner Calculates the greatest common divisor of a list of numbers. -Use `reduce()` and `math.gcd` over the given list. +Use `functools.reduce()` and `math.gcd()` over the given list. ```py from functools import reduce -import math +from math import gcd def gcd(numbers): - return reduce(math.gcd, numbers) + return reduce(gcd, numbers) ``` ```py -gcd([8,36,28]) # 4 -``` \ No newline at end of file +gcd([8, 36, 28]) # 4 +``` diff --git a/snippets/rads_to_degrees.md b/snippets/rads_to_degrees.md index 501ef1e74..bc2efc3af 100644 --- a/snippets/rads_to_degrees.md +++ b/snippets/rads_to_degrees.md @@ -8,13 +8,13 @@ Converts an angle from radians to degrees. Use `math.pi` and the radian to degree formula to convert the angle from radians to degrees. ```py -import math +from math import pi def rads_to_degrees(rad): return (rad * 180.0) / math.pi ``` ```py -import math +from math import pi rads_to_degrees(math.pi / 2) # 90.0 ``` diff --git a/snippets/snake.md b/snippets/snake.md index 0b29df99c..61ed16432 100644 --- a/snippets/snake.md +++ b/snippets/snake.md @@ -8,11 +8,12 @@ Converts a string to snake case. Break the string into words and combine them adding `_` as a separator, using a regexp. ```py -import re +from re import sub def snake(s): - return '_'.join(re.sub('([A-Z][a-z]+)', r' \1', - re.sub('([A-Z]+)', r' \1', + return '_'.join( + sub('([A-Z][a-z]+)', r' \1', + sub('([A-Z]+)', r' \1', s.replace('-', ' '))).split()).lower() ```