日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
FAANG面試在2021年問這5個(gè)Python問題

我的Github上提供了完整的Python代碼。

成都創(chuàng)新互聯(lián)是專業(yè)的淮南網(wǎng)站建設(shè)公司,淮南接單;提供做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行淮南網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

對于任何與數(shù)據(jù)相關(guān)的采訪,使用Python編程都是必不可少的技能,也是必須準(zhǔn)備的領(lǐng)域!編程問題有四種類型,包括數(shù)據(jù)結(jié)構(gòu)和算法,機(jī)器學(xué)習(xí)算法,數(shù)學(xué)和統(tǒng)計(jì)以及數(shù)據(jù)操作(請參閱Emma Ding @Airbnb撰寫的精彩文章)。

我在相關(guān)文章中闡述了有關(guān)數(shù)據(jù)處理和字符串提取的主題。在今天的帖子中,我將重點(diǎn)介紹數(shù)學(xué)和統(tǒng)計(jì),并對一些大型科技公司(尤其是FAANG)提出的五個(gè)Python編程問題進(jìn)行實(shí)時(shí)編碼。這種類型的問題為您提供了業(yè)務(wù)設(shè)置,并通過模擬請求統(tǒng)計(jì)解決方案。

問題1:誰先贏?微軟

艾米和布拉德輪流滾動(dòng)一個(gè)漂亮的六邊形模具。誰先擲出" 6"贏得比賽。艾米首先滾動(dòng)。艾米獲勝的機(jī)率是多少?

(1) 思路

這是一個(gè)核心的模擬問題,沒有比模擬大量過程并檢查Amy獲勝的可能性更好的方法了。

艾米滾第一。如果結(jié)果為6,則游戲結(jié)束,艾米獲勝。否則,布拉德(Brad)翻牌,如果是6,則贏得比賽。如果不是,則轉(zhuǎn)回艾米(Amy)。重復(fù)該過程,直到有人以6結(jié)束游戲。

在這里,關(guān)鍵是要了解邏輯流程:誰贏了,在什么情況下贏了。如果艾米得到6分,布拉德必須投擲嗎?沒有。

(2) 解題

 
 
 
 
  1. import numpy as np
  2. def who_won(die, size):
  3.     
  4.     A_count = 0 # initialize A_count
  5.     
  6.     B_count = 0 # initialize B_count
  7.     
  8.     for i in range(size): # create an iteration 
  9.         
  10.         A_6 = np.random.choice(die)  # roll the fair dice and choose a random value from 0 to 6
  11.         
  12.         if A_6 == 6:       # if A rolls a 6, then A_count adds 1. 
  13.             
  14.             A_count+=1     # a side-note for Python beginners: the full expression is "A_countA_count = A_count+1."
  15.         
  16.         else:              # if the above if condition does not fullfill
  17.             
  18.             B_6 = np.random.choice(die)  # then, it's B's turn to roll the dice, which is a random choice from 0 to 6.
  19.             
  20.             if B_6 == 6:   # if B rolls a B, B_count adds 1.
  21.                 
  22.                 B_count+=1
  23.                 
  24.     return A_count/(A_count+B_count)   # return the total number of cases that A won divided by the combined number of A and B wonaka. the result is the probability that Amy wins.  

檢查第11行:A_6是Amy的數(shù)據(jù)分布,如果她的數(shù)據(jù)為6,則計(jì)數(shù)為+1。否則,布拉德可以擲骰子。

在最后(第25行),最終結(jié)果應(yīng)該是Amy獲勝的次數(shù)除以Amy和Brad獲勝的總數(shù)。一個(gè)常見的錯(cuò)誤是將A_count除以仿真總數(shù)。這是不正確的,因?yàn)楫?dāng)Amy和Brad都未能擲出6時(shí),會(huì)有迭代。

讓我們測試一下上述算法。

 
 
 
 
  1. np.random.seed(123)
  2. die = [1,2,3,4,5,6]
  3. size = 10000
  4. who_won(die,size)
  5. view raw

0.531271015467384

事實(shí)證明,艾米(Amy)在布拉德(Brad)之前就開始滾動(dòng)贏得這場比賽。艾米(Amy)在10,000次模擬中獲勝的概率為53%。

> Photo by john vicente on Unsplash

問題2:每個(gè)主要公司的最大數(shù)量為69

-給定一個(gè)僅由數(shù)字6和9組成的正整數(shù)num。-返回最多可更改一個(gè)數(shù)字可得到的最大數(shù)字(6變?yōu)?,而9變?yōu)?)。https://leetcode.com/problems/maximum-69-number/

(1) 思路

給定一個(gè)正整數(shù),只有一種方法可以增大值,即將" 6"變成" 9",而不是相反。另外,我們必須更改最左邊的6;否則,它不是最大數(shù)量。例如,我們必須將" 6996"更改為" 9996",而不是" 6999"。

我想出了這個(gè)問題的幾種變體:您可以更改一次,也可以全部更改為6。

