2013-07-09 73 views
0

我在試圖弄清楚我正在處理的一個難題時遇到了一些困難。 這個想法是在減去百分比時找到剩餘的總數。使用百分比查找偏蟒蛇

這應該是一個相當簡單的練習,但由於某種原因,我很難找到適當工作的結果。

請原諒我的格式,這是我的第一篇文章。

from math import floor 
def compute_active_users(n, b): 
    x = float(b)/float(n) * 100 
    x = x * 100 
    return floor((n - x)) 

print '-' * 25 
print compute_active_users(1000,25) # Expected output: ------- 750 
print '-' * 25 
print compute_active_users(835,17) # Expected output: ------- 693 
print '-' * 25 


Results: 
------------------------- 
750.0 
------------------------- 
631.0 
------------------------- 

回答

0

下面將做到這一點:

def compute_active_users(n, b): 
    return n * (1 - b/100.) 

這裏:

  • b/100.百分比轉換爲分數
  • 1 - ...計算剩餘的分數。
  • n * ...計算其餘用戶數

請注意,這將很高興地返回一小部分用戶,因此您可能需要添加舍入作爲最後一步。