Msaada Programmers (Python)

Mwelewa

JF-Expert Member
Jan 5, 2011
2,343
3,284
I have started my carrier as programmer. Nina siku chache tu toka nimeanza sasa nimekutana na hii task nahitaji mtu anisaidie. How do I go about it?

Task description

A zero-indexed array A consisting of N integers is given.

An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 8 elements:

A[0] = -1 A[1] = 3 A[2] = -4 A[3] = 5 A[4] = 1 A[5] = -6 A[6] = 2 A[7] = 1

P = 1 is an equilibrium index of this array, because:

A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]

P = 3 is an equilibrium index of this array, because:

A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

P = 7 is also an equilibrium index, because:

A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0

and there are no elements with indices greater than 7.

P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function:

def solution(A)

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

For example, given array A shown above, the function may return 1, 3 or 7, as explained above.

Assume that:

N is an integer within the range [0..100,000];each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

Complexity:

expected worst-case time complexity is O(N);expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Cc WAHEED SUDAY
Cc Kang
Cc Stefano Mtangoo
 
Inanukia kama assignment hivi.
Onyesha ulichofanya na ulipokwama
Mkuu hii ni demo test nilikuwa natafuta maswali yakufanya kuhusu Python nikakutana na hili swali sasa nimekwama.

Mkuu najifunza mwenyewe Programming muda wangu wa ziada.
 
Wewe ni programmer?

Sijaanza hilo swali maana linanipa utata ndiyo maana nikaweka hapa nipate usaidizi
Duuuuhh.!!!!! mwana una moyo sana. mi swali complicated ningekuwa nushaliacha zamaniiii. tena kwa python ndo kabisa. maana mi natumia C++. kwa Python na Java sijui kwanini hatuelewani
 
Duuuuhh.!!!!! mwana una moyo sana. mi swali complicated ningekuwa nushaliacha zamaniiii. tena kwa python ndo kabisa. maana mi natumia C++. kwa Python na Java sijui kwanini hatuelewani
Kweli mkuu lipo complicated lakini hapa JF naamini wapo code geeks wanaweza ku-solve hii problem. Ngoja nione kama nitapata msaada.
 
Yaan umeshajua basics za python au ndio umeanza?
Mfano array, function declaration, numpy, and etc.
 
Yaan umeshajua basics za python au ndio umeanza?
Mfano array, function declaration, numpy, and etc.
Naendelea kujifunza mkuu. Any help regarding the question above?
 
Hehe, dah haka ka swali hadi imebidi nikashikie kalamu.
Sasa nimekapata ila nakupa hint tu ili uimplement mwenyewe.

List yako iko hivi
0 1 2 3 4 5 6 7
[ -1 , 3 , -4 , 5 , 1 , -6 , 2 , 1 ]

TotalSum ya elements zote ni = 1
Anza ku-loop tena, ukianzia index 0. Huku unaweka current_sum hii ni sum ya index ulipofikia, kwa kujumlisha elements zote zilizopita, Ukichukua TotalSum - current_sum - current_element ikawa sawa na current_sum basi hiyo ndo equllibrium

Mfano
Kwenye index 0.
current_sum = 0 kwa kua nyuma ya hiyo index hakuna kitu.
total_sum - current_sum - current_element = 1 - 0 - (-1) = 2 hapa 2 sio sawa na current_sum ambayo ni 0. So sio index

Kwenye index 1
current_sum = -1 kwa kua nyuma ya hiyo index kuna -1 peke yake, so total sum ya all elements before this ni -1
total_sum - current_sum - current_element = 1 - (-1) - 3 = -1. Hii -1 ni sawa na current_sum. Kwa hiyo hii ni equillibrium.

Kwenye index 2
current_sum = 2 kwa kua elements before index 2 yaani -1 + 3 = 2
total_cum - current_sum - current_element = 1 - 2 - (-4) = 3. Hii si sawa na current_sum kwa hiyo sio equillibrium.

Nadhani umeshaona inavyokwenda. Ni rahisi sana kuandika loop itakayokupa hili jibu, ila sikuandikii hapo umiza kichwa mwenyewe ujifunze kitu.
 
Endelea kujifunza, then hapo baadae naweza kukupa ABC za python. Ukiielewa ni nzuri sana wala haina complication kama C++.
 
Mkuu Graph

Asante sana sasa nimepaza mwanga ngoja nipambane kama nikikwama nitarudi tena. Asante sana
 
Nahisi kama unaruka stage, soma python(or whatever language you want to use) uielewe kwanza then you can dive into these types of questions. And the best way to get help here ni kuleta ulichofanya na wapi ulipokwama sio kuleta swali zima zima.

I suggest you take a look at HackerRank | Technical Recruiting | Hiring the Best Engineers wana challenges for all levels, beginner, medium, advanced etc and you can practise and learn more as you go.
 
python ina material mengi sana kwenye internet. Unaweza kupata tutorials pamoja na reading notes kwenye google. Pia unaweza kuingia Stack Exchange.
 
This is an algorithmic problem - complete with time and space complexity. Kama ndio unajifunza programming in python then angalia resources unazotumia. Hili swali hukutakiwa ukumbane nalo.
 
def Equilibrum(ArrayList):
N = len(ArrayList)
EquilibrumIntegers = [ ]

for P in range(0, N+1):
lowerBound = ArrayList[0:p]
upperBound = ArrayList[P+1:N+1]
if sum(lowerBound) == sum(upperBound):
EquilibrumIntegers.append(P)

if EquilibrumIntegers == [ ]:
return -1
else:
return EquilibrumIntegers

Kama ukiamua kui test kwenye interactive shell keep in mind the indentation maana ni muhimu kwenye python.Ukizingua indentation inaweza is execute hzo nested statements

Pia hyo range( ) function happy juu bro nimetumia python 2.7.Uki run kwenye python 3.0 Ina generate iterative construct na cyo list
So I test kwenye python version less than 3.0
Kama hauna hzo version less than 3.0 then I force I generate list kwa kuongezea Neno list mbele
Have a nice day.
 
Hyo sentensi hapo juu ni
lowerBound = ArrayList[0 double colon followed by P]
Naona ukiandika hvo apa if inaleta emoji
 
Back
Top Bottom