Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / max_elements_and_values.py
Created November 27, 2019 15:24
Finding and Locating Maximum Elements Using numpy
import numpy as np
students = ["Alice", "Bob", "Carl", "David"]
subjects = ["Math", "Physics", "Biology"]
# Quiz scores
# - Columns -> subjects
# - Rows -> students
scores = np.array([[10, 8, 5], [6, 9, 8], [9, 9, 8], [7, 5, 9]])
print("\nSubject and name of the student, and the highest score")
@vlad-bezden
vlad-bezden / primes_generator.py
Created November 11, 2019 14:40
Prime numbers generator. Creates infinite list of prime numbers.
from math import sqrt
from itertools import count
NUMBER_OF_PRIMES = 10
def primes() -> int:
"""Primes generator."""
yield 2
for x in count(3, step=2):
@vlad-bezden
vlad-bezden / pairs.py
Created October 25, 2019 10:25
Splits iterable by pairs.
print(*zip(*[iter(range(20)] * 2))
# output
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11) (12, 13) (14, 15) (16, 17) (18, 19)
@vlad-bezden
vlad-bezden / progress_bar.py
Created September 18, 2019 14:13
Progress bar implementation in Python
from time import sleep
def progress(percent=0, width=50):
left = width * percent // 100
right = width - left
print('\r[', '#' * left, ' ' * right, ']',
f' {percent:.0f}%',
sep='', end='', flush=True)
for i in range(101):
@vlad-bezden
vlad-bezden / spinning_wheel.py
Last active September 18, 2019 13:56
spinning wheel to indicate a work in progress
from itertools import cycle
from time import sleep
for frame in cycle(r'-\|/'):
print('\r', frame, sep='', end='', flush=True)
sleep(0.2)
@vlad-bezden
vlad-bezden / reverse.go
Created August 25, 2019 11:21
Example of reverse string in Go using strings.Builder. This function about 3 times faster than using string concatenation
//Reverse reverses string using strings.Builder. It's about 3 times faster
//than the one with using a string
// BenchmarkReverse-8 3000000 499 ns/op 56 B/op 6 allocs/op
func Reverse(in string) string {
var sb strings.Builder
runes := []rune(in)
for i := len(runes) - 1; 0 <= i; i-- {
sb.WriteRune(runes[i])
}
return sb.String()
@vlad-bezden
vlad-bezden / pipeline.go
Created August 22, 2019 21:01
This example illustrates pipelines in Go. There are two channels. The first channel (channel A) is used for getting the random numbers from the first function and sending them to the second function. The second channel (channel B) is used by the second function for sending the acceptable random numbers to the third function. The third function i…
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
@vlad-bezden
vlad-bezden / preprocess_text.go
Created August 13, 2019 13:56
An example on how to cleanup text for text classification in ML
package main
import (
"fmt"
"regexp"
"strings"
)
func preProcess(text string) string {
// Find all chars that are not alphabet
@vlad-bezden
vlad-bezden / scientificNotation.go
Created August 11, 2019 10:55
How to use scientific notation in Go and convert it from float64 to int
/*
By default Go interpreter scientific notation to float64.
This example shows how to use scientific notation to declare int types.
This example also shows how to reuse the same parameter in Printf function.
*/
package main
import (
"fmt"
@vlad-bezden
vlad-bezden / twoSlicesDiffs.go
Created August 11, 2019 09:25
An example of how to find difference between two slices in Go. It uses map and empty struct (0 bytes) as a void to convert slice to map (kind of set).
/*
An example of how to find the difference between two slices.
This example uses empty struct (0 bytes) for map values.
*/
package main
import (
"fmt"
)