diff --git a/scripts/lint.py b/scripts/lint.py index 1e8dd29c7..1b7ea35e6 100644 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -2,6 +2,7 @@ import util import subprocess import sys for snippet in util.read_snippets(): + print(snippet.name) code = snippet.read_code() check_1 = subprocess.run(['flake8', '-','--select=E901,E999,F821,F822,F823','--count','--show-source','--statistics'], input=code, encoding='utf8',stdout=subprocess.PIPE) check_2 = subprocess.run(['flake8', '-','--exit-zero','--max-complexity=10','--count','--max-line-length=127','--statistics','--ignore=W292'], input=code, encoding='utf8',stdout=subprocess.PIPE) diff --git a/snippets/difference.md b/snippets/difference.md index e6adc0298..949a26e75 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -6,7 +6,7 @@ Use list comprehension to only keep values not contained in `b` ```python def difference(a, b): - Β  Β return [item for item in a if item not in b] + return [item for item in a if item not in b] ``` ``` python difference([1, 2, 3], [1, 2, 4]) # [3] diff --git a/website/app/snippets b/website/app/snippets deleted file mode 100644 index 28ea1fabc..000000000 --- a/website/app/snippets +++ /dev/null @@ -1,8 +0,0 @@ -count_vowels -byte_size -capitalize -capitalize_every_word -decapitalize -palindrome -is_upper_case -is_lower_case \ No newline at end of file diff --git a/website/app/templates/index.html b/website/app/templates/index.html index 0551a1b67..724038a4f 100644 --- a/website/app/templates/index.html +++ b/website/app/templates/index.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% block content %}

Math

average

+{% block content %}

Math

average

β„Ή 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 python2.

@@ -99,8 +99,7 @@ lcm([1, 3, 4], 5) # 60 def max_n(arr, n=1): numbers = deepcopy(arr) - numbers.sort() - numbers.reverse() + numbers.sort(reverse=True) return numbers[:n]
max_n([1, 2, 3]) # [3]
@@ -143,7 +142,7 @@ def chunk(arr, size):
 

Use filter() to filter out falsey values (False, None, 0, and "").

def compact(arr):
-    return list(filter(lambda x: bool(x), arr))
+ return list(filter(bool, arr))
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
@@ -151,15 +150,10 @@ def chunk(arr, size):

count_occurences

β„Ή Already implemented via list.count().

Counts the occurrences of a value in an list.

-

Uses the reduce functin from built-in module functools to increment a counter each time you encounter the specific value inside the list.

+

Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.

-
from functools import reduce
-
-
-def count_occurences(arr, val):
-    return reduce(
-        (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
-        arr)
+
def count_occurrences(lst, val):
+    return len([x for x in lst if x == val and type(x) == type(val)])
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
@@ -188,11 +182,10 @@ def deep_flatten(arr):

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
@@ -375,8 +368,8 @@ decapitalize('FooBar', True) # 'fOOBAR'

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]
+
+
{% endblock %} \ No newline at end of file diff --git a/website/main.py b/website/main.py index ea6bf0897..9409634fa 100644 --- a/website/main.py +++ b/website/main.py @@ -8,7 +8,7 @@ codeRe = "```\s*python([\s\S]*?)```" def tagger(): tag_data = open('tag_database').read() tag_dict = {} - tag_list = tag_data.split('\n') + tag_list = filter(lambda x:x.strip() != '',tag_data.split('\n')) for tag in tag_list: category = tag.split(':')[1] snippet = tag.split(':')[0]