Skip to content

Instantly share code, notes, and snippets.

class Solution:
def isPalindrome(self, x: int) -> bool:
xStr = str(x)
for i in range(0, len(xStr)):
if xStr[i] != xStr[-(i+1)]:
return False
# Qua il ciclo è finito!
return True
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
@ecamellini
ecamellini / compunding_iterest.py
Created November 11, 2021 16:09
Compounding interest calculator.
"""
Compounding interest calculator.
"""
yearly_deposit = float(input("Yearly deposit: "))
yearly_interest = float(input("Average yearly interest: "))
yearly_multiplier = (100.0 + yearly_interest) / 100.0
years = int(input("Years: "))
total = 0
@ecamellini
ecamellini / contiguous_intervals.py
Last active November 11, 2021 14:09
Given a list of integers, find the contiguous ranges.
def contiguous_intervals(list_of_ints):
"""
Given a list of integers, find the contiguous ranges.
EXAMPLE:
list_of_ints = [1, 2, 3, 5, 8, 9, 10]
result = [(1,3), (5,5), (8,10)]
"""
length = len(list_of_ints)
if length == 0:
@ecamellini
ecamellini / simple-text-editor.py
Last active March 18, 2021 15:34
Solving the Simple Text Editor challenge as fast as possible. See challenge here: https://www.hackerrank.com/challenges/simple-text-editor/problem
#!/usr/bin/env python3
# Challenge: https://www.hackerrank.com/challenges/simple-text-editor/problem
# Trying to solve it as fast as possible!
import sys
from collections import namedtuple
import re
# Inizializing global variables (I don't like global vars, it's faster like this...)
@ecamellini
ecamellini / mount-ntfs
Created June 22, 2020 17:54
Script to mount an NTFS file on Mac OS, enabling read/write.
#! /bin/bash
########## PREREQUISITES ##########
# This script uses NTFS-3G to mount an NTFS file on Mac OS.
# See: https://github.com/osxfuse/osxfuse/wiki/NTFS-3G
# See also: https://www.makeuseof.com/tag/solving-the-read-only-external-hard-drive-problem-on-your-mac/
# TL;DR;
# brew install ntfs-3g
@ecamellini
ecamellini / cryptocurrencies.sh
Created December 19, 2017 20:32
bitbar compatible script to retrieve cryptocurrency prices
#! /bin/bash
JQ=/usr/local/bin/jq
COINBASE_BTC_EUR_BUY=`curl -sL https://api.coinbase.com/v2/prices/BTC-EUR/buy | $JQ -r '.data.amount'`
COINBASE_BTC_EUR_SELL=`curl -sL https://api.coinbase.com/v2/prices/BTC-EUR/sell | $JQ -r '.data.amount'`
COINBASE_ETH_EUR_BUY=`curl -sL https://api.coinbase.com/v2/prices/ETH-EUR/buy | $JQ -r '.data.amount'`
COINBASE_ETH_EUR_SELL=`curl -sL https://api.coinbase.com/v2/prices/ETH-EUR/sell | $JQ -r '.data.amount'`
COINBASE_LTC_EUR_BUY=`curl -sL https://api.coinbase.com/v2/prices/LTC-EUR/buy | $JQ -r '.data.amount'`
COINBASE_LTC_EUR_SELL=`curl -sL https://api.coinbase.com/v2/prices/LTC-EUR/sell | $JQ -r '.data.amount'`
@ecamellini
ecamellini / evaluation-strategies-scala-example.md
Last active May 15, 2017 16:24
Evaluation strategies: call-by-name and call-by-value

call-by-value

The interpreter evaluates all the arguments before evaluating the function itself (i.e., before rewriting the function application).

It has the advantage that every argument is evaluated only once.

call-by-name

The interpreter passes the arguments "as they are" to the function: this means that they are evaluated every time they are used inside it.

@ecamellini
ecamellini / python_currying.md
Last active March 30, 2018 08:01
Currying example in python

Currying

See the Definition.

Implement the function add so that it works in the following way:

add(1,2)  # 3
add(1)(2) # 3
@ecamellini
ecamellini / find_odd_id.md
Created March 30, 2017 10:27
Odd id problem.

Given a list of ids, where every id is present an even number of times but only on of them is present an odd number of times, find this last id in an efficient way and estimate the complexity.

First solution (probably inefficient):

def find_odd_id(L):
    return find_odd_id_helper(L, [])

def find_odd_id_helper(L, studied_ids):                                  
 c = 0