Update some snippets
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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]
|
||||
```
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user