Update some snippets
This commit is contained in:
@ -1,18 +1,23 @@
|
||||
---
|
||||
title: factorial
|
||||
tags: math
|
||||
tags: math,recursion,beginner
|
||||
---
|
||||
|
||||
Calculates the factorial of a number.
|
||||
|
||||
Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number.
|
||||
Use recursion.
|
||||
If `num` is less than or equal to `1`, return `1`.
|
||||
Otherwise, return the product of `num` and the factorial of `num - 1`.
|
||||
Throws an exception if `num` is a negative or a floating point number.
|
||||
|
||||
```py
|
||||
def factorial(num):
|
||||
if not ((num >= 0) & (num % 1 == 0)):
|
||||
raise Exception(
|
||||
f"Number( {num} ) can't be floating point or negative ")
|
||||
return 1 if num == 0 else num * factorial(num - 1)
|
||||
if not ((num >= 0) & (num % 1 == 0)):
|
||||
raise Exception(
|
||||
f"Number( {num} ) can't be floating point or negative ")
|
||||
return 1 if num == 0 else num * factorial(num - 1)
|
||||
```
|
||||
|
||||
```py
|
||||
factorial(6) # 720
|
||||
```
|
||||
|
||||
@ -1,35 +0,0 @@
|
||||
---
|
||||
title: fermat_test
|
||||
tags: math
|
||||
---
|
||||
Checks if the number is prime or not. Returns True if passed number is prime, and False if not.
|
||||
|
||||
The function uses Fermat's theorem.
|
||||
First, it picks the number `A` in range `1`..`(n-1)`, then it checks if `A` to the power of `n-1` modulo `n` equals `1`.
|
||||
If not, the number is not prime, else it's pseudoprime with probability 1/2. Applying this test `k `times we have probability `1/(2^k)`.
|
||||
For example, if the number passes the test `10` times, we have probability `0.00098`.
|
||||
|
||||
```py
|
||||
from random import randint
|
||||
|
||||
|
||||
def fermat_test(n, k=100):
|
||||
if n <= 1:
|
||||
return False
|
||||
for i in range(k):
|
||||
a = randint(1, n - 1)
|
||||
if pow(a, n - 1, n) != 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
```
|
||||
|
||||
```py
|
||||
fermat_test(0) # False
|
||||
fermat_test(1) # False
|
||||
fermat_test(561) # False
|
||||
fermat_test(41041) # False
|
||||
fermat_test(17) # True
|
||||
fermat_test(162259276829213363391578010288127) # True
|
||||
fermat_test(-1) # False
|
||||
```
|
||||
@ -1,23 +1,24 @@
|
||||
---
|
||||
title: fibonacci
|
||||
tags: math
|
||||
tags: math,list,intermediate
|
||||
---
|
||||
|
||||
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
||||
|
||||
Starting with 0 and 1, adds the sum of the last two numbers of the list to the end of the list using ```list.append()``` until the length of the list reaches n. If the given nth value is 0 or less, the method will just return a list containing 0.
|
||||
Starting with `0` and `1`, use `list.apoend() to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches `n`.
|
||||
If `n` is less or equal to `0`, return a list containing `0`.
|
||||
|
||||
```py
|
||||
def fibonacci(n):
|
||||
if n <= 0:
|
||||
return [0]
|
||||
if n <= 0:
|
||||
return [0]
|
||||
|
||||
sequence = [0, 1]
|
||||
while len(sequence) <= n:
|
||||
# Add the sum of the previous two numbers to the sequence
|
||||
next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]
|
||||
sequence.append(next_value)
|
||||
sequence = [0, 1]
|
||||
while len(sequence) <= n:
|
||||
next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]
|
||||
sequence.append(next_value)
|
||||
|
||||
return sequence
|
||||
return sequence
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,20 +0,0 @@
|
||||
---
|
||||
title: fibonacci_until_num
|
||||
tags: math
|
||||
---
|
||||
Returns the n-th term in a Fibonnaci sequence that starts with 1
|
||||
|
||||
A term in a Fibonnaci sequence is the sum of the two previous terms.
|
||||
This function recursively calls the function to find the n-th term.
|
||||
|
||||
```py
|
||||
def fibonacci_until_num(n):
|
||||
if n < 3:
|
||||
return 1
|
||||
return fibonacci_until_num(n - 2) + fibonacci_until_num(n - 1)
|
||||
```
|
||||
|
||||
```py
|
||||
fibonnaci_until_num(5) # 5
|
||||
fibonnaci_until_num(15) # 610
|
||||
```
|
||||
@ -1,17 +1,18 @@
|
||||
---
|
||||
title: gcd
|
||||
tags: math
|
||||
tags: math,beginner
|
||||
---
|
||||
|
||||
Calculates the greatest common divisor of a list of numbers.
|
||||
|
||||
Uses the reduce function from the inbuilt module `functools`. Also uses the `math.gcd` function over a list.
|
||||
Use `reduce()` and `math.gcd` over the given list.
|
||||
|
||||
```py
|
||||
from functools import reduce
|
||||
import math
|
||||
|
||||
def gcd(numbers):
|
||||
return reduce(math.gcd, numbers)
|
||||
return reduce(math.gcd, numbers)
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
---
|
||||
title: has_duplicates
|
||||
tags: list
|
||||
tags: list,beginner
|
||||
---
|
||||
Checks a flat list for duplicate values. Returns True if duplicate values exist and False if values are all unique.
|
||||
|
||||
This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.
|
||||
Returns `True` if there are duplicate values in a flast list, `False` otherwise.
|
||||
|
||||
Use `set()` on the given list to remove duplicates, compare its length with the length of the list.
|
||||
|
||||
```py
|
||||
def has_duplicates(lst):
|
||||
return len(lst) != len(set(lst))
|
||||
return len(lst) != len(set(lst))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
---
|
||||
title: insertion_sort
|
||||
tags: list
|
||||
---
|
||||
On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting!
|
||||
|
||||
```py
|
||||
def insertion_sort(lst):
|
||||
|
||||
for i in range(1, len(lst)):
|
||||
key = lst[i]
|
||||
j = i - 1
|
||||
while j >= 0 and key < lst[j]:
|
||||
lst[j + 1] = lst[j]
|
||||
j -= 1
|
||||
lst[j + 1] = key
|
||||
```
|
||||
|
||||
```py
|
||||
lst = [7,4,9,2,6,3]
|
||||
insertionsort(lst)
|
||||
print('Sorted %s' %lst) # sorted [2, 3, 4, 6, 7, 9]
|
||||
```
|
||||
Reference in New Issue
Block a user