0% found this document useful (0 votes)
83 views30 pages

Python Codes

Uploaded by

roja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
83 views30 pages

Python Codes

Uploaded by

roja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Most Asked Coding Questions In Placements Top Coding Questions asked in Placement + Introduction to Proerarei ead Moro: Top 100 Codes [About Coding Questions Ctng Questions se very important, in bth online assessments and technical interviews. Depending onthe profilinterviowers ask candidates conceptual uestions abut 3 program or can as the candidate wrt the Introduction to Programming, "here ae primarily four programming languages on which interviewers con ak questions. You es prepare for coding languages hee: + bone Loomaste beam ston Other then casing languages, he placement process elsoreques one to learn about DEMS and DSA Most Asked Coding Questions Top 50 Coding Interview Questions Python £8. Wrte code of Grestest Common Dvisor c co AVA Python 'Ge0 of two Nunber? () Use Coupon Case “CTIO" and get fiat 10% OFF on your Prepneta Prime subeertion, c cr JAVA Python = at(Srput (ater any number *)) me far 5 im ranges 9) Mon 4 == 9) Ae (sump =n) ‘5. Wate code to Check two stings ae Anagram or nat c on wa Python ake user input Sleigh = input (“Eater the 2st sel :*) Sieg = Snput (“Eater the 2nd stg”) check 1F Length matches He Lon(Stringh) I= Len(String2) 96 False te tsorted function sort string by characters Singh = sorted( Sting) Sigd = sorted( Sting) check ¥f nom strings matches vif True 6. White code Check he given strings Palindrome or not c cr JAVA Python seake user tnput Siringh = SnputtEnter the String :°) Winiesatize sering and save reverse of Ist string String) = Seringh [22-1 chock 4 both marches A serngt ei ‘string 48 patindronic!) oe print('Strings $= not palindromic") “rite code to Calculate requoney of characters ina string c cr saa Python teake user input Siriog = input ("Enter the string :") take character snput Character = input ("Eneor character ernttiaatize Int vartable t0 store Frequency use count funceson to count frequency of character tcount function ts case sencetive 20 $¢ print Frequency of Character sceording to given Character er(frequeney) = ' 48 the frequency of given character") ‘8. Write code to check we strings match where one string contains wdeard charactors c co Python et sotve(s,b) nymeLen(a), eat) Wooo Sand fo) == 19" and = return Fale W (o> 2 and a{6) == 12°) oF Gn return solve(ai2:),0(4:1)5 © and n 20 and 8 9 00 and fo) o= 1" retura solve(9(1:1,8) aF soive(e,b(1:2) 8.Wte a code forbubble sort c / Function 0 print array */ seid: sepLay (it srr], Hat 5120) ( fas fr (i= 05 4 « atzey printf (rsd ", sertil)s print omnn) 1 Naim fonction t0 run the program Ine asin) fm arrayi = ( h Int 5320 © shaot(array) / saerrayle)) peintt("Before bubble sorts \n") Gtaplay array, $120); for = 9; 4 € size a; 4) C U1 Since, after each iteration righwost 4 elements are sorted for () "053 size = 1-1; 30) WH (arrayid] > arrayls + 4) ( emp = arrayLi)s // swap the element arrays] = srrayt} + 103 array(j + 1) = temp; } } print#tatter bubble sors: \a")s splay array, 120); oH 10 How s the mange sort algorithm implemented? eld ergeSor (intl) iat, Hat eld erse(Int nt, iat, iat veld sisplay(it ser [], Int siz0) ¢ ims for = 95 4 € sizes 400) C prinef(ved ", aretsI)s , peineromne) vid win) fm 3015) = fat 520 = sofa) / sleeo(0]) splay(a, size) gesors(a, 0, siz0 ~ eisplaya, size); told mergeSore(tat 2f7, tat Lert, tat righe) (can alsa use mid = left + (right = left) / 2 1 wie can avoid dats type overflow nie = (lefe = righ // recursive calts £9 sort iret half and second half subsrrays rergesore(a, left, mid); rergesort(s, mid «2, rg erge(a, left, mid, right) told mergo(int arr imi is fat oi = nid = Left fn n2 = right ~ mids fae 10, It ic, ne seh) U/ create temp arrays to stare Left and right subarcays fe Lion}, 8192)5 17 Copying data to tenp arrays Lt} and RE for =o; ems tH) 1 = arrleft + 415, for = 854 <5 400) RS] = arrintd v1 + § 17 here we merge the temp arrays back inte arr{t..r] <0; // Starting index of LI5) 8; /7 Staring inden of RES) k= lofts // Starting index of merged subsrray wie G1 1 8 5 < 02) 1 place the saatler item at arrtk] pos , 1 Copy the remaining elewents of Li, 3 any ile C1 t) ert) = Ui) } 1/ Copy the ronoining elements oF Ri, 7 any smite (<2) arth © RUD5 , cr Java 1, Wit to code to chock whothor avon your leap yer orn c inctuce estdio.h> fmt main) printf (wkd 4s 9 Leap Year", year); die i (year % 4 o= @ be year % 160 I 8) prinef(vkd 4s a Leap Year", year); Printf(vkd i not a Leap Year, year): ce 12. Find nontepesting characters in string c co awa Python Steig = "prepinstat Hinieialize 9 count variable chock for repeated characters 21f character is found more than J tine rorerak the 1009 sprint for nonrepeating characters code toroplace substring ina string. on aA Python staing-input (MEnter String s\n") Strdeinput ("Eater substring which has to be replaced :\n") Seraeinpue(Enter substring with which strl has te be repleced “\n" stringsring. rep aco( seri, str2) ‘String after replecenen:™ string) Use Coupon Cade “OTIO" and get flat 10% OFF on your Praplhsta Prime subscription. 2 Got flat 10% OFF 14, Writ code for Hoa sort c nase. Halu Trary Fler fete eonsi (ine oor), An see, nt 27/decLaring factions pF (lof ¢ slne aefteet] sae(eae) pF (oiane cine a8 ae(are] > aroae)) (nae 1 gaforeing evvieg Loge by using tenporaryvorsnte eros sts) e(s]> avin nat) tom: psiy(rn, 596, ma) fete suseors(at sr), Ant 58) proven cent t0feap sort for (= see 2-3 soe a: sesiy(ae, 336, §) fee itetecss ce; ero ent retele artis ris] = tore aostyarrs by 8 fete oats) 17 aepttag natats nt arr] = (3h 186, 3, 67, 2, AD, 15, 28,98, 99, nt sce» sheaf sae ys ers see): interning sree elennts\n"); 1 printing the sorte array rite Rate 20); cr ava 15. Write code toroplace ach elomont nan arayby its rank inthe array c cr JAA Python det changeder(inpues): newteray = Snputh.copy() newieray.sort() for 5 tw rangeClen(inputt)): far 5 tw range(Len(news~ray)) AW Anputa(t}ennenarray(}: nputn[s = Js # orsver code arr = [208, 2, 79, 22, 98) cnangesre (are) 1 Print the array elanents ein (are) cr JAVA Python det cotatenrraylarr, my a) temp = 0 mite current and 4sPaLinsrone(arrL4])) arr = [2, 232, 5545455, 989098, 161] = ten(arr) # print required answer prlat(LargestPatindrane(arr, n)) 10, Write code to find the factorial of «number c ct JNA Python for 5 ia range(2,nuee1) prlat(ourput) 20, Write the code to for Armstrong number c cH JNA Python eneth =) for 5 tw rango( lenge) siett = Iat(oureie) un = num/8 peat *arastrong") Print ("Not Armstrong") length) 21. Write program tind the sum of Natural Numbers using Recursion. c on wa Python et g2tSun(run) eotsun(nun)) 22. Write a program todd Two Matrices using Mull. cimensionsl fray c mt 330] (50], MEO I68}, suNIBB}OH}, 8, ritsnter he nen of ows (tae 2 08). > ant", 5 rints(unter the mater of coluens (beater 1 aed 180)“ atte 1 ritdntnee eenants of a8 tebe a) Gegten a) Glades we rite Center eters aH: 68, 5D cet, 15ND) rite(nter elevate oF tnd mata): eeten a Gras ee wih itt (enter eleven Es 8 62, 9 = 2 ett, LS) 1 obing a0 watts egies) Gre dee ane ar3ICH) = aC) + SEES: 1 orntig the rest ts osun of to aatrlees: eo") Gestion as Jee wih ¢ satslians ae 23, writ Program to Find the Sum of Natural Number using Recursion. c cr JAA Python fet cocursun(ounber) M number == 8 return sunver return number + recursun(ounder=1) runber, sum = 6,8 prlat(recursun(ounber)) Get 150+ hours of self- paced learning with Syllabus Cet et CoCr cy Se ae lees eR ec AVA Python c cr awa Python sr TRIGA tar bar) iti “bnary Wane ts {) a becinat Mane ts {J°frnat(binary sa decimal ¥at)) 26. write program to check whether a character fsa vowel or consonant c trom) cnch AF the Char Belang 9 set of nes Wf tee Print "carecer i Yow") ef folse 27. inte a code to find an Automorphie number © rt cochastonoghttnt us) ‘ enter vitae print (astonorrhe"): cH saya, 128, Write a code to find Find the ASCII value of character antistiva) £28. write code to Remove all charactors from string except alphabets © cr JAVA, Python ehch for eotebets Se fora} Sus and ors) 90) ar (ord(s) >= 97 and ors) co 22 Use Coupon Gade “CT10" and get fiat 10% OFF on your Propnsta Prine subscription. 2 Get flat 1096 OFF 30, Write a code to Prin the smallest eloment ofthe array © cH JAVA, Python 38, 89, 9, 56, 4, 88, 8) snd = arr{@) for 4 3n rangeClen(arr) 26 are(h] < mint rink» art] ne (aint) 1. Write code to Reverse the elem ofthe ray c cH saa Python Jo reversetist(a, start, end) [start], Alerd] = ACen), atstart] A= (39, 29, 38, 42, 59) reverseList(a, & 4) print (a) 22, Wite a code to Sort the lament of the aay © cr JAVA, Python nunsers = [18, 30, 40, 28] rumbers.s0rt() print(numers) 38, Wite a code to Sort the element of the array without sort mothod © cH JAVA, Python numbers = (18, 38, 40, 28] ombers.sore() print (numsers) ‘34. write code to Roplace a Substring ina string © stringeanpur(*Enter String :\n") Strteinput ("Eater substring which nas to be replaced V0") tracinput("Enter rubetring with wnien seri hae t0 Be replaced s\n") stringerering-rephace(str,st°2) peiat(*string efter reslacerere”) print (string) 35. Wite «code to Remove space rom astrng c cr ava, Python tse Join function String = **-Sotm(Steing.spL4t()) priat(rafeer reroving spaces string 45 :",String) 96, Wite cade to Count inversion © cH save, Python det inversion(arn) for 1 sn eange(ten(arey) fon 3 in ranges + 1, Len(ar): 4 are] « aerft] seray = (5, 3, 5) 2, 71 sriat(There are", inversion(areay), "possible Snversion") _37.Wirte a code tind consecutive largst subsequence © cH JAVA, Python ef Longestcansagiubeaa(arr, 2) welt for 1 an eungetny vite ere(t) +n an a return sax(val) seray = 09, 84, 5) 4) 3D print ("Longest Consecutive Subrequence :", LorgestConsegsunseg(areay, Ten(oreay))) 38: Write a Program to Find out the Sum of Digits of a Number. Programin © Programin C++ Program in JAVA Program in Python foe 5 In oon im = son + att) priatsu Fir More Solutions at Python Program o Fins the Sum of Digits 2 Number 39: Write a Program to Find out the Power of a Number Program in © Program in C++ Program in JAVA Program in Python Fn More Solutions at Python Program o Fins ot the Power of 2 Number 40: Write a Program to Find out the Sum of Digits of a Number. Programin ¢ Programin G++ Program in JAVA Programin Python rum = Snpus ("Enter Naners Fir More Solutions at Python Program o Find the Sum of Digs of = Number 41: Write a Program to Add two Fractions Program in © Programin C++ Program in JAVA Program in Python (et FineecD(nd, 92) far 4 im range(, ie(nin(at, m2)) 42) Woot 52 ond nz 8a =e 8 WF iopue fest fraction runt, deni = map(lt, Lise ing sptie(t *)) ter munerator and denontnator of first number funds den? = waptit, Ui umber: ").sp1s%0" "))) sput(tEnter nunerator and denominator of second ten mi * gen2) // Findoco(dent, den2) sim = (oust * Lex // dent) + (numa + tem // dena) umd = sun // FindécoCsum, tem) lem = ten // Findécotsum, tem) eimtcrum, "7%, denn, * + %, munz, M/*, dena, * = #, mums, H/*, Lem) Fir More Solutions at Python Program oa we rations 4; Write a Program to Find the Largest Element in an Array. Program in © 1 © rogram to Find Largest element in an array Int getLargest(lat arr{1, tat ten) 1 assign First array element 25 Largest It max = orr(0} 1 Vincarty search for the largest elonent force S13 4 mx) ret] = (20, 5, 35, 49, 19, 59, 1595 1 get the length of the array In en» seat a0 1) seo srr") 5 printf ("The Largest element ts: Ad", getLargest(err, len)? 1 Tine complexity: (0) 1 Space conptexity: O(N) Fi More Solution at ¢ Progrsm 1 Find the Larges lent ina Aray Programin G++ Program in JAVA Programin Python 43: Write a Program to Find the Roots of a Quadratic Equation Program in ¢ Programin Cor Program in JAVA Program in Python YW write a program to Find roots of a quadracie equation tn Python # oriver Program # Function att Fir More Solutions at Python Program e Fins the Roots 8 Quadra 44: Write a Program to Find the Prime Factors of a Number. Program in ¢ Programin C++ Program in JAVA Programin Python for 4 In raneo(2, it a VERDI peta Prion Factorial(0) Fin More Solitions at Python Program Fins the Prime Foctrof a Number 45: Write a Program to Convert Digits to Words. Program in © Program in C++ Program in JAVA Program in Python from oun2words impor sun2n0rss 1 host common usage. ‘eet (rundwords(12098)) 4 can also specity this for -th forsat Priat(nunzuord(13098, so-ordinat)) Fd More Solutions at Python Program to Convert Digit o Were 46: Write a Program to Find the Factorial of a Number using Recursion. Programin © Programin G++ Program in JAVA Program in Python We return 9° Factorsatin = mrit ("Factorial of*, mum, "8", factorial (nun) Fir More Solutions at Python Program Fins Factorial of a Numba sit Recueson 47: Write a Program to Reverse an Array Program in © Program in Cr+ Program in JAVA Program in Python, 'F oriver function t0 test above functton A= [18, 28, 3% Fin More Solutions at Python Program te Reverso 29 Array 48, write code to chock f two stings match where one sting contain wlderd characters c He > Land o(0) 08 18 anda ee (o> 2 and (0) == 12") oF (9 1= 0 and = 20 and (0) == 519)) sb22]) 10) oF sotve(aybf2 Drint(solve(strt,str2)) 72) 49: Write a Program to find out the Spiral Traversal of a Matrix. Programin include Hoetine © 4 Heetine «4 in af4104) = (025 2, 9) #6 13, 14, 15, 16 5 95 ines, tot = 9, rie le Cleft <= right BH top <= bottom) { LF Prine the First row from the eemeining rows */ prfnet (mad ", (top! (4))$ 1 Prine the tase coluen from the ranstning columns */ far (i= top; 1 <= bottr ‘ peinet (md ", a(t1Crignel) /* Prine the tase row from the ronaining rows */ (top <* Bottom {for (1 = righty 4 >= Le t prinef (mid ", afbortorl [11)s /* Prine the First cotven from the ronaining eotums */ Ar (late ce right) Ufor (1 = botton 49 toot 4) ( prinetcmed ", afs1 flere Fir More Solutions at ¢ Progr to Find the Spiral Trovorsal of @ Matric Programin C++ Program in JAVA 50. Write 3 code to find Fibonacci Series using Recursion c cu Java, Python ction Far Farce maar ribet FAQs on Most Asked Coding Questions in Placement Question 1: What are the basis of coding? “Tha bass of casing re Data Types, Faw controls (oops) and functional programming Question 2: Which company has the hardest coding interviow questions? Protct Sosed Companies like Google, Meroeot Arnaz ak he hares cos interview questions (Question 3: What language is used in cosing? Coding languages mostly usedin placement exams are Java C, C++ and Python. Get over 200+ course One Subscription Courses ike AUML, Cloud Computing Ethical Hacking, C, Cr, lava Python, DSA (All Language Corpative Coding Al Languages), TCS, voeys, Wipro, Amazon, DBMS, OL anothers Get Prime Course List Checkout list of all the video courses in Prepineta Prime Subscription Checkout Logged nas ojasrtallapragads. di our arate, Laz aut? Required fs are marked* stor comment hot. Post Comment Support Companies AllExame Getin Touch Getin Touch Dashboards QPrepinsta 2 decease esac cisco bine Gaza 18 Linas suger Prapnsncom 7 vitae ses i” wo Plecemens nda saeenal sean bute ae placomants wit ho bos: tucy rmareral. one classes Sectional stones @ tips by ToppersonPreplnsts, ‘Ieonsand ‘nations rivacy Policy |Copyright © 2024 Prep Insta

You might also like