(2) 解決方法1:替換一次

 
 
 
 
  1. # replace once
  2. def max_69_once(num):
  3.     return int(str(num).replace('6','9',1))
  4. # test case
  5. num = 966666669
  6. max_69_once(num)

996666669

在第3行中,我們將整數(shù)轉(zhuǎn)換為字符串,然后將第一個(gè)" 6"替換為" 9";使用int()將其返回為整數(shù)。

(3) 解決方案2:全部替換

 
 
 
 
  1. def max_69_all(num):
  2.     k = len(str(num))
  3.     return int(str(num).replace('6','9',k))
  4. # test case
  5. num = 966666669
  6. max_69_all(num)

999999999

對于第二種情況,我們不必指定k,因?yàn)閞eplace()方法默認(rèn)會(huì)更改所有合適的值。我出于教學(xué)目的指定了k值。這個(gè)問題的另一個(gè)變體是替換前兩個(gè)或三個(gè)" 6"以使數(shù)字最大。

> Photo by Alessandro Capuzzi on Unsplash

問題3:有效的完美正方形。Facebook

-給定正整數(shù)num,編寫一個(gè)函數(shù),如果num是一個(gè)完美的平方則返回True;否則返回False。-后續(xù)操作:請勿使用任何內(nèi)置庫函數(shù)(例如sqrt)。

https://leetcode.com/problems/valid-perfect-square/

(1) 思路

這非常簡單:檢查正整數(shù)是否具有理想的平方根,如果有正整數(shù),則返回True,這可以分兩步完成。

  • 找到平方根。
  • 檢查它是否是完美的平方根。

棘手的部分是我們必須使用內(nèi)置庫(例如,數(shù)學(xué),Numpy)來計(jì)算平方根,這在LeetCode上是一個(gè)簡單的問題。如果我們不能使用這些庫,那么它將成為更具挑戰(zhàn)性和反復(fù)性的問題,這是LeetCode的一個(gè)中等水平的問題。

(2) 解決方案1:內(nèi)置庫

 
 
 
 
  1. import math 
  2. def valid_perfect_square(num):
  3.     return int(math.sqrt(num))**2==num  # the int() method only returns the integer part and leaves out the decimal part.
  4.                                         # For perfect squares, there should be no decimal part. The equation should thus hold.
  5. # test case 
  6. valid_perfect_square(16)

該算法輕松通過了測試案例。應(yīng)當(dāng)指出,我們需要使用int()方法僅獲取平方根的整數(shù)部分,而忽略任何小數(shù)部分。對于完美的正方形,它不會(huì)有任何差異,因此等式成立。對于非完美的平方,方程將不成立并返回False。

(特別感謝韓琦發(fā)現(xiàn)錯(cuò)誤!)

解決方案2:沒有內(nèi)置庫和二進(jìn)制搜索

 
 
 
 
  1. # 1 find the squre root of num
  2. # 2 check if it is a perfect square number
  3. # solution: no built-in library & binary search
  4. def valid_perfect_square(num):
  5.     if num < 2: 
  6.         return True
  7.     left, right = 2, num//2       # create two pointers: left and right 
  8.     while left<=right:            # while loop to constantly update left and right 
  9.         x = left + (right-left)//2# take a wild guess and treat x as the starting point
  10.         xx_squared = x*x           # calculate the squared value of x
  11.         if x_squared == num:      # use the following 'if-else' statement to constantly update x_squared.
  12.             return True           # if there are the same, return True
  13.         if x_squared 
  14.             left= x+1 
  15.         else:                     # if x_squared is bigger, right decreases by 1
  16.             right= x-1
  17.     return False                  # the while loop should continue looping until left and right converge and no common value obtained
  18.  
  19.  
  20. # test case
  21. valid_perfect_square(16)

如果不允許使用任何庫,則采用二進(jìn)制搜索。LeetCode包含一個(gè)詳細(xì)的解釋(在此處),我也對此主題發(fā)表了另一篇文章(在這里)。簡而言之,我們創(chuàng)建左右兩個(gè)指針,并將這兩個(gè)數(shù)字的平均值與原始數(shù)字進(jìn)行比較:如果小于該數(shù)字,則增加該值;如果更大,我們減少它;或者,如果它們匹配,則返回True。

這些條件將在while循環(huán)中自動(dòng)檢查。

#問題4:階乘尾隨零。彭博社

給定整數(shù)n,返回n中的尾隨零!

后續(xù)行動(dòng):您能否編寫一種適用于對數(shù)時(shí)間復(fù)雜度的解決方案?

https://leetcode.com/problems/factorial-trailing-zeroes/

(1) 思路

這個(gè)問題有兩個(gè)步驟:

  • 計(jì)算n階乘n!
  • 計(jì)算尾隨零的數(shù)量

對于第一步,我們使用while循環(huán)迭代遍歷n個(gè)階乘并停止循環(huán)直到1。對于第二步,事情變得有些棘手。

該問題要求尾隨零而不是總數(shù)零。這是個(gè)很大的差異。8!是40,320,它有2個(gè)零,但只有1個(gè)尾隨零。我們在計(jì)算時(shí)必須格外小心。我提出了兩種解決方案。

