Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / piggy.go
Created August 11, 2019 00:51
Exercise #6 from "Get Programming with Go" book by by Nathan Youngman and Roger Peppé
/*
Write a program that randomly places nickels ($0.05), dimes ($0.10),
and quarters ($0.25) into an empty piggy bank until it contains at least $20.00.
Display the running balance of the piggy bank after each deposit,
formatting it with an appropriate width and precision.
*/
package main
import (
@vlad-bezden
vlad-bezden / floatingPointRoundingErrors.go
Created August 10, 2019 20:51
Rounding Errors example using addition in a loop versus multiplication
package main
import "fmt"
func main() {
piggyBank := 0.0
for i := 0; i < 11; i++ {
piggyBank += 0.1
}
fmt.Println("Using addition 11 times", piggyBank)
@vlad-bezden
vlad-bezden / minimizeRoundingErrors.go
Created August 10, 2019 20:35
To minimize rounding errors, perform multiplication before division. The result tends to be more accurate that way.
package main
import "fmt"
func main() {
fmt.Println("1. Division First")
celsius := 21.0
fmt.Print((celsius/5.0*9.0)+32, "° F\n")
fmt.Print((9.0/5.0*celsius)+32, "° F\n\n")
@vlad-bezden
vlad-bezden / generator.py
Created August 8, 2019 17:46
Generator that generates infinite stream of random numbers between 1 and 99 inclusive.
gen = (random.randrange(1, 100, 2) for _ in itertools.count())
# or
gen = (random.randrange(1, 100, 2) for _ in itertools.cycle("Happy Coding!"))
# usage:
for n in gen:
print(n)
@vlad-bezden
vlad-bezden / urllib_json_post.py
Created August 8, 2019 14:12
POST JSON data using Python urllib standard library
import urllib.request
import json
from pprint import pprint
url = "<some_url>"
values = {
"first_name": "Vlad",
"last_name": "Bezden",
"email": "[email protected]",
@vlad-bezden
vlad-bezden / fibonacci.go
Created August 4, 2019 12:29
Fibonacci number calculator in go
package main
import (
"flag"
"fmt"
"time"
)
var n int
@vlad-bezden
vlad-bezden / tasks.json
Created August 1, 2019 15:12
VS Code configuration for go build project (Ctrl+Shift+B)
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "go build <project name>",
"group": {
"kind": "build",
"isDefault": true
@vlad-bezden
vlad-bezden / asyncio_pub_sub.py
Created May 18, 2019 11:15
Typical pub/sub example using asyncio in Python 3.7+
import asyncio
import random
import logging
from collections import namedtuple
import uuid
import string
logging.basicConfig(level=logging.INFO)
Message = namedtuple("Message", ["msg_id", "inst_name"])
@vlad-bezden
vlad-bezden / logger.py
Created April 12, 2019 15:33
An example of how to configure logger to log output to console as well as to rotation file. There will be max 10 files created with each 1MB max
import logging as logger
from logging.handlers import RotatingFileHandler
logger.basicConfig(
level=logger.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
RotatingFileHandler(
"log_info.log", maxBytes=1_000_000, backupCount=10
@vlad-bezden
vlad-bezden / weekday_name.py
Last active April 12, 2019 10:34
Gets weekday name (Monday - Sunday), day of week as integer (Monday = 0 - Sunday = 6)
import pandas as pd
df = pd.Timestamp("2019-04-12")
print(f"{df}, {df.dayofweek}, {df.weekday_name}")
# 2019-04-12 00:00:00, 4, Friday