2024-05-27
Ip Info Collect

Tutorial:

https://stackoverflow.com/questions/24678308/how-to-find-location-with-ip-address-in-python

To obtain IP of ourselves, we can visit:

1
2
curl https://api.ipify.org

To get geo info of our IP, visit:

1
2
curl https://ipinfo.io | jq .country

TO get geo info of any IP, use:

https://pypi.org/project/IP2Location/

https://ip2location-python.readthedocs.io/en/latest/quickstart.html

Read More

2023-12-27
Defaultdict And Counter

from collections import defaultdict, Counter
mydict = defaultdict(Counter) # remember to put callable or none as argument of defaultdict
mydict = defaultdict(lambda: defaultdict(lambda: 0)) # alternative
mydict['k1']['k2'] += 1 # two is ok, but not one or three
Read More

2022-12-18
Elo Rating System

Elo rating system is a method for calculating the relative skill levels of players in two-player games such as chess. It is named after its creator Arpad Elo, a Hungarian-American physics professor.

The basic idea behind the Elo rating system is that each player is assigned a rating, and the difference between the ratings of two players determines the expected outcome of a match between them. If a higher-rated player wins, their rating will increase, while the rating of the lower-rated player will decrease. If the lower-rated player wins, the opposite will happen. The amount of change in the ratings depends on the difference between the ratings and the result of the match.

Here is an example of how the Elo rating system can be implemented in Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def elo_rating(rating1, rating2, k, result):
# Calculate the expected score for each player
expect1 = 1 / (1 + 10 ** ((rating2 - rating1) / 400))
expect2 = 1 / (1 + 10 ** ((rating1 - rating2) / 400))
# Calculate the new ratings for each player
if result == 1:
# Player 1 wins
rating1 = rating1 + k * (1 - expect1)
rating2 = rating2 + k * (0 - expect2)
elif result == 0:
# Player 2 wins
rating1 = rating1 + k * (0 - expect1)
rating2 = rating2 + k * (1 - expect2)
return rating1, rating2

This function takes four arguments:

rating1: The current rating of player 1.

rating2: The current rating of player 2.

k: The “k-factor”, which determines the amount of change in the ratings. A higher k-factor means more change.

result: The result of the match, where 1 indicates a win for player 1 and 0 indicates a win for player 2.

The function returns a tuple containing the updated ratings for both players.

Read More

2022-11-04
Mixing Different Version Of Python Libraries And Pass Environment Variables Beforehand

command of mpython3

1
2
env JAVA_HOME=/opt/homebrew/Cellar/openjdk/18.0.2 PYTHONPATH=$(python3 -c "import sys; print(':'.join(sys.path))"):/opt/homebrew/lib/python3.10/site-packages python3 $@

Read More

2022-05-31
Algorithms Compilers Sicp Clrs Ct4S

Read More