Update some snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 10:13:54 +03:00
parent 0f405f0da9
commit a340ba4492
4 changed files with 8 additions and 29 deletions

View File

@ -9,7 +9,7 @@ Use `set()` on the given list to remove duplicates, compare its length with the
```py
def all_unique(lst):
return len(lst) == len(set(lst))
return len(lst) == len(set(lst))
```
```py

View File

@ -1,16 +1,15 @@
---
title: average
tags: math
tags: math,list,beginner
---
:information_source: Already implemented via `statistics.mean`. `statistics.mean` takes an array as an argument whereas this function takes variadic arguments.
Returns the average of two or more numbers.
Takes the sum of all the `args` and divides it by `len(args)`. The second argument `0.0` in sum is to handle floating point division in `python3`.
Use `sum()` to sum all of the `args` provided, divide by `len(args)`.
```py
def average(*args):
return sum(args, 0.0) / len(args)
return sum(args, 0.0) / len(args)
```
```py

View File

@ -1,21 +0,0 @@
---
title: bubble_sort
tags: list
---
Bubble_sort uses the technique of comparing and swapping
```py
def bubble_sort(lst):
for passnum in range(len(lst) - 1, 0, -1):
for i in range(passnum):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
```
```py
lst = [54,26,93,17,77,31,44,55,20]
bubble_sort(lst)
print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]
```

View File

@ -1,14 +1,15 @@
---
title: byte_size
tags: string
tags: string, beginner
---
Returns the length of a string in bytes.
`utf-8` encodes a given string, then `len` finds the length of the encoded string.
Use `string.encode('utf-8')` to encode the given string and return its length.
```py
def byte_size(string):
return(len(string.encode('utf-8')))
return len(string.encode('utf-8'))
```
```py