Skip to content

Instantly share code, notes, and snippets.

View antonio-catalano's full-sized avatar

Antonio Catalano antonio-catalano

View GitHub Profile
@antonio-catalano
antonio-catalano / list_of_lists.py
Last active April 8, 2018 00:25
Another CutTheKnotMath problem
"""We want to calculate the probabilty that in a set of 3 sets, each composed of 3 different integers in the range [1,9]
we have at least one set where all the integers are odd"""
import random
print("""We want to calculate the probabilty that in a set of 3 sets, each composed of 3 different integers in the range [1,9]
we have at least one set where all the integers are odd\n\n""")
print("For example in the list [[1,4,5],[7,3,9],[2,6,8]] we have 1 sublist where all numbers are odd\n\n")
print("Let's start...")
print("Loading...")
""" Given the first 10 numbers, we want to know how many snake permutations exist, where a snake permutations is:
x1 < x2 > x3 < x4 > x5 < x6 > x7 < x8 > x9 < x10
The result is very counterintuitive....more than 50000 snake permutation exist.
It takes a lot of time to have the output, so take only the idea of the code..."""
import random
union = []
for i in range (25000000):
A = [s for s in range (1,11)]
new = []
@antonio-catalano
antonio-catalano / die.py
Created April 8, 2018 00:38
On average how many times do you need to roll a die before all six different numbers show up?
# On average how many times do you need to roll a die before all six different numbers show up?
import random
dado = [i for i in range (1,7)]
throw_list = []
N = 100000 # montecarlo method
for i in range(N):
lista = []
''' A tennis club invites 32 players of equal ability to compete in an elimination tournament.
(Only the winner of the pair will stay for the next round)
What is the probability that a particular pair of players will meet during the tournament?'''
import random
count = 0
def recursive_round(a): # we create a recursive function to simulate the tournament where the argument is a list
if len(a) == 1:
return a[0]
@antonio-catalano
antonio-catalano / collatz_conjecture.py
Created April 8, 2018 00:40
It will print the collatz sequence given an initial positive integer number
def collatz(number):
if number % 2 == 0:
print(number // 2)
return (number // 2)
else:
print(3 * number + 1)
return (3 * number + 1)
''' In what follows all number are uniformly distributed on [0,1]. Start with
a number So and the keep on generating the sequence S1,S2....until, for some
N, Sn > So for the first time. What is the expected value E(N) of that N ? '''
import statistics
import random
# we proceed with Montecarlo method
Series_of_n = [] #we creat a list of 10 million elements (n = 10.000.000)
@antonio-catalano
antonio-catalano / Inequality.py
Last active April 8, 2018 00:45
Show that for three real number that satisfy 0 < x < y < z < π/2 , the following inequality holds: (x+y) sin(z) + (x-z) sin(y) < (y+z) sin(x)
import math
import random
count = 0
n = 1000000 #montecarlo simulation
for i in range (1000000):
z = float(random.uniform(0,(math.pi)/2)) #uniform distribution, it gives a random real number between (0, π/2)
y = float(random.uniform(0,z))
x = float(random.uniform(0,y))
''' There are ten coins, each blank on one side and numbered on the other side with numbers 1 through 10.
All ten coins are tossed and the sum of numbers landing face up is calculated. What is the probability that this sum is at least 45?
'''
import random
def toss_coin (x):
coin = random.choice ([0,x]) #a discrete uniform distribution between values of a sequence, in this case 2 values possible.
return (coin)
@antonio-catalano
antonio-catalano / BomberManGame.py
Created April 14, 2018 10:27
The BomberMan Game
''' Here the description of the problem: https://www.hackerrank.com/challenges/bomber-man/problem '''
from random import choice
from time import sleep
R, C, N = map(int, input().split()) # we take the number of rows, columns and seconds of the loop
Lista = [] # we create the grid where each element is randomnly chosen between '.'(free cell) and 'M'(marked bomb)
for r in range(R):
@antonio-catalano
antonio-catalano / AthleteSort.py
Created April 14, 2018 13:22
from hackerrank site
'''' Here's the problem: https://www.hackerrank.com/challenges/python-sort-sort/problem'''
N,M = map(int,input('First number is the number of athletes,\n\
second number is the number of attributes for each atlhete.\nWrite: ').split())
print()
print("So we have {} athletes and {} in our database\n".format(N,M))
L = []
for i in range(1,N+1):
L.append(list(map(int,input('Write attributes for athlete number {} : '.format(i)).split())))