(2) 解決方案1:向后讀取字符串

 
 
 
 
  1. # 1 calculate n!
  2. # 2 calculate the number of trailing zeros
  3. def factorial_zeros(n):
  4.     product = n
  5.     while n > 1 :         # iteratively calculate the product
  6.         product *= (n-1)
  7.         n-=1
  8.         
  9.     count = 0 
  10.     
  11.     for i in str(product)[::-1]:  # calculate the number of trailing zeros
  12.         if i == '0':
  13.             count+=1
  14.         else:
  15.             break
  16.     return count
  17. factorial_zeros(20)

計(jì)算產(chǎn)品的第一部分是不言而喻的。對于第二部分,我們使用str()方法將乘積轉(zhuǎn)換為字符串,然后向后讀取:如果數(shù)字為0,則將count加1;否則為0。否則,我們將打破循環(huán)。

break命令在這里至關(guān)重要。如前所述,上述函數(shù)無需中斷命令即可計(jì)算零的總數(shù)。

(3) 解決方案2:while循環(huán)

 
 
 
 
  1. def factorial_zeros(n):
  2.     product = n
  3.     while n > 1 :          # step 1: iteratively calculate the product 
  4.         product *= (n-1)
  5.         n-=1
  6.     count = 0 
  7.     
  8.     while product%10 == 0:   # step 2: calculate the number of trailing zeros
  9.         productproduct = product/10
  10.         count+=1
  11.         
  12.     return count

第一部分與解決方案1相同,唯一的區(qū)別是我們使用while循環(huán)來計(jì)算尾隨數(shù)字:對于乘積除以10的乘積,最后一位必須為0。因此,我們使用while循環(huán)不斷更新while循環(huán),直到條件不成立為止。

順便說一句,解決方案2是我最喜歡的計(jì)算零的方法。

> Photo by Jamie Fenn on Unsplash

問題5:完美數(shù)字,亞馬遜

  • 理想數(shù)字是一個(gè)正整數(shù),它等于其正因數(shù)之和,但不包括數(shù)字本身。
  • 整數(shù)x的除數(shù)是可以將x均勻除的整數(shù)。
  • 給定整數(shù)n,如果n是完美數(shù)則返回true,否則返回false。
  • https://leetcode.com/problems/perfect-number/

(1) 思路

這個(gè)問題可以分為三個(gè)步驟:

  • 找出正因數(shù)
  • 計(jì)算總和
  • 決定:完美與否

第2步和第3步是不言而喻的,最多不超過一層。但是,棘手的部分是找到正除數(shù)。為此,我們可以采用野蠻力方法,并在從1到整數(shù)的整個(gè)序列中進(jìn)行迭代。

理論上,它應(yīng)該適用于一個(gè)小整數(shù),但是如果我們運(yùn)行大數(shù),它將超過時(shí)間限制。時(shí)間效率不高。

(2) 解決方案1:暴力

 
 
 
 
  1. #1. find the positive divisors
  2. #2. calculate the sum 
  3. #3. perfect or not 
  4. def perfect_number(num):
  5.     divisors = []
  6.     for i in range(1,num):
  7.         if num%i==0:
  8.             divisors.append(i)
  9.     if sum(divisors)==num:
  10.         return True
  11.     else:
  12.         return False
  13.         
  14. # test case 1
  15. perfect_number(2)

此方法不適用于較大的值。您的面試官可能會(huì)要求更有效的解決方案。

(3) 解決方案2:sqrt(n)

 
 
 
 
  1. # solution 2: sqrt(n)
  2. def perfect_number(num):
  3.     
  4.     if num<=1:
  5.         return False
  6.     
  7.     divisors = set([1])
  8.     
  9.     for i in range(2,int(num**0.5)+1):   # from 2 to num**0.5
  10.         if num%i==0:
  11.             divisors.add(i)
  12.             divisors.add(num//i)
  13.     return sum(divisors)==num

要找到其除數(shù),我們不必檢查最大為整數(shù)的值。例如,要找到100的除數(shù),我們不必檢查從1到100的數(shù)字。相反,我們只需要檢查直到100的平方根即10,并且所有其他可用值都已經(jīng)為包括在內(nèi)。

這是一種最佳算法,可為我們節(jié)省大量時(shí)間。

我的Github上提供了完整的Python代碼。https://github.com/LeihuaYe/Python_LeetCode_Coding

總結(jié)

  • 在進(jìn)行了數(shù)十次"實(shí)踐"編碼之后,最大的收獲就是理解問題并將問題分為多個(gè)可操作的組件。
  • 在這些可行的部分中,總會(huì)有一個(gè)步驟使求職者絆倒。諸如"不能使用內(nèi)置函數(shù)/庫"之類的限制。
  • 關(guān)鍵是逐步編寫自定義函數(shù),并嘗試避免在練習(xí)例程中盡可能多地使用內(nèi)置函數(shù)。

文章名稱:FAANG面試在2021年問這5個(gè)Python問題
當(dāng)前地址:http://www.5511xx.com/article/dhocpeh.html