Math
average
difference
Returns the difference between two arrays.
-Create a set from b, then use list comprehension to only keep values not contained in b
Returns the difference between two iterables.
+Use list comprehension to only keep values not contained in b
def difference(a, b):
- b = set(b)
return [item for item in a if item not in b]
difference([1, 2, 3], [1, 2, 4]) # [3]@@ -323,7 +316,7 @@ count_vowels('gym') # 0
def byte_size(string):
return(len(string.encode('utf-8')))
-byte_size('π') # 4
+byte_size('Γ°ΕΈΛβ¬') # 4
byte_size('Hello World') # 11
Checks if a string is upper case.
Convert the given string to upper case, using str.upper() method and compare it to the original.
def is_upper_case(str): - return str == str.upper()+
def is_upper_case(string): + return string == string.upper()
is_upper_case('ABC') # True
is_upper_case('a3@$') # True
@@ -387,14 +380,30 @@ is_upper_case('aB4') # False
Checks if a string is lower case.
Convert the given string to lower case, using str.lower() method and compare it to the original.
def is_lower_case(str): - return str == str.lower()+
def is_lower_case(string): + return string == string.lower()
is_lower_case('abc') # True
is_lower_case('a3@$') # True
is_lower_case('Ab4') # False
list
bubble_sort
Bubble_sort uses the technique of comparing and swapping
+ +def bubble_sort(arr): + for passnum in range(len(arr) - 1, 0, -1): + for i in range(passnum): + if arr[i] > arr[i + 1]: + temp = arr[i] + arr[i] = arr[i + 1] + arr[i + 1] = temp+ +
arr = [54,26,93,17,77,31,44,55,20]
+bubble_sort(arr)
+print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]
+