https://github.com/angel-star/ARTS/tree/master/2018_08_05
Algorithm
題目描述:
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ x, y < 2**31.
Example:
Input: x = 1, y = 4
Output: 2
Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.
由描述可知,漢明距離(Hamming Distance)就是比較兩個整數的二進制表示中,不同的位的總數。下面是AC的代碼:
下面給出最普遍的做法的C++實現
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<vector>
using namespace std;
int main(){
int x,y;
cout<<"Please input two integers:"<<endl;
cin>>x>>y;
/*算法1
int a=x^y,answer=0;
while(a!=0){
answer++;
a&=a-1;
}
*/
//算法二
int answer=0;
while(x!=y){
answer+=(x&1)^(y&1);
x>>=1;
y>>=1;
}
cout<<"Those two integers'' Hamming Distance is:"<<answer<<endl;
return 0;
}
算法一是將x和y異或操作之後計算其中二進制中1的個數,其計算的方法是通過自身和自身減一的二進制數逐位相與,然後減一之後繼續循環直至爲0,這樣也就將其二進制中1的個數計算出來;
算法二是將x和y的最後一位異或操作然後將其都右移一位以後再次計算其異或操作的和,直至兩個數相等。
從算法複雜度來看,兩種算法的複雜度均爲O(n)。
整體來看,雖然實現方法不同,但兩個算法都是通過異或操作計算其漢明距離,這也主要是由於漢明距離的定義所致。
Review
Python for data science : Part 1
本文簡單介紹了python這門語言在數據科學中的使用,難度偏向入門
有如下內容
Python function
Data types and sequences
Date and time
Lambda
Map
Filter
Reduce
Zip
For loop
可以說還是比較基礎的,下週會分享出來他關於本系列的最新的幾個文章。
Technique
在用python進行數據分析的過程中我們大多會選擇jupyter(ipython)這個工具來進行,它比較方便。
但是在某些時候它顯得不是那麼好用,比如在用到matplotlib時,它就顯得比較”僵”
這裏就要引出我們今天的主角了
利用 Jupyter 交互式小部件框架,jupyter-matplotlib 可以在 Jupyter notebook 和 Jupyterlab 中實現 matplotlib 的交互功能。
此外,canvas 是一個合適的 Jupyter 交互式小部件,可以定位在交互部件的 Layout 上。
Github 鏈接:
https://github.com/matplotlib/jupyter-matplotlib
用法:
要啓用 jupyter-matplotlib 後端,只需使用 matplotlib:
%matplotlib widget
示例:
下面是github地址,來個star支持下作者吧
Share
今天的分享來自於雲棲社區
在介紹了基本概念和運行機制後還給出了兩個示例代碼
示例代碼1 自動節點分配策略—-簡單的貪婪策略代價模型估計
示例代碼2 用戶限制的節點分配策略