Py-Script test using BulmaCSS

Small project to demonstrate the integration of py-script code inside an HTML file using Bulma CSS.

Fibonacci

cache = {0: 0, 1: 1} def fibonacci_of(n): if n in cache: # Base case return cache[n] # Compute and cache the Fibonacci number cache[n] = fibonacci_of(n - 1) + fibonacci_of(n - 2) # Recursive case return cache[n] [fibonacci_of(n) for n in range(15)]

List Flattening

print('List flattening','-'*20) list_of_lists = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]] print(f'Input: {list_of_lists}') flattened_list = [item for sublist in list_of_lists for item in sublist] print(f'Output: {flattened_list}')

List Duplicates

print('Get unique elements from list','-'*20) list_duplicates = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'a', 'a', 'c', 'd', 'e', 'l', 'g', 'z', 'i'] print(f'Duplicates: {list_duplicates}') unique_list = list(set(list_duplicates)) print(f'Uniques: {unique_list}') # If you need to sort the list print(f'Sorted Uniques: {sorted(unique_list)}')

Password Generator

import random import string total = string.ascii_letters + string.digits + string.punctuation length = 16 password = "".join(random.sample(total, length)) print(f'Password: {password}')

Change String Case

print('Change string case','-'*20) my_string = "sTrangECaSE" print(f'Standard: {my_string}') my_string_lower = my_string.lower() print(f'Lower: {my_string_lower}') my_string_upper = my_string.upper() print(f'Upper: {my_string_upper}') my_string_title = my_string.title() print(f'Title: {my_string_title}')

Merge Two List

print('Merge two list into a dictionary','-'*20) list_a = ['a', 'b', 'c'] print(f'List A: {list_a}') list_b = [1, 2, 3] print(f'List B: {list_b}') out_dict = dict(zip(list_a, list_b)) print(f'Merged Dictionary: {out_dict}')

Sort List of Dicts by Key

print('Sort a list of dictionaries by a key','-'*20) people = [ {'name': 'John', 'height': 90}, {'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}, {'name': 'Sam', 'height': 75}, ] print(f'People: {people}') print('-'*20) sorted_people = sorted(people, key=lambda k: k['height']) print(f'Sorted People: {sorted_people}')

Get Most Frequent Value From a List

print('Get most frequent value from a list','-'*20) list_a = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'b', 'a', 'b', 'c', 'b', 'b', 'c', 'a', 'b', 'c'] print(f'List A: {list_a}') print(f'Most frequent value: {max(set(list_a), key=list_a.count)}')

Palindrome

palindrome_string = "racecar" print(f'Word: {palindrome_string}') is_palindrome = palindrome_string == palindrome_string[::-1] print(f'Is a palindrome: {is_palindrome}')

Merge Two Dictionaries with **

dict1 = {1: "PY", 2: "JS"} print(f'Dict1: {dict1}') dict2 = {3: "CPP", 4: "C#"} print(f'Dict2: {dict2}') dict3 = {**dict1, **dict2} print(f'Mergef Dict: {dict3}')