From 442efba76414b78448eb396404b70cb072d001e7 Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Wed, 18 Oct 2017 20:06:40 +0530 Subject: [PATCH 01/75] Update and rename README to README.md --- README | 0 README.md | 1 + 2 files changed, 1 insertion(+) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index e69de29b..00000000 diff --git a/README.md b/README.md new file mode 100644 index 00000000..6693ab05 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convince. From ab86338096a6d7ead69ca929d19a4540fb5369b0 Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Thu, 26 Oct 2017 18:11:12 +0530 Subject: [PATCH 02/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6693ab05..ae42f2c7 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convince. +Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. From 4d7e037e83fef50570467a5dae6f2f8e2ce7dd97 Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Fri, 27 Oct 2017 19:58:23 +0530 Subject: [PATCH 03/75] Add files via upload --- Python-Logo-PNG-Image.png | Bin 0 -> 4007 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Python-Logo-PNG-Image.png diff --git a/Python-Logo-PNG-Image.png b/Python-Logo-PNG-Image.png new file mode 100644 index 0000000000000000000000000000000000000000..5237b45ff72ce52ca6a9f02f9f88bffffa5af43f GIT binary patch literal 4007 zcmW-k2{@G97r+Oh2t_HfG)0jl883$6H8W@`OO}!)Nf;Y$^T`~6r5KIA833f7Uz|U^GwEiCWHUUDa-FHK==49h6ViL44-Ur zB@XMA0yFSiy?l;^=iS%*$+o2I)i?UuLG-O2Bhcf<@;`=r6n_<;tnFV2Gjy=tWb2q* z?6n)Fd`0f$<+mm{Z$1RO+=&CP#+wpv!*4ODU|MsnE6y{OKeRP$Z%Rxe=KceEBx1di zaKsdIw>Z$}mVD4dz#|ud5aGK62%!T(g+O5bp8<0qARh=Upc=;P8Mc5LyH?aPX)dJcxz+ zP2f>mzTe>i6PS4#2&Taw2tZ`je;RuY08%mFn*)#70RFk~h&BA)5Ppw_`;6c|3=m!c z51oUD&H~q;!Tbc71EJ6H-YGytCETme=Kx`40I3B2c%JWZ!0!>@p9h4O!~Mqauq6;y z%HKH<^7Q`(FEAsLhvbit`Ob(Z*;?8CQs;HQ*_=&UbX%U-D#@ww`T%$>KD>U&{r5U+ z`lj3L+u?7=TI;qJ`l#m5PV72s(LB^wyu9+txO}>1)hf(RJI^rI+2SwmqUEoQmD!6C z(5jD0ro4=Y_{Sy9j#a(Mq*XB$k!F(vtD%vrV|xVe2xz8Yl@&i0XG!iC6c<1!Fp@z8 zLcE87H?@x(XHP~s%6Kb@20!tpjBoh7ejT=W$*;9uNv+{vs(t2OBddb}@PH)X+ zvv;l9KJ?iyG#WudiAEDGaA??E1MB%HUB-xLns}uJKZO`q?TG;%;B)0g_#>=jZ3AU3-)? zS;cK@Yh%PjOku>SxZ@+ddRJFhJs%cR!q&c*eRBDdl6QD*dIjZnS&3$UDs=0uHtr!$}IW}w$ zC|>TZ&SD3({p9vlqh9OWnmm|7^ID6=k~PcqHl!*-lX_W&21jO=S}IjO%{5EJt_hL@|p+G&`8LBrmG*;!JMv2J;;Fe%E2HWjr(c~dsX#*I%3(D~Cx-`P_0&D3T1#;)O1%9wZ6?9k_8hGkR_&o9+zZL8 zQB+R0;d)8nN;D~7j#g|kM=n&4U>PK=B`)tkG7VFaD4&zy=DwzXG2FnbVb%)sHv&)f zC`2a{(=zRl&-`bT;;okaeKZN0K z8Wu06Ib=%(Uz^da~B%!NJjmk2YaSq#~@NuX`<;WGW`%#jVy)e^0;vz`)=& zZ@9A1-B0dPMk+;tMny7jN@Jv9Y}D0`+=K$-xVipzlcsncH=+Ia+(b~dOXKP%?-=X+ zU+Y6_^9*J5`ew&*l#0`7`o4Q{9^(srIZ0$PUVKbv>1~|HG}-#ZrjWe!fHR>5iun4b zlWffuW*=pWU)jh!(3DA3$O06)2I(r}2uNk!G>GNi`F4i|<#smllXcdq)R!$Zt=)#P zzjG+JAM$(tKArpw{d-teQ~%&)x`x>vxuxOO78;nso7|#T(GhuvnFa{OLc9>^k@Vz?#IoYJG6ibaITWYFKj5hyPOA6F=rp3D-jzU)cL}&)kNBy5^NxYV%s#>md z9;KhDGTsT<9*M>X@g8d+G(G8;SUDT~<0Tdwzb?LWJ^G~u1CP*r7Qg2tQ(5~!hpr0g z5I_1HH12r0tlHnQGCKQX87agMfd`GV>%Dku?XInvpLIU(ej~c=kiz&$FKGLZssT#uj*J++(DMIMR@-C zIv0jb?$C=H%1t4WbmX)fcGyMVHaF5)Xy3X{$6Ahg2W*m81(oLO$B)-3H&FS{Yru3T z@i)U_vdBi_o4?rzY>2KlIFKR?A=uRk2-5yCbX=J*wMeUVePpP4|Bqd&G0|%B=`btgAbxjlSPLFp~m&C#{T~GAkb1`=YXg@K;!Wl_V@DrrTDpn-tdl=z?&( zNPyfR$1(aw<9%v`BdII$yrsMxTYGLSdGN?(=gYKDyyu5?ccEIfKj$sfXm{qx4d;=@ zDsEja7ZA-|*x{wP)rx8|oQVM6ZFp+t{^}n~zD#JTa|nE`dtx#X0xFNz;W{Lb9Xoj` zV{xE%Uu`>D+55@(p^slI8dV>~%9PCt6x=Z+_y$8$p^CXx<(vh!e_+O?PRdGEz5R^Y zj6QAU%s0V|;ZDUJv)1L@9c=B)Zdd9}`LVBNU4iL6bq5-$y>NyL)q6%8+_9E*g|$ak zzCCnpa-Zkbsm6RL-E*7k9k&+aYpi3k=o{{y;z8-}A{ZB#ldCATF@k#z5NtHHkZ z_2XHKQlA2@D;G85oC~Zh%x$2eLQ#(&$8U8XKT9lbeS3O5Pi&<|)%jHG@g$drXKKnn z^axq*M7UcLq$x=jpr4qDr#Za?fnV*yrX00BhyBxD* zt(D(+(N<#H?Cjo)VkRW=6xzqi&f)id2}wyY5IdpAlDnj$78aipmtC%rt_k|I4O%$g6(*MMVlmUZXjKW~@muQ1pl=|M@Umy5yr}wl_t0ZIX7r z8+;-|{6)1~*wJ5l@ZqshANNxVQ@Il9p`oGgj-8w8vs{p$-tw1eq~3ulNNy#!7)1vp zmc^GTIx&;xB9>KMr%t|p?}2CY)PzDN6ow$v?Ay13FKlNNjh5S7b4bQsbjBuSw%oQM z8XXj2odjnvI9>3pR7P>?2LY#OVl3Gl37(ZKn8O5!LAK!I9-Xzl+o?q Date: Fri, 27 Oct 2017 19:59:24 +0530 Subject: [PATCH 04/75] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ae42f2c7..59ea14e9 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. + +Python-Logo-Image From b1359442df24fc918c8c63eed5b4375d5b65634c Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Fri, 27 Oct 2017 20:02:08 +0530 Subject: [PATCH 05/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 59ea14e9..6a4914c0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. -Python-Logo-Image +Python-Logo-Image From 7bee1285a1a8c4aab58c59672abb00b18fd2e0b9 Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Fri, 27 Oct 2017 20:04:40 +0530 Subject: [PATCH 06/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a4914c0..dae46ced 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. -Python-Logo-Image + From 0ed7fa3953faa2a1959b70e0fd4f6b4bfaeedf5b Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Fri, 27 Oct 2017 20:05:34 +0530 Subject: [PATCH 07/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dae46ced..39a59911 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. - +Python Logo From 57405473a72204ba099244bafbd64bd2614e9e3d Mon Sep 17 00:00:00 2001 From: ASHWANI KUMAR Date: Fri, 27 Oct 2017 20:30:38 +0530 Subject: [PATCH 08/75] Delete Python-Logo-PNG-Image.png --- Python-Logo-PNG-Image.png | Bin 4007 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Python-Logo-PNG-Image.png diff --git a/Python-Logo-PNG-Image.png b/Python-Logo-PNG-Image.png deleted file mode 100644 index 5237b45ff72ce52ca6a9f02f9f88bffffa5af43f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4007 zcmW-k2{@G97r+Oh2t_HfG)0jl883$6H8W@`OO}!)Nf;Y$^T`~6r5KIA833f7Uz|U^GwEiCWHUUDa-FHK==49h6ViL44-Ur zB@XMA0yFSiy?l;^=iS%*$+o2I)i?UuLG-O2Bhcf<@;`=r6n_<;tnFV2Gjy=tWb2q* z?6n)Fd`0f$<+mm{Z$1RO+=&CP#+wpv!*4ODU|MsnE6y{OKeRP$Z%Rxe=KceEBx1di zaKsdIw>Z$}mVD4dz#|ud5aGK62%!T(g+O5bp8<0qARh=Upc=;P8Mc5LyH?aPX)dJcxz+ zP2f>mzTe>i6PS4#2&Taw2tZ`je;RuY08%mFn*)#70RFk~h&BA)5Ppw_`;6c|3=m!c z51oUD&H~q;!Tbc71EJ6H-YGytCETme=Kx`40I3B2c%JWZ!0!>@p9h4O!~Mqauq6;y z%HKH<^7Q`(FEAsLhvbit`Ob(Z*;?8CQs;HQ*_=&UbX%U-D#@ww`T%$>KD>U&{r5U+ z`lj3L+u?7=TI;qJ`l#m5PV72s(LB^wyu9+txO}>1)hf(RJI^rI+2SwmqUEoQmD!6C z(5jD0ro4=Y_{Sy9j#a(Mq*XB$k!F(vtD%vrV|xVe2xz8Yl@&i0XG!iC6c<1!Fp@z8 zLcE87H?@x(XHP~s%6Kb@20!tpjBoh7ejT=W$*;9uNv+{vs(t2OBddb}@PH)X+ zvv;l9KJ?iyG#WudiAEDGaA??E1MB%HUB-xLns}uJKZO`q?TG;%;B)0g_#>=jZ3AU3-)? zS;cK@Yh%PjOku>SxZ@+ddRJFhJs%cR!q&c*eRBDdl6QD*dIjZnS&3$UDs=0uHtr!$}IW}w$ zC|>TZ&SD3({p9vlqh9OWnmm|7^ID6=k~PcqHl!*-lX_W&21jO=S}IjO%{5EJt_hL@|p+G&`8LBrmG*;!JMv2J;;Fe%E2HWjr(c~dsX#*I%3(D~Cx-`P_0&D3T1#;)O1%9wZ6?9k_8hGkR_&o9+zZL8 zQB+R0;d)8nN;D~7j#g|kM=n&4U>PK=B`)tkG7VFaD4&zy=DwzXG2FnbVb%)sHv&)f zC`2a{(=zRl&-`bT;;okaeKZN0K z8Wu06Ib=%(Uz^da~B%!NJjmk2YaSq#~@NuX`<;WGW`%#jVy)e^0;vz`)=& zZ@9A1-B0dPMk+;tMny7jN@Jv9Y}D0`+=K$-xVipzlcsncH=+Ia+(b~dOXKP%?-=X+ zU+Y6_^9*J5`ew&*l#0`7`o4Q{9^(srIZ0$PUVKbv>1~|HG}-#ZrjWe!fHR>5iun4b zlWffuW*=pWU)jh!(3DA3$O06)2I(r}2uNk!G>GNi`F4i|<#smllXcdq)R!$Zt=)#P zzjG+JAM$(tKArpw{d-teQ~%&)x`x>vxuxOO78;nso7|#T(GhuvnFa{OLc9>^k@Vz?#IoYJG6ibaITWYFKj5hyPOA6F=rp3D-jzU)cL}&)kNBy5^NxYV%s#>md z9;KhDGTsT<9*M>X@g8d+G(G8;SUDT~<0Tdwzb?LWJ^G~u1CP*r7Qg2tQ(5~!hpr0g z5I_1HH12r0tlHnQGCKQX87agMfd`GV>%Dku?XInvpLIU(ej~c=kiz&$FKGLZssT#uj*J++(DMIMR@-C zIv0jb?$C=H%1t4WbmX)fcGyMVHaF5)Xy3X{$6Ahg2W*m81(oLO$B)-3H&FS{Yru3T z@i)U_vdBi_o4?rzY>2KlIFKR?A=uRk2-5yCbX=J*wMeUVePpP4|Bqd&G0|%B=`btgAbxjlSPLFp~m&C#{T~GAkb1`=YXg@K;!Wl_V@DrrTDpn-tdl=z?&( zNPyfR$1(aw<9%v`BdII$yrsMxTYGLSdGN?(=gYKDyyu5?ccEIfKj$sfXm{qx4d;=@ zDsEja7ZA-|*x{wP)rx8|oQVM6ZFp+t{^}n~zD#JTa|nE`dtx#X0xFNz;W{Lb9Xoj` zV{xE%Uu`>D+55@(p^slI8dV>~%9PCt6x=Z+_y$8$p^CXx<(vh!e_+O?PRdGEz5R^Y zj6QAU%s0V|;ZDUJv)1L@9c=B)Zdd9}`LVBNU4iL6bq5-$y>NyL)q6%8+_9E*g|$ak zzCCnpa-Zkbsm6RL-E*7k9k&+aYpi3k=o{{y;z8-}A{ZB#ldCATF@k#z5NtHHkZ z_2XHKQlA2@D;G85oC~Zh%x$2eLQ#(&$8U8XKT9lbeS3O5Pi&<|)%jHG@g$drXKKnn z^axq*M7UcLq$x=jpr4qDr#Za?fnV*yrX00BhyBxD* zt(D(+(N<#H?Cjo)VkRW=6xzqi&f)id2}wyY5IdpAlDnj$78aipmtC%rt_k|I4O%$g6(*MMVlmUZXjKW~@muQ1pl=|M@Umy5yr}wl_t0ZIX7r z8+;-|{6)1~*wJ5l@ZqshANNxVQ@Il9p`oGgj-8w8vs{p$-tw1eq~3ulNNy#!7)1vp zmc^GTIx&;xB9>KMr%t|p?}2CY)PzDN6ow$v?Ay13FKlNNjh5S7b4bQsbjBuSw%oQM z8XXj2odjnvI9>3pR7P>?2LY#OVl3Gl37(ZKn8O5!LAK!I9-Xzl+o?q Date: Fri, 27 Oct 2017 20:31:02 +0530 Subject: [PATCH 09/75] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 39a59911..3f4de723 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,2 @@ Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. -Python Logo From dab2a481ff64d913141d2a25932c1c0cec12678b Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 18:41:56 +0530 Subject: [PATCH 10/75] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f4de723..4e9963bf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. +#Python-programming-exercises +--> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. From 5b797ab12563c30fc2c9e71ffd1b7e7f5349ddbe Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 18:42:18 +0530 Subject: [PATCH 11/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4e9963bf..4902dc93 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -#Python-programming-exercises +# Python-programming-exercises --> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. From e2e0edc7d8befa416cfea034c76e4913a4f83572 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 18:42:47 +0530 Subject: [PATCH 12/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4902dc93..ac45f6b5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Python-programming-exercises ---> Contain 100+ program written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. +Contain 100+ programs written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. From 07add53861e8b6786d071d89b6638503e2916c0e Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 18:57:41 +0530 Subject: [PATCH 13/75] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 66c00215..812606a7 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -177,7 +177,7 @@ Level 2 Question: Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. -Note: i=0,1.., X-1; j=0,1,��Y-1. +Note: i=0,1.., X-1; j=0,1,¡­Y-1. Example Suppose the following inputs are given to the program: 3,5 @@ -225,7 +225,7 @@ print ','.join(items) Question 9 Level 2 -Question�� +Question£º Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. Suppose the following input is supplied to the program: Hello world @@ -425,7 +425,7 @@ Question: Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following: D 100 W 200 -�� +¡­ D means deposit while W means withdrawal. Suppose the following input is supplied to the program: D 300 @@ -568,13 +568,13 @@ for i in reverse(100): Question 21 Level 3 -Question�� +Question£º A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following: UP 5 DOWN 3 LEFT 3 RIGHT 2 -�� +¡­ The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer. Example: If the following tuples are given as input to the program: @@ -1932,7 +1932,7 @@ Use random.choice() to a random element from a list. Solution: import random -print random.choice([i for i in range(201) if i%5==0 and i%7==0]) +print (random.choice([i for i in range(201) if i%5==0 and i%7==0])) From 9bc3da5c86a8278ee8ccb0473fac6972a97ff136 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:13:08 +0530 Subject: [PATCH 14/75] Update README.md --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index ac45f6b5..b19ef69e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. +# Level Description +Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. +Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. +Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. + +# Problem template +#----------------------------------------# +Question +Hints +Solution + + From b234dc1a292bd1b55587cc0c5bdd9e58b2b1116b Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:15:27 +0530 Subject: [PATCH 15/75] Update README.md --- README.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b19ef69e..0dd0f675 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,20 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. # Level Description -Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. -Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. -Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. +-- Level 1
+Beginner
+Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. +-- Level 2
+Intermediate
+Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. +-- Level 3
+Advanced
+He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. # Problem template -#----------------------------------------# -Question -Hints -Solution +#----------------------------------------#
+Question
+Hints
+Solution
From fb3de09f205097e1b3cf4e09c948c166a263974e Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:16:30 +0530 Subject: [PATCH 16/75] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0dd0f675..21366a72 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description -- Level 1
Beginner
-Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. +Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
-- Level 2
Intermediate
-Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. +Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
-- Level 3
Advanced
He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. From 1db44732386d03546eb254d18615572985f9478a Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:20:45 +0530 Subject: [PATCH 17/75] Update README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 21366a72..6ac82550 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,20 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. # Level Description --- Level 1
-Beginner
+ +_Beginner_
Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
--- Level 2
-Intermediate
+ +_Intermediate_
Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
--- Level 3
-Advanced
+ +_Advanced_
He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. # Problem template #----------------------------------------#
-Question
-Hints
-Solution
+-Question
+-Hints
+-Solution
From efdcf527f8766659bda43b2e611ff138d8ea6c4a Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:21:11 +0530 Subject: [PATCH 18/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6ac82550..e24733dc 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ He should use Python to solve more complex problem using more rich libraries fun # Problem template #----------------------------------------#
--Question
--Hints
--Solution
+- Question
+- Hints
+- Solution
From 3be3928c3a474bda592ee8bbb71ac019d97d081e Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:21:57 +0530 Subject: [PATCH 19/75] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e24733dc..3f78f687 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description -_Beginner_
-Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
+- Beginner
+_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
-_Intermediate_
-Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
+- Intermediate
+_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
-_Advanced_
-He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. +- Advanced
+_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. # Problem template #----------------------------------------#
From 6844de51f600552b52e6352ef04425761bbe2694 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:22:21 +0530 Subject: [PATCH 20/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3f78f687..8113d8e4 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description - Beginner
-_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
+_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks._
- Intermediate
-_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
+_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
- Advanced
-_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. +_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template #----------------------------------------#
From a4a559ead2c505fb803f9e22f46a4dfed5be0788 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:23:01 +0530 Subject: [PATCH 21/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8113d8e4..9728a63a 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description -- Beginner
+# - Beginner
_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks._
-- Intermediate
+# - Intermediate
_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
-- Advanced
+# - Advanced
_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template From ecef397a3b43966c170b00c5dcfedafac51a6929 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:24:26 +0530 Subject: [PATCH 22/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9728a63a..180d2ff6 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description -# - Beginner
+**- Beginner**
_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks._
-# - Intermediate
+**- Intermediate**
_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
-# - Advanced
+**- Advanced**
_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template From 071b57f96631b693706d6d37f39bd4a9f7ee937b Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sat, 20 Oct 2018 19:24:56 +0530 Subject: [PATCH 23/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 180d2ff6..821dd8c7 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ Contain 100+ programs written in Python 2.0v. So make sure you can convert the c # Level Description -**- Beginner**
+**Beginner**
_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks._
-**- Intermediate**
+**Intermediate**
_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
-**- Advanced**
+**Advanced**
_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template From 0485286a4abb5f2dd575bc458a7a4050ef8eae37 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 16:24:41 +0530 Subject: [PATCH 24/75] Update and rename 100+ Python challenging programming exercises.txt to 100+ Python challenging programming exercises ( in Python 2x ) --- ...nging programming exercises ( in Python 2x ) | 17 ----------------- 1 file changed, 17 deletions(-) rename 100+ Python challenging programming exercises.txt => 100+ Python challenging programming exercises ( in Python 2x ) (98%) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises ( in Python 2x ) similarity index 98% rename from 100+ Python challenging programming exercises.txt rename to 100+ Python challenging programming exercises ( in Python 2x ) index 3ceb9d09..6127b1d8 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises ( in Python 2x ) @@ -1,23 +1,6 @@ 100+ Python challenging programming exercises -1. Level description -Level Description -Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. -Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. -Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. -2. Problem template - -#----------------------------------------# -Question -Hints -Solution - -3. Questions - -#----------------------------------------# -Question 1 -Level 1 Question: Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, From 326dd8c0b951b52f188d8a82631d7ed9b7419df4 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 16:49:55 +0530 Subject: [PATCH 25/75] Created using Colaboratory --- ...gramming_exercises_(_in_Python_3_x_).ipynb | 365 ++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb diff --git a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb new file mode 100644 index 00000000..89013f95 --- /dev/null +++ b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb @@ -0,0 +1,365 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "100+ Python challenging programming exercises ( in Python 3.x ).ipynb", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ashwanidv100/Python-programming-exercises/blob/master/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb)" + ] + }, + { + "metadata": { + "id": "3cNRoIuf3t_0", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# 100+ Python challenging programming exercises ( in Python 3.x)" + ] + }, + { + "metadata": { + "id": "jGZXCsmh3zk5", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 1 :\n", + "Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,\n", + "between 2000 and 3200 (both included).\n", + "The numbers obtained should be printed in a comma-separated sequence on a single line.\n", + "\n", + "**Hints: \n", + "Consider use range(#begin, #end) method**\n" + ] + }, + { + "metadata": { + "id": "-Y7hzQih4FyZ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution:\n", + "\n" + ] + }, + { + "metadata": { + "id": "ZXQA9GEO2wAH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "acc5ff9e-c3bc-4e92-a2a3-5363aed4cd2d" + }, + "cell_type": "code", + "source": [ + "l=[]\n", + "for i in range(2000, 3201):\n", + " if (i%7==0) and (i%5!=0):\n", + " l.append(str(i))\n", + "\n", + "print (','.join(l))" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "oZUKUnua4ZnB", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 2 :\n", + "\n", + "Write a program which can compute the factorial of a given numbers.\n", + "The results should be printed in a comma-separated sequence on a single line.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input: 8\n", + "Output: 40320\n", + "```\n", + "\n", + "\n", + "\n", + "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "metadata": { + "id": "oAS1xnsI5Gc4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "yBnKhW4N48P_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "7ca43d17-69eb-4687-e70e-e3512ebc5a1f" + }, + "cell_type": "code", + "source": [ + "def fact(x):\n", + " if x == 0:\n", + " return 1\n", + " return x * fact(x - 1)\n", + "\n", + "x=int(input())\n", + "print (fact(x))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "40320\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "CpLlpqwh5TYL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 3 :\n", + "With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included) and then the program should print the dictionary.\n", + "Example: \n", + "\n", + "\n", + "```\n", + "Input: 8\n", + "Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n", + "```\n", + "\n", + "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input and consider use dict()**" + ] + }, + { + "metadata": { + "id": "JYxUpjSP6nhP", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "lBlzWq2Z6dwS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "9c5f831e-273d-4123-e1a2-290631039f56" + }, + "cell_type": "code", + "source": [ + "n=int(input())\n", + "d=dict()\n", + "for i in range(1,n+1):\n", + " d[i]=i*i\n", + "\n", + "print (d)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "FL2akaO_6w8X", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 4 :\n", + "Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input: 34,67,55,33,12,98\n", + "Output: ['34', '67', '55', '33', '12', '98']\n", + " ('34', '67', '55', '33', '12', '98')\n", + "```\n", + "\n", + " **Hints: In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "tuple() method can convert list to tuple**" + ] + }, + { + "metadata": { + "id": "DVC8Uf9d7bdt", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "E_N1w8FG7dDW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "90d0f6a0-daa7-4f1a-d48e-cb842837a65f" + }, + "cell_type": "code", + "source": [ + "values= input()\n", + "l=values.split(\",\")\n", + "t=tuple(l)\n", + "print (l)\n", + "print (t)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34,67,55,33,12,98\n", + "['34', '67', '55', '33', '12', '98']\n", + "('34', '67', '55', '33', '12', '98')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "jdy5hMu07xPS", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 5 :\n", + "Define a class which has at least two methods:\n", + "\n", + "\n", + "```\n", + "getString: to get a string from console input\n", + "printString: to print the string in upper case.\n", + "```\n", + "\n", + "\n", + "Also please include simple test function to test the class methods.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input : HelloWorld\n", + "Output: HELLOWORLD\n", + "```\n", + "\n", + "\n", + "\n", + "**Hints: Use __init__ method to construct some parameters**" + ] + }, + { + "metadata": { + "id": "veR7ECN88HA8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution :" + ] + }, + { + "metadata": { + "id": "Mrd31rk68I65", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "fe5d4e57-2d51-4423-89bc-8defd8d8a06e" + }, + "cell_type": "code", + "source": [ + "class InputOutString(object):\n", + " def __init__(self):\n", + " self.s = \"\"\n", + "\n", + " def getString(self):\n", + " self.s = input()\n", + "\n", + " def printString(self):\n", + " print (self.s.upper())\n", + "\n", + "strObj = InputOutString()\n", + "strObj.getString()\n", + "strObj.printString()" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "HelloWorld\n", + "HELLOWORLD\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 4d7be9710abfbddab1c7ad17599069e6f6033c35 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 16:52:49 +0530 Subject: [PATCH 26/75] codes working in Python 3.x From 3d25e9d031d9f513af916c18fa61bf0ff11948c1 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 16:55:47 +0530 Subject: [PATCH 27/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 821dd8c7..dffe93cb 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ _Means someone who has just gone through an introductory Python course. He can s _Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
**Advanced**
-_He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques._ +_You should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. And also supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template #----------------------------------------#
From 0f3cdfab22f468c6bbae8eefdb8de9b75b4275d7 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:01:40 +0530 Subject: [PATCH 28/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dffe93cb..63266e8e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Python-programming-exercises -Contain 100+ programs written in Python 2.0v. So make sure you can convert the codes for Python 3.0v as per the convenience. +Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience. And I will be adding new Python 3.x version to get you updated # Level Description From 13b8065dfbc8240a236e20b7da1863c4d012a1ab Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:02:28 +0530 Subject: [PATCH 29/75] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 63266e8e..21dc598e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Python-programming-exercises -Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience. And I will be adding new Python 3.x version to get you updated +Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience. +# And I will be adding new Python 3.x version to get you updated. # Level Description From 78300c7e40a34b705836227b612a350705461567 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:04:29 +0530 Subject: [PATCH 30/75] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 21dc598e..cfe8376f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience. -# And I will be adding new Python 3.x version to get you updated. +**And I will be adding the new Python 3.x version to get you updated.** # Level Description @@ -19,4 +19,4 @@ _You should use Python to solve more complex problem using more rich libraries f - Hints
- Solution
- +If any one have any problem just open an Issue, I will discuss and try to fix it from there. From edf781dfbcda64d4b0a193b74b58400effd0f244 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:05:01 +0530 Subject: [PATCH 31/75] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cfe8376f..96e24eff 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Python-programming-exercises -Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience. +Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience.
**And I will be adding the new Python 3.x version to get you updated.** # Level Description @@ -18,5 +18,5 @@ _You should use Python to solve more complex problem using more rich libraries f - Question
- Hints
- Solution
- -If any one have any problem just open an Issue, I will discuss and try to fix it from there. +
+**If any one have any problem just open an Issue, I will discuss and try to fix it from there.** From 8180d3337f628683800dc4b52e840bc4ce771387 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:05:32 +0530 Subject: [PATCH 32/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 96e24eff..ce77974b 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,5 @@ _You should use Python to solve more complex problem using more rich libraries f - Question
- Hints
- Solution
-
+ **If any one have any problem just open an Issue, I will discuss and try to fix it from there.** From 46fb5172aee51ce78d17ced2504c2da02100cc5a Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:07:30 +0530 Subject: [PATCH 33/75] Your Python 3.x Version Solutions From 6810780a0b15c6a21dcf2d234f721aef54cdd53d Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:10:13 +0530 Subject: [PATCH 34/75] Your Python 3.x Solution --- ..._challenging_programming_exercises_(_in_Python_3_x_).ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb index 89013f95..6a8b5eac 100644 --- a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb +++ b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb @@ -22,7 +22,7 @@ "colab_type": "text" }, "source": [ - "[View in Colaboratory](https://colab.research.google.com/github/ashwanidv100/Python-programming-exercises/blob/master/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb)" + ] }, { @@ -362,4 +362,4 @@ ] } ] -} \ No newline at end of file +} From 7da616abb0e7a937d56317430ccc67e177ffd1e7 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:11:11 +0530 Subject: [PATCH 35/75] Your Python 3.x solutions --- ...on_challenging_programming_exercises_(_in_Python_3_x_).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb index 6a8b5eac..b4f2e7fd 100644 --- a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb +++ b/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb @@ -32,7 +32,7 @@ }, "cell_type": "markdown", "source": [ - "# 100+ Python challenging programming exercises ( in Python 3.x)" + "# 100+ Python challenging programming exercises(in Python 3.x)" ] }, { From fffe890dbb09a642706a39750e1dc42666c4fd10 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:12:14 +0530 Subject: [PATCH 36/75] Rename 100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb to 100+ Python challenging programming exercises(in Python 3.x).ipynb --- ... Python challenging programming exercises(in Python 3.x).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb => 100+ Python challenging programming exercises(in Python 3.x).ipynb (100%) diff --git a/100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb b/100+ Python challenging programming exercises(in Python 3.x).ipynb similarity index 100% rename from 100+_Python_challenging_programming_exercises_(_in_Python_3_x_).ipynb rename to 100+ Python challenging programming exercises(in Python 3.x).ipynb From f4ca8814561471900a8d6a2b04953c58bc20c7df Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 17:12:57 +0530 Subject: [PATCH 37/75] Your Python 3.x solutions --- ...ython challenging programming exercises(in Python 3.x).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/100+ Python challenging programming exercises(in Python 3.x).ipynb b/100+ Python challenging programming exercises(in Python 3.x).ipynb index b4f2e7fd..87e1f4a4 100644 --- a/100+ Python challenging programming exercises(in Python 3.x).ipynb +++ b/100+ Python challenging programming exercises(in Python 3.x).ipynb @@ -32,7 +32,7 @@ }, "cell_type": "markdown", "source": [ - "# 100+ Python challenging programming exercises(in Python 3.x)" + "# 100+ Python challenging programming exercises (in Python 3.x)" ] }, { From 029a80a2b99165b8011f531890060acfa74b90a3 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 25 Oct 2018 18:45:56 +0530 Subject: [PATCH 38/75] Update README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ce77974b..abe21763 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,13 @@ Contain 100+ programs written in Python 2.0v in text. So make sure you can conve # Level Description **Beginner**
-_Means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks._
+_Someone who has just gone through an introductory Python courses can solve some problems 1 or 2 using Python classes or functions. Normally, the answers could directly be found in the textbooks as well._
**Intermediate**
-_Means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks._
+_Someone who has learned Python, and already has a relatively strong programming background from before. And should be able to solve problems which may involve from question 3rd and above with Python classes or functions. The answers cannot be directly be found in the textbooks._
**Advanced**
-_You should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. And also supposed to solve the problem using several Python standard packages and advanced techniques._ +_You should be able to use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. And also supposed to solve the problem using several Python standard packages and advanced techniques._ # Problem template #----------------------------------------#
From d5ea9dfaf76ddf901d7f8d3365f800b3855b15ec Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Fri, 26 Oct 2018 10:33:30 +0530 Subject: [PATCH 39/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abe21763..b8fcefda 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Contain 100+ programs written in Python 2.0v in text. So make sure you can conve _Someone who has just gone through an introductory Python courses can solve some problems 1 or 2 using Python classes or functions. Normally, the answers could directly be found in the textbooks as well._
**Intermediate**
-_Someone who has learned Python, and already has a relatively strong programming background from before. And should be able to solve problems which may involve from question 3rd and above with Python classes or functions. The answers cannot be directly be found in the textbooks._
+_Someone who has learned Python, and already has a relatively strong programming background from before. And should be able to solve problems which may involve from question 3 and above with Python classes or functions. The answers cannot be directly be found in the textbooks._
**Advanced**
_You should be able to use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. And also supposed to solve the problem using several Python standard packages and advanced techniques._ From cf1a03a624525d85107927eb1dce4c2277ce2cfc Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:28:05 +0530 Subject: [PATCH 40/75] Your Python 3 Solutions --- ...gramming exercises ( in Python 3.x ).ipynb | 707 ++++++++++++++++++ 1 file changed, 707 insertions(+) create mode 100644 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb new file mode 100644 index 00000000..89a821f1 --- /dev/null +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -0,0 +1,707 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "0-10 Python challenging programming exercises ( in Python 3.x ).ipynb", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "metadata": { + "id": "3cNRoIuf3t_0", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# 0-10 Python challenging programming exercises ( in Python 3.x)" + ] + }, + { + "metadata": { + "id": "jGZXCsmh3zk5", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 1 :\n", + "Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,\n", + "between 2000 and 3200 (both included).\n", + "The numbers obtained should be printed in a comma-separated sequence on a single line.\n", + "\n", + "**Hints: \n", + "Consider use range(#begin, #end) method**\n" + ] + }, + { + "metadata": { + "id": "-Y7hzQih4FyZ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution:\n", + "\n" + ] + }, + { + "metadata": { + "id": "ZXQA9GEO2wAH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "acc5ff9e-c3bc-4e92-a2a3-5363aed4cd2d" + }, + "cell_type": "code", + "source": [ + "l=[]\n", + "for i in range(2000, 3201):\n", + " if (i%7==0) and (i%5!=0):\n", + " l.append(str(i))\n", + "\n", + "print (','.join(l))" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "oZUKUnua4ZnB", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 2 :\n", + "\n", + "Write a program which can compute the factorial of a given numbers.\n", + "The results should be printed in a comma-separated sequence on a single line.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input: 8\n", + "Output: 40320\n", + "```\n", + "\n", + "\n", + "\n", + "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "metadata": { + "id": "oAS1xnsI5Gc4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "yBnKhW4N48P_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "7ca43d17-69eb-4687-e70e-e3512ebc5a1f" + }, + "cell_type": "code", + "source": [ + "def fact(x):\n", + " if x == 0:\n", + " return 1\n", + " return x * fact(x - 1)\n", + "\n", + "x=int(input())\n", + "print (fact(x))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "40320\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "CpLlpqwh5TYL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 3 :\n", + "With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included) and then the program should print the dictionary.\n", + "Example: \n", + "\n", + "\n", + "```\n", + "Input: 8\n", + "Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n", + "```\n", + "\n", + "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input and consider use dict()**" + ] + }, + { + "metadata": { + "id": "JYxUpjSP6nhP", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "lBlzWq2Z6dwS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "9c5f831e-273d-4123-e1a2-290631039f56" + }, + "cell_type": "code", + "source": [ + "n=int(input())\n", + "d=dict()\n", + "for i in range(1,n+1):\n", + " d[i]=i*i\n", + "\n", + "print (d)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "FL2akaO_6w8X", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 4 :\n", + "Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input: 34,67,55,33,12,98\n", + "Output: ['34', '67', '55', '33', '12', '98']\n", + " ('34', '67', '55', '33', '12', '98')\n", + "```\n", + "\n", + " **Hints: In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "tuple() method can convert list to tuple**" + ] + }, + { + "metadata": { + "id": "DVC8Uf9d7bdt", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "E_N1w8FG7dDW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "90d0f6a0-daa7-4f1a-d48e-cb842837a65f" + }, + "cell_type": "code", + "source": [ + "values= input()\n", + "l=values.split(\",\")\n", + "t=tuple(l)\n", + "print (l)\n", + "print (t)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34,67,55,33,12,98\n", + "['34', '67', '55', '33', '12', '98']\n", + "('34', '67', '55', '33', '12', '98')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "jdy5hMu07xPS", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 5 :\n", + "Define a class which has at least two methods:\n", + "\n", + "\n", + "```\n", + "getString: to get a string from console input\n", + "printString: to print the string in upper case.\n", + "```\n", + "\n", + "\n", + "Also please include simple test function to test the class methods.\n", + "Example:\n", + "\n", + "\n", + "```\n", + "Input : HelloWorld\n", + "Output: HELLOWORLD\n", + "```\n", + "\n", + "\n", + "\n", + "**Hints: Use __init__ method to construct some parameters**" + ] + }, + { + "metadata": { + "id": "veR7ECN88HA8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution :" + ] + }, + { + "metadata": { + "id": "Mrd31rk68I65", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "fe5d4e57-2d51-4423-89bc-8defd8d8a06e" + }, + "cell_type": "code", + "source": [ + "class InputOutString(object):\n", + " def __init__(self):\n", + " self.s = \"\"\n", + "\n", + " def getString(self):\n", + " self.s = input()\n", + "\n", + " def printString(self):\n", + " print (self.s.upper())\n", + "\n", + "strObj = InputOutString()\n", + "strObj.getString()\n", + "strObj.printString()" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "HelloWorld\n", + "HELLOWORLD\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QHObDG5cWYle", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 6 : \n", + "\n", + "Question:\n", + "Write a program that calculates and prints the value according to the given formula:
\n", + "Q = Square root of (2*C*D/ H)
\n", + "Following are the fixed values of C and H:
\n", + "C is 50. H is 30.
\n", + "D is the variable whose values should be input to your program in a comma-separated sequence.
\n", + "\n", + "```\n", + "Example: \n", + "\n", + "Input: 100,150,180\n", + "Output: 18,22,24\n", + "```\n", + "**Hints:\n", + "If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26)\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n", + "\n" + ] + }, + { + "metadata": { + "id": "etJhrZraYu4m", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "RBcL4qZtYurF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6e7d5060-7c9c-4552-9383-f0e488452e19" + }, + "cell_type": "code", + "source": [ + "#!/usr/bin/env python\n", + "import math\n", + "c=50\n", + "h=30\n", + "value = []\n", + "items=[x for x in input().split(',')]\n", + "for d in items:\n", + " value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))\n", + "\n", + "print (','.join(value))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "100,150,180\n", + "18,22,24\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "56vA73u1ZQKV", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "" + ] + }, + { + "metadata": { + "id": "25f7txxUZUFy", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 7 :\n", + "\n", + "Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.\n", + "**Note: i=0,1.., X-1; j=0,1,Y-1**
\n", + "\n", + "\n", + "```\n", + "Example:\n", + "Input: 3,5\n", + "Output: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n", + "```\n", + "\n", + " **Hints:\n", + "Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.**" + ] + }, + { + "metadata": { + "id": "-ymNMuPpaEIs", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution :" + ] + }, + { + "metadata": { + "id": "RLmwx_BUWO8_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "b73c13f5-0b9d-4e68-8e91-08482fa49a3f" + }, + "cell_type": "code", + "source": [ + "input_str = input()\n", + "dimensions=[int(x) for x in input_str.split(',')]\n", + "rowNum=dimensions[0]\n", + "colNum=dimensions[1]\n", + "multilist = [[0 for col in range(colNum)] for row in range(rowNum)]\n", + "\n", + "for row in range(rowNum):\n", + " for col in range(colNum):\n", + " multilist[row][col]= row*col\n", + "\n", + "print (multilist)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3, 5\n", + "[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "yQWf89hNaiHt", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 8 :\n", + "\n", + "Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.\n", + "\n", + "\n", + "```\n", + "Example: \n", + "Input: without,hello,bag,world\n", + "Output: bag,hello,without,world\n", + "```\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "metadata": { + "id": "1aS3YHfVa6XJ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "8hWq8w_PaH36", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e7d895ae-73b7-405b-92f5-fbeec8c6e5e6" + }, + "cell_type": "code", + "source": [ + "items=[x for x in input().split(',')]\n", + "items.sort()\n", + "print (','.join(items))" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "without,hello,bag,world\n", + "bag,hello,without,world\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "j23c1wwpbLsZ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Question 9 :\n", + "\n", + "Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.\n", + "\n", + "\n", + "```\n", + "Example:\n", + "Input1 : Hello world Input2: Practice makes perfect\n", + "Output1 : HELLO WORLD Output2: PRACTICE MAKES PERFECT\n", + "```\n", + "** Hints: In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "metadata": { + "id": "BcDsAD7bbpbi", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution : " + ] + }, + { + "metadata": { + "id": "IXd7_Dfib5eb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + }, + "outputId": "c4e6272d-e4f6-4fa1-f05c-d6e50d763fca" + }, + "cell_type": "code", + "source": [ + "lines = []\n", + "while True:\n", + " s = input()\n", + " if s:\n", + " lines.append(s.upper())\n", + " else:\n", + " break;\n", + " \n", + "for sentence in lines:\n", + " print (sentence)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Hello world\n", + "Practice makes perfect\n", + "\n", + "HELLO WORLD\n", + "PRACTICE MAKES PERFECT\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "2OQVzhsUcjx_", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#Question 10 :\n", + "Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.\n", + " \n", + "\n", + "```\n", + "Example: \n", + "Input : hello world and practice makes perfect and hello world again\n", + "Output : again and hello makes perfect practice world\n", + "```\n", + "\n", + "\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "We use set container to remove duplicated data automatically and then use sorted() to sort the data.**\n" + ] + }, + { + "metadata": { + "id": "sQHZH_dVc7py", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Solution :" + ] + }, + { + "metadata": { + "id": "t_LQR1TZc-EO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "44a2c9cf-5e8f-40ac-f7a4-03c9d9f09c3a" + }, + "cell_type": "code", + "source": [ + "s = input()\n", + "words = [word for word in s.split(\" \")]\n", + "print (' '.join(sorted(list(set(words))))) " + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello world and practice makes perfect and hello world again\n", + "again and hello makes perfect practice world\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From dbace6e004336603383b5aa5a866d7b6de069c8a Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:31:27 +0530 Subject: [PATCH 41/75] Your Python 3 Solutions --- ...n challenging programming exercises ( in Python 3.x ).ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb index 89a821f1..6eaa680c 100644 --- a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -32,7 +32,8 @@ }, "cell_type": "markdown", "source": [ - "# 0-10 Python challenging programming exercises ( in Python 3.x)" + "# 0-10 Python challenging programming exercises
\n", + "# ( in Python 3.x)" ] }, { From 7273e8d5780a08fe938b0acec09e6b7b8e5559e4 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:31:58 +0530 Subject: [PATCH 42/75] Delete 100+ Python challenging programming exercises(in Python 3.x).ipynb --- ...programming exercises(in Python 3.x).ipynb | 365 ------------------ 1 file changed, 365 deletions(-) delete mode 100644 100+ Python challenging programming exercises(in Python 3.x).ipynb diff --git a/100+ Python challenging programming exercises(in Python 3.x).ipynb b/100+ Python challenging programming exercises(in Python 3.x).ipynb deleted file mode 100644 index 87e1f4a4..00000000 --- a/100+ Python challenging programming exercises(in Python 3.x).ipynb +++ /dev/null @@ -1,365 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "100+ Python challenging programming exercises ( in Python 3.x ).ipynb", - "version": "0.3.2", - "provenance": [], - "collapsed_sections": [], - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "view-in-github", - "colab_type": "text" - }, - "source": [ - - ] - }, - { - "metadata": { - "id": "3cNRoIuf3t_0", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# 100+ Python challenging programming exercises (in Python 3.x)" - ] - }, - { - "metadata": { - "id": "jGZXCsmh3zk5", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Question 1 :\n", - "Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,\n", - "between 2000 and 3200 (both included).\n", - "The numbers obtained should be printed in a comma-separated sequence on a single line.\n", - "\n", - "**Hints: \n", - "Consider use range(#begin, #end) method**\n" - ] - }, - { - "metadata": { - "id": "-Y7hzQih4FyZ", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Solution:\n", - "\n" - ] - }, - { - "metadata": { - "id": "ZXQA9GEO2wAH", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 54 - }, - "outputId": "acc5ff9e-c3bc-4e92-a2a3-5363aed4cd2d" - }, - "cell_type": "code", - "source": [ - "l=[]\n", - "for i in range(2000, 3201):\n", - " if (i%7==0) and (i%5!=0):\n", - " l.append(str(i))\n", - "\n", - "print (','.join(l))" - ], - "execution_count": 1, - "outputs": [ - { - "output_type": "stream", - "text": [ - "2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199\n" - ], - "name": "stdout" - } - ] - }, - { - "metadata": { - "id": "oZUKUnua4ZnB", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Question 2 :\n", - "\n", - "Write a program which can compute the factorial of a given numbers.\n", - "The results should be printed in a comma-separated sequence on a single line.\n", - "Example:\n", - "\n", - "\n", - "```\n", - "Input: 8\n", - "Output: 40320\n", - "```\n", - "\n", - "\n", - "\n", - "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input.**" - ] - }, - { - "metadata": { - "id": "oAS1xnsI5Gc4", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Solution : " - ] - }, - { - "metadata": { - "id": "yBnKhW4N48P_", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - }, - "outputId": "7ca43d17-69eb-4687-e70e-e3512ebc5a1f" - }, - "cell_type": "code", - "source": [ - "def fact(x):\n", - " if x == 0:\n", - " return 1\n", - " return x * fact(x - 1)\n", - "\n", - "x=int(input())\n", - "print (fact(x))" - ], - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "text": [ - "8\n", - "40320\n" - ], - "name": "stdout" - } - ] - }, - { - "metadata": { - "id": "CpLlpqwh5TYL", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Question 3 :\n", - "With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included) and then the program should print the dictionary.\n", - "Example: \n", - "\n", - "\n", - "```\n", - "Input: 8\n", - "Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n", - "```\n", - "\n", - "**Hints: In case of input data being supplied to the question, it should be assumed to be a console input and consider use dict()**" - ] - }, - { - "metadata": { - "id": "JYxUpjSP6nhP", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Solution : " - ] - }, - { - "metadata": { - "id": "lBlzWq2Z6dwS", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - }, - "outputId": "9c5f831e-273d-4123-e1a2-290631039f56" - }, - "cell_type": "code", - "source": [ - "n=int(input())\n", - "d=dict()\n", - "for i in range(1,n+1):\n", - " d[i]=i*i\n", - "\n", - "print (d)" - ], - "execution_count": 3, - "outputs": [ - { - "output_type": "stream", - "text": [ - "8\n", - "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n" - ], - "name": "stdout" - } - ] - }, - { - "metadata": { - "id": "FL2akaO_6w8X", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Question 4 :\n", - "Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.\n", - "Example:\n", - "\n", - "\n", - "```\n", - "Input: 34,67,55,33,12,98\n", - "Output: ['34', '67', '55', '33', '12', '98']\n", - " ('34', '67', '55', '33', '12', '98')\n", - "```\n", - "\n", - " **Hints: In case of input data being supplied to the question, it should be assumed to be a console input.\n", - "tuple() method can convert list to tuple**" - ] - }, - { - "metadata": { - "id": "DVC8Uf9d7bdt", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Solution : " - ] - }, - { - "metadata": { - "id": "E_N1w8FG7dDW", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "90d0f6a0-daa7-4f1a-d48e-cb842837a65f" - }, - "cell_type": "code", - "source": [ - "values= input()\n", - "l=values.split(\",\")\n", - "t=tuple(l)\n", - "print (l)\n", - "print (t)" - ], - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "text": [ - "34,67,55,33,12,98\n", - "['34', '67', '55', '33', '12', '98']\n", - "('34', '67', '55', '33', '12', '98')\n" - ], - "name": "stdout" - } - ] - }, - { - "metadata": { - "id": "jdy5hMu07xPS", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Question 5 :\n", - "Define a class which has at least two methods:\n", - "\n", - "\n", - "```\n", - "getString: to get a string from console input\n", - "printString: to print the string in upper case.\n", - "```\n", - "\n", - "\n", - "Also please include simple test function to test the class methods.\n", - "Example:\n", - "\n", - "\n", - "```\n", - "Input : HelloWorld\n", - "Output: HELLOWORLD\n", - "```\n", - "\n", - "\n", - "\n", - "**Hints: Use __init__ method to construct some parameters**" - ] - }, - { - "metadata": { - "id": "veR7ECN88HA8", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Solution :" - ] - }, - { - "metadata": { - "id": "Mrd31rk68I65", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - }, - "outputId": "fe5d4e57-2d51-4423-89bc-8defd8d8a06e" - }, - "cell_type": "code", - "source": [ - "class InputOutString(object):\n", - " def __init__(self):\n", - " self.s = \"\"\n", - "\n", - " def getString(self):\n", - " self.s = input()\n", - "\n", - " def printString(self):\n", - " print (self.s.upper())\n", - "\n", - "strObj = InputOutString()\n", - "strObj.getString()\n", - "strObj.printString()" - ], - "execution_count": 6, - "outputs": [ - { - "output_type": "stream", - "text": [ - "HelloWorld\n", - "HELLOWORLD\n" - ], - "name": "stdout" - } - ] - } - ] -} From d174e66eb05481f586a5199494e83836af6a1184 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:37:42 +0530 Subject: [PATCH 43/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b8fcefda..3183eff1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience.
-**And I will be adding the new Python 3.x version to get you updated.** +**And I will distributing the 100+ questions in different sets, where every set contains only 10 questions. It's easy to load the documentation with smaller file size. And It's all in new Python 3.x version to get you updated.** # Level Description From c020ceefc403cdcaf4d93deb36a046a994c0e42c Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:38:54 +0530 Subject: [PATCH 44/75] Update 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb --- ...hallenging programming exercises ( in Python 3.x ).ipynb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb index 6eaa680c..b3028dba 100644 --- a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -21,9 +21,7 @@ "id": "view-in-github", "colab_type": "text" }, - "source": [ - "\"Open" - ] + }, { "metadata": { @@ -705,4 +703,4 @@ ] } ] -} \ No newline at end of file +} From aa51ca86e1c5c18871cf3778ab48862ed04841a2 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:40:04 +0530 Subject: [PATCH 45/75] Update 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb --- ...gramming exercises ( in Python 3.x ).ipynb | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb index b3028dba..5cedcf5a 100644 --- a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -1,28 +1,4 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "0-10 Python challenging programming exercises ( in Python 3.x ).ipynb", - "version": "0.3.2", - "provenance": [], - "collapsed_sections": [], - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "view-in-github", - "colab_type": "text" - }, - - }, + { "metadata": { "id": "3cNRoIuf3t_0", From 9e7a31aefb8f2fecff13045aef9203a518c9e5b5 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 12:46:42 +0530 Subject: [PATCH 46/75] Your Python 3 Solutions --- ...gramming exercises ( in Python 3.x ).ipynb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb index 5cedcf5a..00b7954e 100644 --- a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -1,4 +1,19 @@ - +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "0-10 Python challenging programming exercises ( in Python 3.x ).ipynb", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ { "metadata": { "id": "3cNRoIuf3t_0", @@ -679,4 +694,4 @@ ] } ] -} +} \ No newline at end of file From 19634e61d6ca52ad253f23dbad0eb5185644bace Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 13:02:51 +0530 Subject: [PATCH 47/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3183eff1..4fa0830a 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,4 @@ _You should be able to use Python to solve more complex problem using more rich - Hints
- Solution
-**If any one have any problem just open an Issue, I will discuss and try to fix it from there.** +**If you have any problem just open an Issue, We will discuss and try to fix it from there.** From 248068c5a9a18790cd353cb1b7ea83c0a14665f9 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 13:05:37 +0530 Subject: [PATCH 48/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fa0830a..bc3a8f11 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience.
-**And I will distributing the 100+ questions in different sets, where every set contains only 10 questions. It's easy to load the documentation with smaller file size. And It's all in new Python 3.x version to get you updated.** +**And I will distributing the 100+ questions in different sets, where every set contains 10 questions each. And It's easy to load the documentation file as well. All codes are in Python 3.x version to get you to start in Python.** # Level Description From dc589c2b7850a971a0ea5970afe43a3c4ff402db Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 13:07:08 +0530 Subject: [PATCH 49/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc3a8f11..7d0c510f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience.
-**And I will distributing the 100+ questions in different sets, where every set contains 10 questions each. And It's easy to load the documentation file as well. All codes are in Python 3.x version to get you to start in Python.** +**And I will distributing the 100+ questions in different sets, where every set contains 10 questions each. And it's easy to load the documentation file. All codes are in Python 3.x version to give you kickstart in Python 3.** # Level Description From be6fbb4f5e6a829a496003da22eb4b16dd9af792 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Sun, 28 Oct 2018 13:07:35 +0530 Subject: [PATCH 50/75] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d0c510f..37c2adbe 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python-programming-exercises Contain 100+ programs written in Python 2.0v in text. So make sure you can convert the codes for Python 3.0v as per the convenience.
-**And I will distributing the 100+ questions in different sets, where every set contains 10 questions each. And it's easy to load the documentation file. All codes are in Python 3.x version to give you kickstart in Python 3.** +**And I will distributing the 100+ questions in different sets, where every set contains 10 questions each. And it's easy to load the documentation file. All codes are in Python 3.x version to give you kickstart in Python.** # Level Description From c1c5c5498a96cfcb895b0d598e197f686dbc79b9 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Mon, 25 Mar 2019 17:01:10 +0530 Subject: [PATCH 51/75] Update 100+ Python challenging programming exercises ( in Python 2x ) --- 100+ Python challenging programming exercises ( in Python 2x ) | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/100+ Python challenging programming exercises ( in Python 2x ) b/100+ Python challenging programming exercises ( in Python 2x ) index 6127b1d8..6f8e2ec2 100644 --- a/100+ Python challenging programming exercises ( in Python 2x ) +++ b/100+ Python challenging programming exercises ( in Python 2x ) @@ -2,7 +2,8 @@ -Question: +Question 1 +Level 1 Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. From c0a3ff9b882d8d672caeaa64431ba1df7785f5a0 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Mon, 25 Mar 2019 17:03:17 +0530 Subject: [PATCH 52/75] Delete python contents.txt --- python contents.txt | 188 -------------------------------------------- 1 file changed, 188 deletions(-) delete mode 100644 python contents.txt diff --git a/python contents.txt b/python contents.txt deleted file mode 100644 index db6c7e86..00000000 --- a/python contents.txt +++ /dev/null @@ -1,188 +0,0 @@ -Python -The below table largely covers the TOC for 5 popular books. Learning Python (Fourth Edition) has a more in-depth look at concepts than any of the other books. However this book also does not essentially cover some aspects that are covered in other books. -No. Diving into Python The Python Standard Library by Example Python Essential Reference (4th edition) The Quick Python Book Learning Python -Introductory Concepts covering installation on different OS, version history, interpreter. This section also covers questions like Why, Who, What and Where on Python. -1 1. Installing Python -2. Which Python is right for you ? -3. Python & your OS -4. Interactive Shell -5. Summary 1. Introduction (Text) 1. Tutorial Introduction -2. Lexical Conventions and Syntax 1. About Python -2. Getting Started 1. Python Q & A Session -1. Why do people use Python ? -2. Downside of using it -3. Who uses Python Today ? -4. What Can I do with Python ? -5. Python vs Language X -6. Test your Knowledge -2. How Python runs programs -1. Python Interpreter -2. Program Execution -1. Programmer View -2. Python View -3. Execution Model Variations -1. Implementation Alternatives -2. Execution Optimization Tools -3. Frozen Binaries -3. How you run programs -1. Interactive prompt -2. Your first script - -Python Object Types, Numeric Types, Data Structures, Control Structures, Scopes and Arguments -2 1. Your first program -2. Declaring Functions -3. Python Data types vs Other Languages -4. Documenting Functions -5. Everything is an Object -6. The Import Search Path -7. What is an Object ? -8. Indenting Code -9. Testing Modules -10. Native Datatypes -1. Dictionaries -2. List -3. Tuples -11. Variables & referencing 1. Data Structures 1. Types and Objects -2. Operators and Expressions -3. Program Structure and Control Flow -4. Functions and Functional Programming -5. Classes and Object Oriented Programming -6. Modules, Packages and Distribution -7. Input and Output -8. Execution Environment -9. Testing, Debugging, Profiling and Tuning - -Data Structures, Algorithms & Code simplification -String & Text Handling 1. Python Overview -1. Built-in Data types -2. Control Structures -3. Module -4. OOPs -2. Basics -1. Lists -2. Dictionaries -3. Tuple -4. Sets -5. Strings -6. Control Flow -3. Functions -4. Modules and Scoping Rules -5. Python Programs 1. Introducing Python Object Types -1. Why use built-in Types ? -2. Core data types -3. Numbers, Lists, Dictionaries, Tuples, Files, Other Core Types -4. User Defined Classes -2. Numeric Types -1. Literals, Built-in tools, expression operators -2. Formats, Comparisons, Division, Precision -3. Complex Numbers -4. Hexadecimal, Octal & Binary -5. Bitwise Operations -6. Decimal, Fraction, Sets, Booleans - -1. Statements & Syntax -2. Assignments, Expressions & Syntax -3. If Tests & Syntax Rules -4. Scopes -5. Arguments -Built-in functions, Function Design, Recursive Functions, Introspection, Annotations, Lambda, Filter and Reduce -3 1. Power of Introspection -1. Optional and Named Arguments -2. type, str, dir and other built-in functions -3. Object References with getattr -4. Filtering Lists -5. Lambda Functions -6. Real world Lambda functions - None 1. Built-in functions -2. Python run-time services None Built-in functions are covered as part of the topic above but from a numeric perspective -1. Advanced Function Topics -1. Function Design -2. Recursive Functions -3. Attributes and Annotation -4. Lambda -5. Mapping Functions over sequences -6. Filter and Reduce - -Special Class Attributes -Display Tool -OOPS, Modules -4 1. Objects and Object Orientation -1. Importing Modules -2. Defining Classes -3. Initializing and Coding Classes -4. Self & __init__ -5. Instantiating Classes -6. Garbage Collection -7. Wrapper Classes -8. Special Class Methods -9. Advanced Class Methods -10. Class Attributes -11. Private Functions None Covered partially section 2 1. Packages -2. Data Types and Objects -3. Advanced Object Oriented Features 1. Modules -1. Why use Modules ? -2. Program Architecture -3. Module Search Path -4. Module Creation & Usage -5. Namespaces -6. Reloading Modules -7. Packages -2. Advanced Module Topics -1. Data Hiding in Modules -2. as Extension for import and from -3. Modules are Objects: Metaprograms -4. Transitive Module Reloads -5. Module Design Concepts -6. Module Gotchas -3. OOP -1. Why use classes ? -2. Classes & Instances -3. Attribute Inheritance Search -4. Class Method Calls -5. Class Trees -6. Class Objects & Default Behavior -7. Instance Objects are Concrete Items -8. Intercepting Python Operators -9. Classes Vs. Dictionaries -10. Class customization by Inheritance -11. Operator Overloading -12. Subclasses -13. Polymorphism in Action -14. Designing with Classes -15. Mix-in Classes -Advanced Class Topics -5 None None None None 1. Advanced Class Topics -1. Extending Types by Embedding -2. Extending Types by Subclassing -3. Static and Class Methods -4. Decorators and Metaclasses -5. Class Gotchas -Exceptions -6 1. Exceptions and File Handling -1. Handling Exceptions -2. Using exceptions for other purposes 1. Exceptions 1. Exceptions Basics -1. Why use Exceptions ? -2. Default Exception Handler -3. User-Defined Exceptions -4. Class Based Exceptions -5. Designing with Exceptions -XML, HTTP, SOAP, Network Programming, I18N, Unicode -7 1. Regular Expressions -2. Parsing / Processing Mark-up languages (HTML, XML) -1. Unicode -3. HTTP Web Services -1. Headers -2. Debugging -4. SOAP Web Services 1. Networking -2. Internet -3. Email -4. Internationalization and Localization 1. Network Programming and Sockets -2. Internet Application Programming -3. Web Programming -4. Internet Data Handling & Encoding 1. Network, web programming 1. Unicode and Bytes Strings -Miscellaneous -8 None 1. Algorithms -2. Cryptography -3. Data compression and archiving -4. Processes and Threads -5. Data persistence & exchange 1. Extending & Embedding Python 1. GUI None From 4e82dda35b463092ab2815790fd2264fcf389307 Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 18 Oct 2020 14:39:25 +0530 Subject: [PATCH 53/75] Added Question 11 to 20 in Python 3. --- ...ogramming_exercises_(_in_Python_3_x).ipynb | 714 ++++++++++++++++++ 1 file changed, 714 insertions(+) create mode 100644 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb diff --git a/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb b/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb new file mode 100644 index 00000000..3c0a58b6 --- /dev/null +++ b/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb @@ -0,0 +1,714 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "11-20 Python challenging programming exercises ( in Python 3.x).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyO5G+ZRjGhSREvDO9UzJu0Z", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dCYfUyWXVwxU" + }, + "source": [ + "# **Question 11:**\n", + "\n", + "Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.\n", + "\n", + "Example:\n", + "```\n", + " Input: 0100,0011,1010,1001\n", + "\n", + " output: 1010\n", + "```\n", + "\n", + "Notes: Assume the data is input by console.\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YZE1RxkbanjK" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kHOUUqenVHWF", + "outputId": "22c43a22-3398-401b-8dd1-7691a130a578", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "value = []\n", + "items= [x for x in input().split(',')]\n", + "for p in items:\n", + " intp = int(p, 2)\n", + " if not intp%5:\n", + " value.append(p)\n", + "\n", + "print(','.join(value))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0100,0011,1010,1001\n", + "1010\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ITislaFibmei" + }, + "source": [ + "# **Question 12:**\n", + "\n", + "Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.\n", + "The numbers obtained should be printed in a comma-separated sequence on a single line.\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lnSJxW8ecGTv" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bzmhG5ecb9gv", + "outputId": "446500c4-41c1-4065-9ec7-454455c830ef", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "values = []\n", + "for i in range(1000, 3001):\n", + " s = str(i)\n", + " if (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):\n", + " values.append(s)\n", + "print(\",\".join(values))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1000,1002,1004,1006,1008,1020,1022,1024,1026,1028,1040,1042,1044,1046,1048,1060,1062,1064,1066,1068,1080,1082,1084,1086,1088,1200,1202,1204,1206,1208,1220,1222,1224,1226,1228,1240,1242,1244,1246,1248,1260,1262,1264,1266,1268,1280,1282,1284,1286,1288,1400,1402,1404,1406,1408,1420,1422,1424,1426,1428,1440,1442,1444,1446,1448,1460,1462,1464,1466,1468,1480,1482,1484,1486,1488,1600,1602,1604,1606,1608,1620,1622,1624,1626,1628,1640,1642,1644,1646,1648,1660,1662,1664,1666,1668,1680,1682,1684,1686,1688,1800,1802,1804,1806,1808,1820,1822,1824,1826,1828,1840,1842,1844,1846,1848,1860,1862,1864,1866,1868,1880,1882,1884,1886,1888,2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,2048,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,2266,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,2484,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,2802,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,2862,2864,2866,2868,2880,2882,2884,2886,2888,3000\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RX9yhRAUeHS8" + }, + "source": [ + "# **Question 13:**\n", + "Write a program that accepts a sentence and calculate the number of letters and digits.\n", + "\n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "hello world! 123\n", + "```\n", + "Then, the output should be:\n", + "```\n", + "LETTERS 10\n", + "DIGITS 3\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2fdpNfBhfxCG" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-mXwVxIfeSVl", + "outputId": "4cd2456e-6d23-420a-ddab-6d228b8c1d1b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "s = input()\n", + "d={\"DIGITS\":0, \"LETTERS\":0}\n", + "for c in s:\n", + " if c.isdigit():\n", + " d[\"DIGITS\"]+=1\n", + " elif c.isalpha():\n", + " d[\"LETTERS\"]+=1\n", + " else:\n", + " pass\n", + "print(\"LETTERS\", d[\"LETTERS\"])\n", + "print(\"DIGITS\", d[\"DIGITS\"])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello world! 123\n", + "LETTERS 10\n", + "DIGITS 3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GVawfKVogCK-" + }, + "source": [ + "\n", + "# **Question 14:**\n", + "Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.\n", + "\n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "Hello world!\n", + "```\n", + "Then, the output should be:\n", + "```\n", + "UPPER CASE 1\n", + "LOWER CASE 9\n", + "```\n", + "Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s9RwdUKphTh6" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "h-crG-T8gXnH", + "outputId": "329972e9-9425-4e9d-977b-bedea5ca3198", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "s = input()\n", + "d={\"UPPER CASE\":0, \"LOWER CASE\":0}\n", + "for c in s:\n", + " if c.isupper():\n", + " d[\"UPPER CASE\"]+=1\n", + " elif c.islower():\n", + " d[\"LOWER CASE\"]+=1\n", + " else:\n", + " pass\n", + "print(\"UPPER CASE\", d[\"UPPER CASE\"])\n", + "print(\"LOWER CASE\", d[\"LOWER CASE\"])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Hello World!\n", + "UPPER CASE 2\n", + "LOWER CASE 8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GyaeC9XwhYqM" + }, + "source": [ + "# **Question 15:**\n", + "Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.\n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "9\n", + "```\n", + "Then, the output should be:\n", + "```\n", + "11106\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eCsb8M-GhsWG" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fZInGI0YhnwE", + "outputId": "945d5235-f439-48a8-85cd-2640890cfca2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "a = input()\n", + "n1 = int( \"%s\" % a )\n", + "n2 = int( \"%s%s\" % (a,a) )\n", + "n3 = int( \"%s%s%s\" % (a,a,a) )\n", + "n4 = int( \"%s%s%s%s\" % (a,a,a,a) )\n", + "print(n1+n2+n3+n4)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "9\n", + "11106\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3PpK3qafiTwz" + }, + "source": [ + "# **Question 16:**\n", + "Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.\n", + "\n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "1,2,3,4,5,6,7,8,9\n", + "```\n", + "Then, the output should be:\n", + "```\n", + "1,3,5,7,9\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a62ymVFXiyGx" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "M0ivNxNOidki", + "outputId": "dff76cff-0849-43c0-8415-c825f0cdefd5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "values = input()\n", + "numbers = [x for x in values.split(\",\") if int(x)%2!=0]\n", + "print(\",\".join(numbers))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1,2,3,4,5,6,7,8,9\n", + "1,3,5,7,9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kPvEPe00jCEF" + }, + "source": [ + "# **Question 17:**\n", + "Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:\n", + "\n", + "D 100\n", + "\n", + "W 200\n", + "\n", + "D means deposit while W means withdrawal.\n", + "\n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "D 300\n", + "D 300\n", + "W 200\n", + "D 100\n", + "```\n", + "\n", + "Then, the output should be:\n", + "```\n", + "500\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XvUkZfmZj5He" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "uprZugDYjPJ6", + "outputId": "538efebe-4097-4ed1-844a-2975906aec0a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + } + }, + "source": [ + "netAmount = 0\n", + "while True:\n", + " s = input()\n", + " if not s:\n", + " break\n", + " values = s.split(\" \")\n", + " operation = values[0]\n", + " amount = int(values[1])\n", + " if operation==\"D\":\n", + " netAmount+=amount\n", + " elif operation==\"W\":\n", + " netAmount-=amount\n", + " else:\n", + " pass\n", + "print(netAmount)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "D 300\n", + "D 300\n", + "W 200\n", + "D 100\n", + "\n", + "500\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GszEpE98seP1" + }, + "source": [ + "# **Question 18:**\n", + "A website requires the users to input username and password to register. Write a program to check the validity of password input by users.\n", + "Following are the criteria for checking the password:\n", + "1. At least 1 letter between [a-z]\n", + "2. At least 1 number between [0-9]\n", + "1. At least 1 letter between [A-Z]\n", + "3. At least 1 character from [$#@]\n", + "4. Minimum length of transaction password: 6\n", + "5. Maximum length of transaction password: 12\n", + "Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.\n", + "\n", + "Example:\n", + "\n", + "If the following passwords are given as input to the program:\n", + "```\n", + "ABd1234@1,a F1#,2w3E*,2We3345\n", + "```\n", + "Then, the output of the program should be:\n", + "```\n", + "ABd1234@1\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GVBp_rXdtJ-5" + }, + "source": [ + "# **Solutions:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SmzpO2BUssa4", + "outputId": "d8164f55-649e-4991-9bc8-dbd7e2106b93", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "value = []\n", + "items=[x for x in input().split(',')]\n", + "for p in items:\n", + " if len(p)<6 or len(p)>12:\n", + " continue\n", + " else:\n", + " pass\n", + " if not re.search(\"[a-z]\",p):\n", + " continue\n", + " elif not re.search(\"[0-9]\",p):\n", + " continue\n", + " elif not re.search(\"[A-Z]\",p):\n", + " continue\n", + " elif not re.search(\"[$#@]\",p):\n", + " continue\n", + " elif re.search(\"\\s\",p):\n", + " continue\n", + " else:\n", + " pass\n", + " value.append(p)\n", + "print(\",\".join(value))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "ABd1234@1,a F1#,2w3E*,2We3345\n", + "ABd1234@1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0srCj1jruHvD" + }, + "source": [ + "# **Question 19:**\n", + "You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:\n", + "\n", + "1: Sort based on name;\n", + "\n", + "2: Then sort based on age;\n", + "\n", + "3: Then sort by score.\n", + "\n", + "The priority is that name > age > score.\n", + "If the following tuples are given as input to the program:\n", + "```\n", + "Tom,19,80\n", + "John,20,90\n", + "Jony,17,91\n", + "Jony,17,93\n", + "Json,21,85\n", + "```\n", + "Then, the output of the program should be:\n", + "```\n", + "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "We use itemgetter to enable multiple sort keys.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Bier0x-guwvS" + }, + "source": [ + "# **Solutions:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "X9vwlMiluWcE", + "outputId": "c940b685-ca58-448f-fc33-0bf91260dfeb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + } + }, + "source": [ + "from operator import itemgetter, attrgetter\n", + "l = []\n", + "while True:\n", + " s = input()\n", + " if not s:\n", + " break\n", + " l.append(tuple(s.split(\",\")))\n", + "print(sorted(l, key=itemgetter(0,1,2)))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Tom,19,80\n", + "John,20,90\n", + "Jony,17,91\n", + "Jony,17,93\n", + "Json,21,85\n", + "\n", + "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O84F8m1Rwjbi" + }, + "source": [ + "# **Question 20:**\n", + "Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.\n", + "\n", + "**Hints:\n", + "Consider use yield**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H3aZ9vglwxoB" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "i6Muna7GwtPX", + "outputId": "840c2f0f-718d-42ac-d202-f3d19713f709", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 272 + } + }, + "source": [ + "def putNumbers(n):\n", + " i = 0\n", + " while i Date: Sun, 18 Oct 2020 15:22:20 +0530 Subject: [PATCH 54/75] Rename 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb to 11-20 Python challenging programming exercises(in Python 3x).ipynb Rename the Heading --- ...Python challenging programming exercises(in Python 3x).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb => 11-20 Python challenging programming exercises(in Python 3x).ipynb (99%) diff --git a/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb b/11-20 Python challenging programming exercises(in Python 3x).ipynb similarity index 99% rename from 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb rename to 11-20 Python challenging programming exercises(in Python 3x).ipynb index 3c0a58b6..a9ce8f29 100644 --- a/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb +++ b/11-20 Python challenging programming exercises(in Python 3x).ipynb @@ -711,4 +711,4 @@ ] } ] -} \ No newline at end of file +} From abbf9cbc7af8dcec8b5a25e1dc739fd8e29cfe7d Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 18 Oct 2020 16:57:53 +0530 Subject: [PATCH 55/75] Added 21-30 Question in Python 3.x --- ... programming exercises(in Python 3x).ipynb | 660 ++++++++++++++++++ 1 file changed, 660 insertions(+) create mode 100644 21-30 Python challenging programming exercises(in Python 3x).ipynb diff --git a/21-30 Python challenging programming exercises(in Python 3x).ipynb b/21-30 Python challenging programming exercises(in Python 3x).ipynb new file mode 100644 index 00000000..f3fbeb52 --- /dev/null +++ b/21-30 Python challenging programming exercises(in Python 3x).ipynb @@ -0,0 +1,660 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "21-30-Python-challenging-programming-exercises-(in-Python-3x).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyOAW3NiHeCk497PQ5h6R1qy", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "I0tWsfOcEsyZ" + }, + "source": [ + "# **Question 21:**\n", + "A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:\n", + "\n", + "UP 5\n", + "\n", + "DOWN 3\n", + "\n", + "LEFT 3\n", + "\n", + "RIGHT 2\n", + "­\n", + "\n", + "The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.\n", + "Example:\n", + "\n", + "If the following tuples are given as input to the program:\n", + "```\n", + "UP 5\n", + "DOWN 3\n", + "LEFT 3\n", + "RIGHT 2\n", + "```\n", + "Then, the output of the program should be:\n", + "```\n", + "2\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rs7Ul7dOFR6N" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1INYFzJwEyUe", + "outputId": "f4e6b0d2-f050-474e-f2ab-04cd94c83bac", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + } + }, + "source": [ + "import math\n", + "pos = [0,0]\n", + "while True:\n", + " s = input()\n", + " if not s:\n", + " break\n", + " movement = s.split(\" \")\n", + " direction = movement[0]\n", + " steps = int(movement[1])\n", + " if direction==\"UP\":\n", + " pos[0]+=steps\n", + " elif direction==\"DOWN\":\n", + " pos[0]-=steps\n", + " elif direction==\"LEFT\":\n", + " pos[1]-=steps\n", + " elif direction==\"RIGHT\":\n", + " pos[1]+=steps\n", + " else:\n", + " pass\n", + "\n", + "print(int(round(math.sqrt(pos[1]**2+pos[0]**2))))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "UP 5\n", + "DOWN 3\n", + "LEFT 3\n", + "RIGHT 2\n", + "\n", + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dG8u5sHuGt5M" + }, + "source": [ + "# **Question 22:**\n", + "Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. \n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.\n", + "```\n", + "\n", + "Then, the output should be:\n", + "```\n", + "2:2\n", + "3.:1\n", + "3?:1\n", + "New:1\n", + "Python:5\n", + "Read:1\n", + "and:1\n", + "between:1\n", + "choosing:1\n", + "or:2\n", + "to:1\n", + "```\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T7pmEajLGy8w" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "m5HFsTufGxDp", + "outputId": "dc3aa886-6b29-48cb-c33c-7c2199976be3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + } + }, + "source": [ + "freq = {} # frequency of words in text\n", + "line = input()\n", + "for word in line.split():\n", + " freq[word] = freq.get(word,0)+1\n", + "\n", + "words = freq.keys()\n", + "sorted(words)\n", + "\n", + "for w in words:\n", + " print(\"%s:%d\" % (w,freq[w]))" + ], + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "text": [ + "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3\n", + "New:1\n", + "to:1\n", + "Python:5\n", + "or:2\n", + "choosing:1\n", + "between:1\n", + "2:2\n", + "and:1\n", + "3?:1\n", + "Read:1\n", + "3:1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wXIZyTkFJR_a" + }, + "source": [ + "# **Question 23:**\n", + " \n", + " Write a method which can calculate square value of number\n", + "\n", + "**Hints:\n", + " Using the ** operator**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KEfhxVC0KuhT" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "j8H0zdLAKs8J", + "outputId": "5c877d6e-f39e-45c2-9599-2047eca5c706", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def square(num):\n", + " return num ** 2\n", + "\n", + "print(square(2))\n", + "print(square(3))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "4\n", + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j7IBphXBLme_" + }, + "source": [ + "# **Question 24:**\n", + " Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.\n", + " \n", + "Please write a program to print some Python built-in functions documents, such as abs(), int(),input()\n", + "And add document for your own function\n", + "\n", + "**Hints:\n", + "The built-in document method is** ``` __doc__ ```" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sLYh82fMO_J6" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xp2fKTFvO7L9", + "outputId": "9947bf58-21fb-4f41-dd2b-a355e3c62d19", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 459 + } + }, + "source": [ + "print(abs.__doc__)\n", + "print(int.__doc__)\n", + "print(input.__doc__)\n", + "def square(num):\n", + "\n", + " '''\n", + "Return the square value of the input number.\n", + "The input number must be integer.\n", + "'''\n", + " return num ** 2\n", + "\n", + "print(square(2))\n", + "print(square.__doc__)" + ], + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Return the absolute value of the argument.\n", + "int(x=0) -> integer\n", + "int(x, base=10) -> integer\n", + "\n", + "Convert a number or string to an integer, or return 0 if no arguments\n", + "are given. If x is a number, return x.__int__(). For floating point\n", + "numbers, this truncates towards zero.\n", + "\n", + "If x is not a number or if base is given, then x must be a string,\n", + "bytes, or bytearray instance representing an integer literal in the\n", + "given base. The literal can be preceded by '+' or '-' and be surrounded\n", + "by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n", + "Base 0 means to interpret the base from the string as an integer literal.\n", + ">>> int('0b100', base=0)\n", + "4\n", + "Forward raw_input to frontends\n", + "\n", + " Raises\n", + " ------\n", + " StdinNotImplentedError if active frontend doesn't support stdin.\n", + " \n", + "4\n", + "\n", + "Return the square value of the input number.\n", + "The input number must be integer.\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3WFnXSXxTdiB" + }, + "source": [ + "# **Question 25:**\n", + "Define a class, which have a class parameter and have a same instance parameter.\n", + "\n", + "**Hints:\n", + "Define a instance parameter, need add it in ```__init__``` method.You can init a object with construct parameter or set the value later.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HxThJu-1UKMp" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9IAe-wIwT7h6", + "outputId": "8a8a59c7-bf98-483a-c680-367780d14f22", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "class Person:\n", + " # Define the class parameter \"name\"\n", + " name = \"Person\"\n", + " \n", + " def __init__(self, name = None):\n", + " # self.name is the instance parameter\n", + " self.name = name\n", + "\n", + "jeffrey = Person(\"Jeffrey\")\n", + "print(\"%s name is %s\" % (Person.name, jeffrey.name))\n", + "\n", + "nico = Person()\n", + "nico.name = \"Nico\"\n", + "print(\"%s name is %s\" % (Person.name, nico.name))" + ], + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Person name is Jeffrey\n", + "Person name is Nico\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "djLbfpylUSDs" + }, + "source": [ + "# **Question 26:**\n", + "Define a function which can compute the sum of two numbers.\n", + "\n", + "**Hints:\n", + "Define a function with two numbers as arguments. You can compute the sum in the function and return the value.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ohhD2HPRUmGe" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "O-vPrYdZUjHL", + "outputId": "6d41be4a-84ae-4e4c-d6b1-890aadcfbae5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def SumFunction(number1, number2):\n", + "\treturn number1+number2\n", + "print(SumFunction(1,2))" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4-4tEvgZU7HI" + }, + "source": [ + "# **Question 27:**\n", + "Define a function that can convert a integer into a string and print it in console.\n", + "\n", + "**Hints:\n", + "Use str() to convert a number to string.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wm_in-UrVHD1" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cf4B5Y1EVCKw", + "outputId": "6119c1ee-9485-49c6-80e4-9285ca71162d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(n):\n", + "\tprint(str(n))\n", + "print(printValue(3))" + ], + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U9_tXcSrV9QG" + }, + "source": [ + "# **Question 28:**\n", + "Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.\n", + "\n", + "**Hints:\n", + "Use int() to convert a string to integer.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fl4lfv8RWF3B" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tznUFAF2WMdM", + "outputId": "858c5bbe-4d7a-41e6-bdf6-28ebe8ca5314", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tprint(int(s1)+int(s2))\n", + "print(printValue(\"3\",\"4\"))" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "7\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z6CdCNdPWjRG" + }, + "source": [ + "# **Question 29:**\n", + "Define a function that can accept two strings as input and concatenate them and then print it in console.\n", + "\n", + "**Hints:\n", + "Use + to concatenate the strings**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mebHXsiTWyIE" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zGi49t9SW3zL", + "outputId": "2a362e90-49ab-4be3-de10-f142cfc38c09", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tprint(s1+s2)\n", + "\n", + "print(printValue(\"3\",\"4\")) " + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "28d2DMkMXcTS" + }, + "source": [ + "# **Question 30:**\n", + "Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.\n", + "\n", + "**Hints:\n", + "Use len() function to get the length of a string**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gsIr6NPgYUhI" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MvEa3LwZXqcZ", + "outputId": "9f3cffea-def9-4c8a-fbb6-b11acee7611b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tlen1 = len(s1)\n", + "\tlen2 = len(s2)\n", + "\tif len1>len2:\n", + "\t\tprint(s1)\n", + "\telif len2>len1:\n", + "\t\tprint(s2)\n", + "\telse:\n", + "\t\tprint(s1)\n", + "\t\tprint(s2)\n", + "print(printValue(\"one\",\"three\"))" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "three\n", + "None\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 2c32d4d5aedc3cd9ea3dcb11b9c0f78aa5a9999a Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 25 Oct 2020 09:24:37 +0530 Subject: [PATCH 56/75] Added 31-40 Questions in python 3.x --- ...programming exercise( in Python 3x ).ipynb | 574 ++++++++++++++++++ 1 file changed, 574 insertions(+) create mode 100644 31- 40 python programming exercise( in Python 3x ).ipynb diff --git a/31- 40 python programming exercise( in Python 3x ).ipynb b/31- 40 python programming exercise( in Python 3x ).ipynb new file mode 100644 index 00000000..1370cc6b --- /dev/null +++ b/31- 40 python programming exercise( in Python 3x ).ipynb @@ -0,0 +1,574 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "31-40 python programming exercise( in Python 3x ).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyOhxzOfwxdUnZIcKsEX0ERt" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "fZtM3nNugzLE" + }, + "source": [ + "# **Question 31:**\n", + "Define a function that can accept an integer number as input and \n", + "print the \"It is an even number\" if the number is even, \n", + "otherwise print \"It is an odd number\".\n", + "\n", + "**Hints:\n", + "Use % operator to check if a number is even or odd.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "elDhYzhOnLZD" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K1tT7VCpgdoH", + "outputId": "4ebc3426-a4ed-4497-82d7-1982a14735dd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def checkValue(n):\n", + "\tif n%2 == 0:\n", + "\t\tprint(\"It is an even number\")\n", + "\telse:\n", + "\t\tprint(\"It is an odd number\")\n", + "checkValue(7)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "It is an odd number\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TNns17RAnlL-" + }, + "source": [ + "# **Question 32:**\n", + "Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.\n", + "Use ** operator to get power of a number.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HPA-qgJsoEKm" + }, + "source": [ + "# **Solution**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K8OZnrCeoBr_", + "outputId": "7fbb355a-8128-4f80-b653-fc9210db82bd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\td[1]=1\n", + "\td[2]=2**2\n", + "\td[3]=3**2\n", + "\tprint(d)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7pioZC01oeF7" + }, + "source": [ + "# **Question 33:**\n", + "Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.**\n", + "**Use ** operator to get power of a number.**\n", + "**Use range() for loops.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ckj8UvcLoz-m" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3TblIpjdoju4", + "outputId": "52aeb622-c84e-4fdf-899e-3309243e1cf5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tprint(d)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yujiv_GZArXA" + }, + "source": [ + "# **Question 34:**\n", + "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.\n", + "\n", + "**Hints:**\n", + "**Use dict[key]=value pattern to put entry into a dictionary.**\n", + "**Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Rj36SY0IAyv4" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K5eK0-glAu6O", + "outputId": "5f4e7def-356f-42c4-add1-6d45d822c4d3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 357 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tfor (k,v) in d.items():\t\n", + "\t\tprint(v)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n", + "4\n", + "9\n", + "16\n", + "25\n", + "36\n", + "49\n", + "64\n", + "81\n", + "100\n", + "121\n", + "144\n", + "169\n", + "196\n", + "225\n", + "256\n", + "289\n", + "324\n", + "361\n", + "400\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OeSUvdjDBjX5" + }, + "source": [ + "# **Question 35:**\n", + "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xDq32yekBtGD" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cBeNk8_vBqcW", + "outputId": "35f5bf55-1827-40d6-fde1-fcd8cec4012e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 357 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tfor k in d.keys():\t\n", + "\t\tprint(k)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "20\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hOh1kEx3CZ6Z" + }, + "source": [ + "# **Question 36:**\n", + "Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b5-lxfcaCoVp" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RwZOaZMBChSR" + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li)\n", + "printList()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a8Wypfx0C3pf" + }, + "source": [ + "# **Question 37:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.\n", + "Use [n1:n2] to slice a list.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L6dEZlI3DtT6" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YLb0H7OhDoKP" + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[:5])\n", + "printList()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Aijf-oP8DxkF" + }, + "source": [ + "# **Question 38:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.Use [n1:n2] to slice a list.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lTqVFL5mD7iX" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tEXDsxf5D5J3", + "outputId": "178b8395-bebd-4ef6-8229-8984296dc5b6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[-5:])\n", + "printList()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fq0XgwZiEa0s" + }, + "source": [ + "# **Question 39:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.\n", + "Use [n1:n2] to slice a list**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CulY44JIEkhv" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MNzbukbKEh5a", + "outputId": "5fbc640f-db72-4321-93d0-24caf56ff4fe", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[5:])\n", + "printList()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "muyVwy7ME3Ul" + }, + "source": [ + "# **Question 40:**\n", + "Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). \n", + "\n", + "**Hints:Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.\n", + "Use tuple() to get a tuple from a list.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y9ww5ybzFA8b" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xIoo2mqfE-Rr", + "outputId": "6cdc4435-2c32-4e5f-b030-3b948acee72c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printTuple():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(tuple(li))\n", + "printTuple()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 9e3e65a05ceebba831c0546b819f92b793eebf1e Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 25 Oct 2020 09:29:38 +0530 Subject: [PATCH 57/75] Added 31- 40 Questions in Python 3.x --- ...programming_exercise(_in_Python_3x_).ipynb | 600 ++++++++++++++++++ 1 file changed, 600 insertions(+) create mode 100644 31_40_python_programming_exercise(_in_Python_3x_).ipynb diff --git a/31_40_python_programming_exercise(_in_Python_3x_).ipynb b/31_40_python_programming_exercise(_in_Python_3x_).ipynb new file mode 100644 index 00000000..46ad3633 --- /dev/null +++ b/31_40_python_programming_exercise(_in_Python_3x_).ipynb @@ -0,0 +1,600 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "31-40 python programming exercise( in Python 3x ).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyOhxzOfwxdUnZIcKsEX0ERt" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "fZtM3nNugzLE" + }, + "source": [ + "# **Question 31:**\n", + "Define a function that can accept an integer number as input and \n", + "print the \"It is an even number\" if the number is even, \n", + "otherwise print \"It is an odd number\".\n", + "\n", + "**Hints:\n", + "Use % operator to check if a number is even or odd.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "elDhYzhOnLZD" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K1tT7VCpgdoH", + "outputId": "4ebc3426-a4ed-4497-82d7-1982a14735dd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def checkValue(n):\n", + "\tif n%2 == 0:\n", + "\t\tprint(\"It is an even number\")\n", + "\telse:\n", + "\t\tprint(\"It is an odd number\")\n", + "checkValue(7)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "It is an odd number\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TNns17RAnlL-" + }, + "source": [ + "# **Question 32:**\n", + "Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.\n", + "Use ** operator to get power of a number.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HPA-qgJsoEKm" + }, + "source": [ + "# **Solution**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K8OZnrCeoBr_", + "outputId": "7fbb355a-8128-4f80-b653-fc9210db82bd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\td[1]=1\n", + "\td[2]=2**2\n", + "\td[3]=3**2\n", + "\tprint(d)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7pioZC01oeF7" + }, + "source": [ + "# **Question 33:**\n", + "Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.**\n", + "**Use ** operator to get power of a number.**\n", + "**Use range() for loops.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ckj8UvcLoz-m" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3TblIpjdoju4", + "outputId": "52aeb622-c84e-4fdf-899e-3309243e1cf5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tprint(d)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yujiv_GZArXA" + }, + "source": [ + "# **Question 34:**\n", + "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.\n", + "\n", + "**Hints:**\n", + "**Use dict[key]=value pattern to put entry into a dictionary.**\n", + "**Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Rj36SY0IAyv4" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "K5eK0-glAu6O", + "outputId": "5f4e7def-356f-42c4-add1-6d45d822c4d3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 357 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tfor (k,v) in d.items():\t\n", + "\t\tprint(v)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n", + "4\n", + "9\n", + "16\n", + "25\n", + "36\n", + "49\n", + "64\n", + "81\n", + "100\n", + "121\n", + "144\n", + "169\n", + "196\n", + "225\n", + "256\n", + "289\n", + "324\n", + "361\n", + "400\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OeSUvdjDBjX5" + }, + "source": [ + "# **Question 35:**\n", + "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.\n", + "\n", + "**Hints:\n", + "Use dict[key]=value pattern to put entry into a dictionary.\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xDq32yekBtGD" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cBeNk8_vBqcW", + "outputId": "35f5bf55-1827-40d6-fde1-fcd8cec4012e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 357 + } + }, + "source": [ + "def printDict():\n", + "\td=dict()\n", + "\tfor i in range(1,21):\n", + "\t\td[i]=i**2\n", + "\tfor k in d.keys():\t\n", + "\t\tprint(k)\n", + "printDict()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "20\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hOh1kEx3CZ6Z" + }, + "source": [ + "# **Question 36:**\n", + "Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b5-lxfcaCoVp" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RwZOaZMBChSR", + "outputId": "2f2ac841-2106-4c8f-fbe0-3a6596064dcd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li)\n", + "printList()" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a8Wypfx0C3pf" + }, + "source": [ + "# **Question 37:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.\n", + "Use [n1:n2] to slice a list.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L6dEZlI3DtT6" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YLb0H7OhDoKP", + "outputId": "3a259261-454c-4aef-beb3-5f88dd0efa43", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[:5])\n", + "printList()" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Aijf-oP8DxkF" + }, + "source": [ + "# **Question 38:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.Use [n1:n2] to slice a list.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lTqVFL5mD7iX" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tEXDsxf5D5J3", + "outputId": "178b8395-bebd-4ef6-8229-8984296dc5b6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[-5:])\n", + "printList()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fq0XgwZiEa0s" + }, + "source": [ + "# **Question 39:**\n", + "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.\n", + "\n", + "**Hints:\n", + "Use ** operator to get power of a number.\n", + "Use range() for loops.\n", + "Use list.append() to add values into a list.\n", + "Use [n1:n2] to slice a list**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CulY44JIEkhv" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MNzbukbKEh5a", + "outputId": "5fbc640f-db72-4321-93d0-24caf56ff4fe", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printList():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(li[5:])\n", + "printList()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "muyVwy7ME3Ul" + }, + "source": [ + "# **Question 40:**\n", + "Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). \n", + "\n", + "**Hints:Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.\n", + "Use tuple() to get a tuple from a list.**\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y9ww5ybzFA8b" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xIoo2mqfE-Rr", + "outputId": "6cdc4435-2c32-4e5f-b030-3b948acee72c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def printTuple():\n", + "\tli=list()\n", + "\tfor i in range(1,21):\n", + "\t\tli.append(i**2)\n", + "\tprint(tuple(li))\n", + "printTuple()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From beae04e519522a193e77d9c62c2f13e4e82d8a21 Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 25 Oct 2020 09:33:33 +0530 Subject: [PATCH 58/75] Delete 31- 40 python programming exercise( in Python 3x ).ipynb --- ...programming exercise( in Python 3x ).ipynb | 574 ------------------ 1 file changed, 574 deletions(-) delete mode 100644 31- 40 python programming exercise( in Python 3x ).ipynb diff --git a/31- 40 python programming exercise( in Python 3x ).ipynb b/31- 40 python programming exercise( in Python 3x ).ipynb deleted file mode 100644 index 1370cc6b..00000000 --- a/31- 40 python programming exercise( in Python 3x ).ipynb +++ /dev/null @@ -1,574 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "31-40 python programming exercise( in Python 3x ).ipynb", - "provenance": [], - "collapsed_sections": [], - "authorship_tag": "ABX9TyOhxzOfwxdUnZIcKsEX0ERt" - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "fZtM3nNugzLE" - }, - "source": [ - "# **Question 31:**\n", - "Define a function that can accept an integer number as input and \n", - "print the \"It is an even number\" if the number is even, \n", - "otherwise print \"It is an odd number\".\n", - "\n", - "**Hints:\n", - "Use % operator to check if a number is even or odd.**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "elDhYzhOnLZD" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "K1tT7VCpgdoH", - "outputId": "4ebc3426-a4ed-4497-82d7-1982a14735dd", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def checkValue(n):\n", - "\tif n%2 == 0:\n", - "\t\tprint(\"It is an even number\")\n", - "\telse:\n", - "\t\tprint(\"It is an odd number\")\n", - "checkValue(7)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "It is an odd number\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "TNns17RAnlL-" - }, - "source": [ - "# **Question 32:**\n", - "Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.\n", - "\n", - "**Hints:\n", - "Use dict[key]=value pattern to put entry into a dictionary.\n", - "Use ** operator to get power of a number.**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "HPA-qgJsoEKm" - }, - "source": [ - "# **Solution**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "K8OZnrCeoBr_", - "outputId": "7fbb355a-8128-4f80-b653-fc9210db82bd", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def printDict():\n", - "\td=dict()\n", - "\td[1]=1\n", - "\td[2]=2**2\n", - "\td[3]=3**2\n", - "\tprint(d)\n", - "printDict()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "{1: 1, 2: 4, 3: 9}\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "7pioZC01oeF7" - }, - "source": [ - "# **Question 33:**\n", - "Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.\n", - "\n", - "**Hints:\n", - "Use dict[key]=value pattern to put entry into a dictionary.**\n", - "**Use ** operator to get power of a number.**\n", - "**Use range() for loops.**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Ckj8UvcLoz-m" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "3TblIpjdoju4", - "outputId": "52aeb622-c84e-4fdf-899e-3309243e1cf5", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def printDict():\n", - "\td=dict()\n", - "\tfor i in range(1,21):\n", - "\t\td[i]=i**2\n", - "\tprint(d)\n", - "printDict()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400}\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "yujiv_GZArXA" - }, - "source": [ - "# **Question 34:**\n", - "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.\n", - "\n", - "**Hints:**\n", - "**Use dict[key]=value pattern to put entry into a dictionary.**\n", - "**Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Rj36SY0IAyv4" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "K5eK0-glAu6O", - "outputId": "5f4e7def-356f-42c4-add1-6d45d822c4d3", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 357 - } - }, - "source": [ - "def printDict():\n", - "\td=dict()\n", - "\tfor i in range(1,21):\n", - "\t\td[i]=i**2\n", - "\tfor (k,v) in d.items():\t\n", - "\t\tprint(v)\n", - "printDict()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "1\n", - "4\n", - "9\n", - "16\n", - "25\n", - "36\n", - "49\n", - "64\n", - "81\n", - "100\n", - "121\n", - "144\n", - "169\n", - "196\n", - "225\n", - "256\n", - "289\n", - "324\n", - "361\n", - "400\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OeSUvdjDBjX5" - }, - "source": [ - "# **Question 35:**\n", - "Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.\n", - "\n", - "**Hints:\n", - "Use dict[key]=value pattern to put entry into a dictionary.\n", - "Use ** operator to get power of a number.\n", - "Use range() for loops.\n", - "Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.**\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "xDq32yekBtGD" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "cBeNk8_vBqcW", - "outputId": "35f5bf55-1827-40d6-fde1-fcd8cec4012e", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 357 - } - }, - "source": [ - "def printDict():\n", - "\td=dict()\n", - "\tfor i in range(1,21):\n", - "\t\td[i]=i**2\n", - "\tfor k in d.keys():\t\n", - "\t\tprint(k)\n", - "printDict()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n", - "7\n", - "8\n", - "9\n", - "10\n", - "11\n", - "12\n", - "13\n", - "14\n", - "15\n", - "16\n", - "17\n", - "18\n", - "19\n", - "20\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "hOh1kEx3CZ6Z" - }, - "source": [ - "# **Question 36:**\n", - "Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).\n", - "\n", - "**Hints:\n", - "Use ** operator to get power of a number.\n", - "Use range() for loops.\n", - "Use list.append() to add values into a list.**\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "b5-lxfcaCoVp" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "RwZOaZMBChSR" - }, - "source": [ - "def printList():\n", - "\tli=list()\n", - "\tfor i in range(1,21):\n", - "\t\tli.append(i**2)\n", - "\tprint(li)\n", - "printList()" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "a8Wypfx0C3pf" - }, - "source": [ - "# **Question 37:**\n", - "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.\n", - "\n", - "**Hints:\n", - "Use ** operator to get power of a number.\n", - "Use range() for loops.\n", - "Use list.append() to add values into a list.\n", - "Use [n1:n2] to slice a list.**\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "L6dEZlI3DtT6" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "YLb0H7OhDoKP" - }, - "source": [ - "def printList():\n", - "\tli=list()\n", - "\tfor i in range(1,21):\n", - "\t\tli.append(i**2)\n", - "\tprint(li[:5])\n", - "printList()" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Aijf-oP8DxkF" - }, - "source": [ - "# **Question 38:**\n", - "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.\n", - "\n", - "**Hints:\n", - "Use ** operator to get power of a number.\n", - "Use range() for loops.\n", - "Use list.append() to add values into a list.Use [n1:n2] to slice a list.**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "lTqVFL5mD7iX" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "tEXDsxf5D5J3", - "outputId": "178b8395-bebd-4ef6-8229-8984296dc5b6", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def printList():\n", - "\tli=list()\n", - "\tfor i in range(1,21):\n", - "\t\tli.append(i**2)\n", - "\tprint(li[-5:])\n", - "printList()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[256, 289, 324, 361, 400]\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Fq0XgwZiEa0s" - }, - "source": [ - "# **Question 39:**\n", - "Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.\n", - "\n", - "**Hints:\n", - "Use ** operator to get power of a number.\n", - "Use range() for loops.\n", - "Use list.append() to add values into a list.\n", - "Use [n1:n2] to slice a list**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "CulY44JIEkhv" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "MNzbukbKEh5a", - "outputId": "5fbc640f-db72-4321-93d0-24caf56ff4fe", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def printList():\n", - "\tli=list()\n", - "\tfor i in range(1,21):\n", - "\t\tli.append(i**2)\n", - "\tprint(li[5:])\n", - "printList()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "muyVwy7ME3Ul" - }, - "source": [ - "# **Question 40:**\n", - "Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). \n", - "\n", - "**Hints:Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.\n", - "Use tuple() to get a tuple from a list.**\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Y9ww5ybzFA8b" - }, - "source": [ - "# **Solution:**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "xIoo2mqfE-Rr", - "outputId": "6cdc4435-2c32-4e5f-b030-3b948acee72c", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "def printTuple():\n", - "\tli=list()\n", - "\tfor i in range(1,21):\n", - "\t\tli.append(i**2)\n", - "\tprint(tuple(li))\n", - "printTuple()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)\n" - ], - "name": "stdout" - } - ] - } - ] -} \ No newline at end of file From b33682b8f10de391bc825c84ab6b52c2a270f914 Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 25 Oct 2020 09:35:00 +0530 Subject: [PATCH 59/75] Rename 31_40_python_programming_exercise(_in_Python_3x_).ipynb to 31- 40 python programming exercise( in Python 3x ).ipynb Update 31- 40 Questions in Python 3.x --- ... => 31- 40 python programming exercise( in Python 3x ).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 31_40_python_programming_exercise(_in_Python_3x_).ipynb => 31- 40 python programming exercise( in Python 3x ).ipynb (99%) diff --git a/31_40_python_programming_exercise(_in_Python_3x_).ipynb b/31- 40 python programming exercise( in Python 3x ).ipynb similarity index 99% rename from 31_40_python_programming_exercise(_in_Python_3x_).ipynb rename to 31- 40 python programming exercise( in Python 3x ).ipynb index 46ad3633..d04f66d5 100644 --- a/31_40_python_programming_exercise(_in_Python_3x_).ipynb +++ b/31- 40 python programming exercise( in Python 3x ).ipynb @@ -597,4 +597,4 @@ ] } ] -} \ No newline at end of file +} From 60f2e0d1437b0f04847de2ce5d65e8f46547f50e Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 25 Oct 2020 10:35:06 +0530 Subject: [PATCH 60/75] Added 41 -50 Questions in Python 3.x --- ... programming exercise(in Pyhton 3.x).ipynb | 524 ++++++++++++++++++ 1 file changed, 524 insertions(+) create mode 100644 41 - 50 python programming exercise(in Pyhton 3.x).ipynb diff --git a/41 - 50 python programming exercise(in Pyhton 3.x).ipynb b/41 - 50 python programming exercise(in Pyhton 3.x).ipynb new file mode 100644 index 00000000..a076c826 --- /dev/null +++ b/41 - 50 python programming exercise(in Pyhton 3.x).ipynb @@ -0,0 +1,524 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "41-50 python programming exercise(in Pyhton 3x).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyMiOo6KxVV9VTEPth6qT/Il" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "bkfVqMXoJLlD" + }, + "source": [ + "# **Question 41:**\n", + "With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in next line. \n", + "\n", + "**Hints:\n", + "Use [n1:n2] notation to get a slice from a tuple.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZMgGsN1KJ0L9" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EvhGKwotJfZz", + "outputId": "dded2519-f415-4d24-cfff-075e55acadee", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "tp=(1,2,3,4,5,6,7,8,9,10)\n", + "tp1=tp[:5]\n", + "tp2=tp[5:]\n", + "print(tp1)\n", + "print(tp2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5)\n", + "(6, 7, 8, 9, 10)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A4Vls8quJ7wu" + }, + "source": [ + "# **Question 42:**\n", + "Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). \n", + "\n", + "**Hints: Use \"for\" to iterate the tuple. Use tuple() to generate a tuple from a list.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3J80hGSfKOS8" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "t6BdDiGXKzFP", + "outputId": "3dfe41ef-3699-406b-d844-016555808fb7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "tp=(1,2,3,4,5,6,7,8,9,10)\n", + "li=list()\n", + "for i in range(0,len(tp)): \n", + " if tp[i]%2==0:\n", + " li.append(tp[i])\n", + "tp2=tuple(li)\n", + "print(tp2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(2, 4, 6, 8, 10)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "soWNO7ztNDl8" + }, + "source": [ + "# **Question 43:**\n", + "Write a program which accepts a string as input to print \"Yes\" if the string is \"yes\" or \"YES\" or \"Yes\", otherwise print \"No\". \n", + "\n", + "**Hints:\n", + "Use if statement to judge condition.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Lwi7UhWXY823" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4VRq5-ICNmNh", + "outputId": "e18e85dd-dd43-436e-ce65-2af7a9ee5d44", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "s=input()\n", + "if s==\"yes\" or s==\"YES\" or s==\"Yes\":\n", + " print(\"Yes\")\n", + "else:\n", + " print(\"No\")" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "yes\n", + "Yes\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jm_e_lxLZft6" + }, + "source": [ + "# **Question 44:**\n", + "Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10].\n", + "\n", + "**Hints:\n", + "Use filter() to filter some elements in a list.\n", + "Use lambda to define anonymous functions.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tTkWVj2gZysk" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TdQZr_afZvuq", + "outputId": "96d7d0b7-ea56-48ec-fba6-c58830f4cd0a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [1,2,3,4,5,6,7,8,9,10]\n", + "evenNumbers = list(filter(lambda x: x%2==0, li))\n", + "print(evenNumbers)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gDgd_vkiZ2AB" + }, + "source": [ + "# **Question 45:**\n", + "Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10].\n", + "\n", + "**Hints:\n", + "Use map() to generate a list.\n", + "Use lambda to define anonymous functions.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QjwimJO5anNq" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "x4voXhAlakr6", + "outputId": "22aebc34-9acd-4950-d05b-394fbe6d01ac", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [1,2,3,4,5,6,7,8,9,10]\n", + "squaredNumbers = list(map(lambda x: x**2, li))\n", + "print(squaredNumbers)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QCqxRP6IaqLM" + }, + "source": [ + "## **Question 46:**\n", + "Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10].\n", + "\n", + "**Hints:\n", + "Use map() to generate a list.\n", + "Use filter() to filter elements of a list.\n", + "Use lambda to define anonymous functions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HBofav2rbK_t" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "lEpi-3ICbS9p", + "outputId": "6b9a3888-8f07-4ddc-9c77-5ab27d80943e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [1,2,3,4,5,6,7,8,9,10]\n", + "evenNumbers = list(map(lambda x: x**2, filter(lambda x: x%2==0, li)))\n", + "print(evenNumbers)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rdPm28K4bfqS" + }, + "source": [ + "# **Question 47:**\n", + "Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).\n", + "\n", + "**Hints:\n", + "Use filter() to filter elements of a list.\n", + "Use lambda to define anonymous functions.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iDs2ZVXKcKxJ" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "j7NjF3HDbtSW", + "outputId": "e3728cdb-cacf-48c8-c20e-1b8d33f44eda", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "evenNumbers=[]\n", + "evenNumbers = list(filter(lambda x: x%2==0, range(1,21)))\n", + "print(evenNumbers)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OBiilS37cP_0" + }, + "source": [ + "# **Question 48:**\n", + "Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).\n", + "\n", + "**Hints: Use map() to generate a list. Use lambda to define anonymous functions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bV7bbzk6ckcf" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XN-hhnmJcnbt", + "outputId": "3300c5e8-8840-445e-86d5-1060a2fbc742", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "squareNumbers=[]\n", + "squaredNumbers = list(map(lambda x: x**2, range(1,21)))\n", + "print(squaredNumbers)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Xs2ML65-dA3U" + }, + "source": [ + "# **Question 49:**\n", + "Define a class named American which has a static method called printNationality.\n", + "\n", + "**Hints: Use @staticmethod decorator to define class static method.**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ok6t5SNWdZNL" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oZUMWPy_dT5F", + "outputId": "4b157398-8e24-4b58-c61d-c5c7dbda1a3a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "class American(object):\n", + " @staticmethod\n", + " def printNationality():\n", + " print(\"America\")\n", + "anAmerican = American()\n", + "anAmerican.printNationality()\n", + "American.printNationality()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "America\n", + "America\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L6tEhKVedmZ6" + }, + "source": [ + "# **Question 50:**\n", + "Define a class named American and its subclass NewYorker.\n", + "\n", + "**Hints:\n", + "Use class Subclass(ParentClass) to define a subclass.**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CXnhLGICd1pY" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qrW6TF9pdvLj", + "outputId": "ce55ed56-7014-41b8-97c9-57915b0c6a49", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "class American(object):\n", + " pass\n", + "class NewYorker(American):\n", + " pass\n", + "\n", + "anAmerican = American()\n", + "aNewYorker = NewYorker()\n", + "print(anAmerican)\n", + "print(aNewYorker)" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "text": [ + "<__main__.American object at 0x7fee3f168b00>\n", + "<__main__.NewYorker object at 0x7fee3f1689b0>\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 4f2d6a64fcb18ef9d67a4261f91f9a240851cbf1 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 29 Oct 2020 14:58:07 +0530 Subject: [PATCH 61/75] Update 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb --- ... challenging programming exercises ( in Python 3.x ).ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb index 00b7954e..052fd96b 100644 --- a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb +++ b/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb @@ -21,7 +21,7 @@ }, "cell_type": "markdown", "source": [ - "# 0-10 Python challenging programming exercises
\n", + "# 1-10 Python challenging programming exercises
\n", "# ( in Python 3.x)" ] }, @@ -694,4 +694,4 @@ ] } ] -} \ No newline at end of file +} From f616560a06949d8fc902aef325a13b5c68a3a15b Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Thu, 29 Oct 2020 14:59:01 +0530 Subject: [PATCH 62/75] Rename 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb to 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb --- ...thon challenging programming exercises ( in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb => 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb (100%) diff --git a/0-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/1-10 Python challenging programming exercises ( in Python 3.x ).ipynb similarity index 100% rename from 0-10 Python challenging programming exercises ( in Python 3.x ).ipynb rename to 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb From 85fe43761ed87c7abc779e222fc8e2cb66099827 Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Thu, 29 Oct 2020 15:25:06 +0530 Subject: [PATCH 63/75] Added 51 - 60 python programming exercise in python 3 --- ...programming exercise( in Python 3x ).ipynb | 565 ++++++++++++++++++ 1 file changed, 565 insertions(+) create mode 100644 51 - 60 python programming exercise( in Python 3x ).ipynb diff --git a/51 - 60 python programming exercise( in Python 3x ).ipynb b/51 - 60 python programming exercise( in Python 3x ).ipynb new file mode 100644 index 00000000..b35f7638 --- /dev/null +++ b/51 - 60 python programming exercise( in Python 3x ).ipynb @@ -0,0 +1,565 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "51-60 python programming exercise( in Python 3x ).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyP7DCe2gAmVKY69QBrWjfQe" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "GLQnf8QS2lk0" + }, + "source": [ + "# **Question 51**\n", + "Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.\n", + "\n", + "**Hints:Use def methodName(self) to define a method.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X3s9thrV8iyK" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JsirS97Y8oji", + "outputId": "8a73ddda-64bb-4605-88c6-47d47e243d79", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Circle(object):\n", + " def __init__(self, r):\n", + " self.radius = r\n", + "\n", + " def area(self):\n", + " return self.radius**2*3.14\n", + "\n", + "aCircle = Circle(2)\n", + "print(aCircle.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "12.56\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3XvVjhPT89q8" + }, + "source": [ + "# **Question 52**\n", + "Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area.\n", + "\n", + "**Hints:Use def methodName(self) to define a method.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AtKMD6479KDR" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LOMijHl69M3B", + "outputId": "06598ad2-f5be-4410-9333-efdfe458ec6e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Rectangle(object):\n", + " def __init__(self, l, w):\n", + " self.length = l\n", + " self.width = w\n", + "\n", + " def area(self):\n", + " return self.length*self.width\n", + "\n", + "aRectangle = Rectangle(2,10)\n", + "print(aRectangle.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "20\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9gJiuJb7-pmL" + }, + "source": [ + "# **Question 53**\n", + "Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default.\n", + "\n", + "**Hints:To override a method in super class, we can define a method with the same name in the super class.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TtEUipcc--Ix" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rQJ1HY3U_Pci", + "outputId": "f0000d2f-2763-4888-81ba-fac0e85e323c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Shape(object):\n", + " def __init__(self):\n", + " pass\n", + "\n", + " def area(self):\n", + " return 0\n", + "\n", + "class Square(Shape):\n", + " def __init__(self, l):\n", + " Shape.__init__(self)\n", + " self.length = l\n", + "\n", + " def area(self):\n", + " return self.length*self.length\n", + "\n", + "aSquare= Square(3)\n", + "print(aSquare.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "54NuSVnj_w7u" + }, + "source": [ + "# **Question 54**\n", + "Please raise a RuntimeError exception.\n", + "\n", + "**Hints:Use raise() to raise an exception.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qkrbQzYH_4a2" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8Z7ZC9rt_6nr", + "outputId": "ac3e88fb-6443-447b-e5aa-39854432c160", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 163 + } + }, + "source": [ + "raise RuntimeError('something wrong')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "RuntimeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'something wrong'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m: something wrong" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "idxWE8pZAw80" + }, + "source": [ + "# **Question 55**\n", + "Write a function to compute 5/0 and use try/except to catch the exceptions.\n", + "\n", + "**Hints:Use try/except to catch exceptions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F0pDN8n7A-Cb" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SX4xCXVRBBbk", + "outputId": "368ef1b8-96ee-42e3-bd4d-b8f255ada392", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def throws():\n", + " return 5/0\n", + "\n", + "try:\n", + " throws()\n", + "\n", + "except ZeroDivisionError:\n", + " print(\"division by zero!\")\n", + "except Exception:\n", + " print('Caught an exception')\n", + "finally:\n", + " print('In finally block for cleanup')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "division by zero!\n", + "In finally block for cleanup\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QJ2kRbhbBXz3" + }, + "source": [ + "# **Question 56**\n", + "Define a custom exception class which takes a string message as attribute.\n", + "\n", + "**Hints:To define a custom exception, we need to define a class inherited from Exception.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XeTPaaymJ7QF" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WGwHBcSVKGCE", + "outputId": "a87f6947-b4cd-4b78-b4ff-ce9c0e784411", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class MyError(Exception):\n", + " \"\"\"My own exception class\n", + "\n", + " Attributes:\n", + " msg -- explanation of the error\n", + " \"\"\"\n", + " \n", + " def __init__(self, msg):\n", + " self.msg = msg\n", + "\n", + "error = MyError(\"something wrong\")\n", + "print(error)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "something wrong\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u3Aw0szzKjHm" + }, + "source": [ + "# **Question 57**\n", + "Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.\n", + "\n", + "Example: \n", + "```\n", + " Input: john@google.com\n", + "\n", + " Output: john\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use \\w to match letters.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fbr6sxLBKr12" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Zx0dsGLrKxVq", + "outputId": "f21975b8-1bf1-474f-f74f-7d9bc1c5b8eb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "emailAddress =input()\n", + "pat2 = \"(\\w+)@((\\w+\\.)+(com))\"\n", + "r2 = re.match(pat2,emailAddress)\n", + "print(r2.group(1))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "john@google.com\n", + "john\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7bstEM-3LbXt" + }, + "source": [ + "# **Question 58**\n", + "Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.\n", + "\n", + "Example: \n", + "```\n", + " Input: john@google.com\n", + "\n", + "output: google\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use \\w to match letters.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IJ5TIrmROqbC" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7iIw5wLzOtHh", + "outputId": "f91813f2-f038-4112-87a9-b8bd9f959340", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "emailAddress =input()\n", + "pat2 = \"(\\w+)@(\\w+)\\.(com)\"\n", + "r2 = re.match(pat2,emailAddress)\n", + "print(r2.group(2))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "john@google.com\n", + "google\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YxT4f1VxRaS0" + }, + "source": [ + "# **Question 59**\n", + "Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.\n", + "\n", + "Example:\n", + "``` \n", + " Input: 2 cats and 3 dogs.\n", + "Output: ['2', '3']\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use re.findall() to find all substring using regex.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BIXQxwA4SsFi" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-qqLpoQHRxvF", + "outputId": "625cbb22-f890-45d8-b725-14b10f7e6fee", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "s =input()\n", + "print(re.findall(\"\\d+\",s))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2 cats and 3 dogs\n", + "['2', '3']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LnAWYPmTSwJX" + }, + "source": [ + "# **Question 60**\n", + "Print a unicode string \"hello world\".\n", + "\n", + "**Hints:Use u'strings' format to define unicode string.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c5CpC9uXTAT2" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HcQNLJ4MyG3P", + "outputId": "951ba48c-cc3a-4dfe-f63f-2a2357675d50", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "unicodeString = u\"hello world!\"\n", + "print(unicodeString)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello world!\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From ae115a7248cd29990719918824fceaf184e54d94 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 12:43:24 +0530 Subject: [PATCH 64/75] Delete 100+ Python challenging programming exercises ( in Python 2x ) Remove the text python file of 2.x version. --- ...ing programming exercises ( in Python 2x ) | 2358 ----------------- 1 file changed, 2358 deletions(-) delete mode 100644 100+ Python challenging programming exercises ( in Python 2x ) diff --git a/100+ Python challenging programming exercises ( in Python 2x ) b/100+ Python challenging programming exercises ( in Python 2x ) deleted file mode 100644 index 6f8e2ec2..00000000 --- a/100+ Python challenging programming exercises ( in Python 2x ) +++ /dev/null @@ -1,2358 +0,0 @@ -100+ Python challenging programming exercises - - - -Question 1 -Level 1 -Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, -between 2000 and 3200 (both included). -The numbers obtained should be printed in a comma-separated sequence on a single line. - -Hints: -Consider use range(#begin, #end) method - -Solution: -l=[] -for i in range(2000, 3201): - if (i%7==0) and (i%5!=0): - l.append(str(i)) - -print ','.join(l) -#----------------------------------------# - -#----------------------------------------# -Question 2 -Level 1 - -Question: -Write a program which can compute the factorial of a given numbers. -The results should be printed in a comma-separated sequence on a single line. -Suppose the following input is supplied to the program: -8 -Then, the output should be: -40320 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -def fact(x): - if x == 0: - return 1 - return x * fact(x - 1) - -x=int(raw_input()) -print fact(x) -#----------------------------------------# - -#----------------------------------------# -Question 3 -Level 1 - -Question: -With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary. -Suppose the following input is supplied to the program: -8 -Then, the output should be: -{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. -Consider use dict() - -Solution: -n=int(raw_input()) -d=dict() -for i in range(1,n+1): - d[i]=i*i - -print d -#----------------------------------------# - -#----------------------------------------# -Question 4 -Level 1 - -Question: -Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. -Suppose the following input is supplied to the program: -34,67,55,33,12,98 -Then, the output should be: -['34', '67', '55', '33', '12', '98'] -('34', '67', '55', '33', '12', '98') - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. -tuple() method can convert list to tuple - -Solution: -values=raw_input() -l=values.split(",") -t=tuple(l) -print l -print t -#----------------------------------------# - -#----------------------------------------# -Question 5 -Level 1 - -Question: -Define a class which has at least two methods: -getString: to get a string from console input -printString: to print the string in upper case. -Also please include simple test function to test the class methods. - -Hints: -Use __init__ method to construct some parameters - -Solution: -class InputOutString(object): - def __init__(self): - self.s = "" - - def getString(self): - self.s = raw_input() - - def printString(self): - print self.s.upper() - -strObj = InputOutString() -strObj.getString() -strObj.printString() -#----------------------------------------# - -#----------------------------------------# -Question 6 -Level 2 - -Question: -Write a program that calculates and prints the value according to the given formula: -Q = Square root of [(2 * C * D)/H] -Following are the fixed values of C and H: -C is 50. H is 30. -D is the variable whose values should be input to your program in a comma-separated sequence. -Example -Let us assume the following comma separated input sequence is given to the program: -100,150,180 -The output of the program should be: -18,22,24 - -Hints: -If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -#!/usr/bin/env python -import math -c=50 -h=30 -value = [] -items=[x for x in raw_input().split(',')] -for d in items: - value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) - -print ','.join(value) -#----------------------------------------# - -#----------------------------------------# -Question 7 -Level 2 - -Question: -Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. -Note: i=0,1.., X-1; j=0,1,¡­Y-1. -Example -Suppose the following inputs are given to the program: -3,5 -Then, the output of the program should be: -[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] - -Hints: -Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. - -Solution: -input_str = raw_input() -dimensions=[int(x) for x in input_str.split(',')] -rowNum=dimensions[0] -colNum=dimensions[1] -multilist = [[0 for col in range(colNum)] for row in range(rowNum)] - -for row in range(rowNum): - for col in range(colNum): - multilist[row][col]= row*col - -print multilist -#----------------------------------------# - -#----------------------------------------# -Question 8 -Level 2 - -Question: -Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically. -Suppose the following input is supplied to the program: -without,hello,bag,world -Then, the output should be: -bag,hello,without,world - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -items=[x for x in raw_input().split(',')] -items.sort() -print ','.join(items) -#----------------------------------------# - -#----------------------------------------# -Question 9 -Level 2 - -Question£º -Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. -Suppose the following input is supplied to the program: -Hello world -Practice makes perfect -Then, the output should be: -HELLO WORLD -PRACTICE MAKES PERFECT - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -lines = [] -while True: - s = raw_input() - if s: - lines.append(s.upper()) - else: - break; - -for sentence in lines: - print sentence -#----------------------------------------# - -#----------------------------------------# -Question 10 -Level 2 - -Question: -Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically. -Suppose the following input is supplied to the program: -hello world and practice makes perfect and hello world again -Then, the output should be: -again and hello makes perfect practice world - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. -We use set container to remove duplicated data automatically and then use sorted() to sort the data. - -Solution: -s = raw_input() -words = [word for word in s.split(" ")] -print " ".join(sorted(list(set(words)))) -#----------------------------------------# - -#----------------------------------------# -Question 11 -Level 2 - -Question: -Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence. -Example: -0100,0011,1010,1001 -Then the output should be: -1010 -Notes: Assume the data is input by console. - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - intp = int(p, 2) - if not intp%5: - value.append(p) - -print ','.join(value) -#----------------------------------------# - -#----------------------------------------# -Question 12 -Level 2 - -Question: -Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. -The numbers obtained should be printed in a comma-separated sequence on a single line. - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -values = [] -for i in range(1000, 3001): - s = str(i) - if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): - values.append(s) -print ",".join(values) -#----------------------------------------# - -#----------------------------------------# -Question 13 -Level 2 - -Question: -Write a program that accepts a sentence and calculate the number of letters and digits. -Suppose the following input is supplied to the program: -hello world! 123 -Then, the output should be: -LETTERS 10 -DIGITS 3 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -s = raw_input() -d={"DIGITS":0, "LETTERS":0} -for c in s: - if c.isdigit(): - d["DIGITS"]+=1 - elif c.isalpha(): - d["LETTERS"]+=1 - else: - pass -print "LETTERS", d["LETTERS"] -print "DIGITS", d["DIGITS"] -#----------------------------------------# - -#----------------------------------------# -Question 14 -Level 2 - -Question: -Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters. -Suppose the following input is supplied to the program: -Hello world! -Then, the output should be: -UPPER CASE 1 -LOWER CASE 9 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -s = raw_input() -d={"UPPER CASE":0, "LOWER CASE":0} -for c in s: - if c.isupper(): - d["UPPER CASE"]+=1 - elif c.islower(): - d["LOWER CASE"]+=1 - else: - pass -print "UPPER CASE", d["UPPER CASE"] -print "LOWER CASE", d["LOWER CASE"] -#----------------------------------------# - -#----------------------------------------# -Question 15 -Level 2 - -Question: -Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a. -Suppose the following input is supplied to the program: -9 -Then, the output should be: -11106 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -a = raw_input() -n1 = int( "%s" % a ) -n2 = int( "%s%s" % (a,a) ) -n3 = int( "%s%s%s" % (a,a,a) ) -n4 = int( "%s%s%s%s" % (a,a,a,a) ) -print n1+n2+n3+n4 -#----------------------------------------# - -#----------------------------------------# -Question 16 -Level 2 - -Question: -Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers. -Suppose the following input is supplied to the program: -1,2,3,4,5,6,7,8,9 -Then, the output should be: -1,3,5,7,9 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -values = raw_input() -numbers = [x for x in values.split(",") if int(x)%2!=0] -print ",".join(numbers) -#----------------------------------------# - -Question 17 -Level 2 - -Question: -Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following: -D 100 -W 200 -D means deposit while W means withdrawal. -Suppose the following input is supplied to the program: -D 300 -D 300 -W 200 -D 100 -Then, the output should be: -500 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -netAmount = 0 -while True: - s = raw_input() - if not s: - break - values = s.split(" ") - operation = values[0] - amount = int(values[1]) - if operation=="D": - netAmount+=amount - elif operation=="W": - netAmount-=amount - else: - pass -print netAmount -#----------------------------------------# - -#----------------------------------------# -Question 18 -Level 3 - -Question: -A website requires the users to input username and password to register. Write a program to check the validity of password input by users. -Following are the criteria for checking the password: -1. At least 1 letter between [a-z] -2. At least 1 number between [0-9] -1. At least 1 letter between [A-Z] -3. At least 1 character from [$#@] -4. Minimum length of transaction password: 6 -5. Maximum length of transaction password: 12 -Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma. -Example -If the following passwords are given as input to the program: -ABd1234@1,a F1#,2w3E*,2We3345 -Then, the output of the program should be: -ABd1234@1 - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solutions: -import re -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - if len(p)<6 or len(p)>12: - continue - else: - pass - if not re.search("[a-z]",p): - continue - elif not re.search("[0-9]",p): - continue - elif not re.search("[A-Z]",p): - continue - elif not re.search("[$#@]",p): - continue - elif re.search("\s",p): - continue - else: - pass - value.append(p) -print ",".join(value) -#----------------------------------------# - -#----------------------------------------# -Question 19 -Level 3 - -Question: -You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is: -1: Sort based on name; -2: Then sort based on age; -3: Then sort by score. -The priority is that name > age > score. -If the following tuples are given as input to the program: -Tom,19,80 -John,20,90 -Jony,17,91 -Jony,17,93 -Json,21,85 -Then, the output of the program should be: -[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] - -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. -We use itemgetter to enable multiple sort keys. - -Solutions: -from operator import itemgetter, attrgetter - -l = [] -while True: - s = raw_input() - if not s: - break - l.append(tuple(s.split(","))) - -print sorted(l, key=itemgetter(0,1,2)) -#----------------------------------------# - -#----------------------------------------# -Question 20 -Level 3 - -Question: -Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n. - -Hints: -Consider use yield - -Solution: -def putNumbers(n): - i = 0 - while ilen2: - print s1 - elif len2>len1: - print s2 - else: - print s1 - print s2 - - -printValue("one","three") - - - -#----------------------------------------# -2.10 - -Question: -Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number". - -Hints: - -Use % operator to check if a number is even or odd. - -Solution -def checkValue(n): - if n%2 == 0: - print "It is an even number" - else: - print "It is an odd number" - - -checkValue(7) - - -#----------------------------------------# -2.10 - -Question: -Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys. - -Hints: - -Use dict[key]=value pattern to put entry into a dictionary. -Use ** operator to get power of a number. - -Solution -def printDict(): - d=dict() - d[1]=1 - d[2]=2**2 - d[3]=3**2 - print d - - -printDict() - - - - - -#----------------------------------------# -2.10 - -Question: -Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. - -Hints: - -Use dict[key]=value pattern to put entry into a dictionary. -Use ** operator to get power of a number. -Use range() for loops. - -Solution -def printDict(): - d=dict() - for i in range(1,21): - d[i]=i**2 - print d - - -printDict() - - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only. - -Hints: - -Use dict[key]=value pattern to put entry into a dictionary. -Use ** operator to get power of a number. -Use range() for loops. -Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs. - -Solution -def printDict(): - d=dict() - for i in range(1,21): - d[i]=i**2 - for (k,v) in d.items(): - print v - - -printDict() - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only. - -Hints: - -Use dict[key]=value pattern to put entry into a dictionary. -Use ** operator to get power of a number. -Use range() for loops. -Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs. - -Solution -def printDict(): - d=dict() - for i in range(1,21): - d[i]=i**2 - for k in d.keys(): - print k - - -printDict() - - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included). - -Hints: - -Use ** operator to get power of a number. -Use range() for loops. -Use list.append() to add values into a list. - -Solution -def printList(): - li=list() - for i in range(1,21): - li.append(i**2) - print li - - -printList() - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list. - -Hints: - -Use ** operator to get power of a number. -Use range() for loops. -Use list.append() to add values into a list. -Use [n1:n2] to slice a list - -Solution -def printList(): - li=list() - for i in range(1,21): - li.append(i**2) - print li[:5] - - -printList() - - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list. - -Hints: - -Use ** operator to get power of a number. -Use range() for loops. -Use list.append() to add values into a list. -Use [n1:n2] to slice a list - -Solution -def printList(): - li=list() - for i in range(1,21): - li.append(i**2) - print li[-5:] - - -printList() - - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list. - -Hints: - -Use ** operator to get power of a number. -Use range() for loops. -Use list.append() to add values into a list. -Use [n1:n2] to slice a list - -Solution -def printList(): - li=list() - for i in range(1,21): - li.append(i**2) - print li[5:] - - -printList() - - -#----------------------------------------# -2.10 - -Question: -Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). - -Hints: - -Use ** operator to get power of a number. -Use range() for loops. -Use list.append() to add values into a list. -Use tuple() to get a tuple from a list. - -Solution -def printTuple(): - li=list() - for i in range(1,21): - li.append(i**2) - print tuple(li) - -printTuple() - - - -#----------------------------------------# -2.10 - -Question: -With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line. - -Hints: - -Use [n1:n2] notation to get a slice from a tuple. - -Solution -tp=(1,2,3,4,5,6,7,8,9,10) -tp1=tp[:5] -tp2=tp[5:] -print tp1 -print tp2 - - -#----------------------------------------# -2.10 - -Question: -Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). - -Hints: - -Use "for" to iterate the tuple -Use tuple() to generate a tuple from a list. - -Solution -tp=(1,2,3,4,5,6,7,8,9,10) -li=list() -for i in tp: - if tp[i]%2==0: - li.append(tp[i]) - -tp2=tuple(li) -print tp2 - - - -#----------------------------------------# -2.14 - -Question: -Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No". - -Hints: - -Use if statement to judge condition. - -Solution -s= raw_input() -if s=="yes" or s=="YES" or s=="Yes": - print "Yes" -else: - print "No" - - - -#----------------------------------------# -3.4 - -Question: -Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10]. - -Hints: - -Use filter() to filter some elements in a list. -Use lambda to define anonymous functions. - -Solution -li = [1,2,3,4,5,6,7,8,9,10] -evenNumbers = filter(lambda x: x%2==0, li) -print evenNumbers - - -#----------------------------------------# -3.4 - -Question: -Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10]. - -Hints: - -Use map() to generate a list. -Use lambda to define anonymous functions. - -Solution -li = [1,2,3,4,5,6,7,8,9,10] -squaredNumbers = map(lambda x: x**2, li) -print squaredNumbers - -#----------------------------------------# -3.5 - -Question: -Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10]. - -Hints: - -Use map() to generate a list. -Use filter() to filter elements of a list. -Use lambda to define anonymous functions. - -Solution -li = [1,2,3,4,5,6,7,8,9,10] -evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li)) -print evenNumbers - - - - -#----------------------------------------# -3.5 - -Question: -Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included). - -Hints: - -Use filter() to filter elements of a list. -Use lambda to define anonymous functions. - -Solution -evenNumbers = filter(lambda x: x%2==0, range(1,21)) -print evenNumbers - - -#----------------------------------------# -3.5 - -Question: -Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included). - -Hints: - -Use map() to generate a list. -Use lambda to define anonymous functions. - -Solution -squaredNumbers = map(lambda x: x**2, range(1,21)) -print squaredNumbers - - - - -#----------------------------------------# -7.2 - -Question: -Define a class named American which has a static method called printNationality. - -Hints: - -Use @staticmethod decorator to define class static method. - -Solution -class American(object): - @staticmethod - def printNationality(): - print "America" - -anAmerican = American() -anAmerican.printNationality() -American.printNationality() - - - - -#----------------------------------------# - -7.2 - -Question: -Define a class named American and its subclass NewYorker. - -Hints: - -Use class Subclass(ParentClass) to define a subclass. - -Solution: - -class American(object): - pass - -class NewYorker(American): - pass - -anAmerican = American() -aNewYorker = NewYorker() -print anAmerican -print aNewYorker - - - - -#----------------------------------------# - - -7.2 - -Question: -Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. - -Hints: - -Use def methodName(self) to define a method. - -Solution: - -class Circle(object): - def __init__(self, r): - self.radius = r - - def area(self): - return self.radius**2*3.14 - -aCircle = Circle(2) -print aCircle.area() - - - - - - -#----------------------------------------# - -7.2 - -Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area. - -Hints: - -Use def methodName(self) to define a method. - -Solution: - -class Rectangle(object): - def __init__(self, l, w): - self.length = l - self.width = w - - def area(self): - return self.length*self.width - -aRectangle = Rectangle(2,10) -print aRectangle.area() - - - - -#----------------------------------------# - -7.2 - -Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default. - -Hints: - -To override a method in super class, we can define a method with the same name in the super class. - -Solution: - -class Shape(object): - def __init__(self): - pass - - def area(self): - return 0 - -class Square(Shape): - def __init__(self, l): - Shape.__init__(self) - self.length = l - - def area(self): - return self.length*self.length - -aSquare= Square(3) -print aSquare.area() - - - - - - - - -#----------------------------------------# - - -Please raise a RuntimeError exception. - -Hints: - -Use raise() to raise an exception. - -Solution: - -raise RuntimeError('something wrong') - - - -#----------------------------------------# -Write a function to compute 5/0 and use try/except to catch the exceptions. - -Hints: - -Use try/except to catch exceptions. - -Solution: - -def throws(): - return 5/0 - -try: - throws() -except ZeroDivisionError: - print "division by zero!" -except Exception, err: - print 'Caught an exception' -finally: - print 'In finally block for cleanup' - - -#----------------------------------------# -Define a custom exception class which takes a string message as attribute. - -Hints: - -To define a custom exception, we need to define a class inherited from Exception. - -Solution: - -class MyError(Exception): - """My own exception class - - Attributes: - msg -- explanation of the error - """ - - def __init__(self, msg): - self.msg = msg - -error = MyError("something wrong") - -#----------------------------------------# -Question: - -Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only. - -Example: -If the following email address is given as input to the program: - -john@google.com - -Then, the output of the program should be: - -john - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: - -Use \w to match letters. - -Solution: -import re -emailAddress = raw_input() -pat2 = "(\w+)@((\w+\.)+(com))" -r2 = re.match(pat2,emailAddress) -print r2.group(1) - - -#----------------------------------------# -Question: - -Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only. - -Example: -If the following email address is given as input to the program: - -john@google.com - -Then, the output of the program should be: - -google - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: - -Use \w to match letters. - -Solution: -import re -emailAddress = raw_input() -pat2 = "(\w+)@(\w+)\.(com)" -r2 = re.match(pat2,emailAddress) -print r2.group(2) - - - - -#----------------------------------------# -Question: - -Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only. - -Example: -If the following words is given as input to the program: - -2 cats and 3 dogs. - -Then, the output of the program should be: - -['2', '3'] - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: - -Use re.findall() to find all substring using regex. - -Solution: -import re -s = raw_input() -print re.findall("\d+",s) - - -#----------------------------------------# -Question: - - -Print a unicode string "hello world". - -Hints: - -Use u'strings' format to define unicode string. - -Solution: - -unicodeString = u"hello world!" -print unicodeString - -#----------------------------------------# -Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8. - -Hints: - -Use unicode() function to convert. - -Solution: - -s = raw_input() -u = unicode( s ,"utf-8") -print u - -#----------------------------------------# -Question: - -Write a special comment to indicate a Python source code file is in unicode. - -Hints: - -Solution: - -# -*- coding: utf-8 -*- - -#----------------------------------------# -Question: - -Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0). - -Example: -If the following n is given as input to the program: - -5 - -Then, the output of the program should be: - -3.55 - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: -Use float() to convert an integer to a float - -Solution: - -n=int(raw_input()) -sum=0.0 -for i in range(1,n+1): - sum += float(float(i)/(i+1)) -print sum - - -#----------------------------------------# -Question: - -Write a program to compute: - -f(n)=f(n-1)+100 when n>0 -and f(0)=1 - -with a given n input by console (n>0). - -Example: -If the following n is given as input to the program: - -5 - -Then, the output of the program should be: - -500 - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: -We can define recursive function in Python. - -Solution: - -def f(n): - if n==0: - return 0 - else: - return f(n-1)+100 - -n=int(raw_input()) -print f(n) - -#----------------------------------------# - -Question: - - -The Fibonacci Sequence is computed based on the following formula: - - -f(n)=0 if n=0 -f(n)=1 if n=1 -f(n)=f(n-1)+f(n-2) if n>1 - -Please write a program to compute the value of f(n) with a given n input by console. - -Example: -If the following n is given as input to the program: - -7 - -Then, the output of the program should be: - -13 - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Hints: -We can define recursive function in Python. - - -Solution: - -def f(n): - if n == 0: return 0 - elif n == 1: return 1 - else: return f(n-1)+f(n-2) - -n=int(raw_input()) -print f(n) - - -#----------------------------------------# - -#----------------------------------------# - -Question: - -The Fibonacci Sequence is computed based on the following formula: - - -f(n)=0 if n=0 -f(n)=1 if n=1 -f(n)=f(n-1)+f(n-2) if n>1 - -Please write a program using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console. - -Example: -If the following n is given as input to the program: - -7 - -Then, the output of the program should be: - -0,1,1,2,3,5,8,13 - - -Hints: -We can define recursive function in Python. -Use list comprehension to generate a list from an existing list. -Use string.join() to join a list of strings. - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: - -def f(n): - if n == 0: return 0 - elif n == 1: return 1 - else: return f(n-1)+f(n-2) - -n=int(raw_input()) -values = [str(f(x)) for x in range(0, n+1)] -print ",".join(values) - - -#----------------------------------------# - -Question: - -Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console. - -Example: -If the following n is given as input to the program: - -10 - -Then, the output of the program should be: - -0,2,4,6,8,10 - -Hints: -Use yield to produce the next value in generator. - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: - -def EvenGenerator(n): - i=0 - while i<=n: - if i%2==0: - yield i - i+=1 - - -n=int(raw_input()) -values = [] -for i in EvenGenerator(n): - values.append(str(i)) - -print ",".join(values) - - -#----------------------------------------# - -Question: - -Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console. - -Example: -If the following n is given as input to the program: - -100 - -Then, the output of the program should be: - -0,35,70 - -Hints: -Use yield to produce the next value in generator. - -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: - -def NumGenerator(n): - for i in range(n+1): - if i%5==0 and i%7==0: - yield i - -n=int(raw_input()) -values = [] -for i in NumGenerator(n): - values.append(str(i)) - -print ",".join(values) - - -#----------------------------------------# - -Question: - - -Please write assert statements to verify that every number in the list [2,4,6,8] is even. - - - -Hints: -Use "assert expression" to make assertion. - - -Solution: - -li = [2,4,6,8] -for i in li: - assert i%2==0 - - -#----------------------------------------# -Question: - -Please write a program which accepts basic mathematic expression from console and print the evaluation result. - -Example: -If the following string is given as input to the program: - -35+3 - -Then, the output of the program should be: - -38 - -Hints: -Use eval() to evaluate an expression. - - -Solution: - -expression = raw_input() -print eval(expression) - - -#----------------------------------------# -Question: - -Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list. - - -Hints: -Use if/elif to deal with conditions. - - -Solution: - -import math -def bin_search(li, element): - bottom = 0 - top = len(li)-1 - index = -1 - while top>=bottom and index==-1: - mid = int(math.floor((top+bottom)/2.0)) - if li[mid]==element: - index = mid - elif li[mid]>element: - top = mid-1 - else: - bottom = mid+1 - - return index - -li=[2,5,7,9,11,17,222] -print bin_search(li,11) -print bin_search(li,12) - - - - -#----------------------------------------# -Question: - -Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list. - - -Hints: -Use if/elif to deal with conditions. - - -Solution: - -import math -def bin_search(li, element): - bottom = 0 - top = len(li)-1 - index = -1 - while top>=bottom and index==-1: - mid = int(math.floor((top+bottom)/2.0)) - if li[mid]==element: - index = mid - elif li[mid]>element: - top = mid-1 - else: - bottom = mid+1 - - return index - -li=[2,5,7,9,11,17,222] -print bin_search(li,11) -print bin_search(li,12) - - - - -#----------------------------------------# -Question: - -Please generate a random float where the value is between 10 and 100 using Python math module. - - - -Hints: -Use random.random() to generate a random float in [0,1]. - - -Solution: - -import random -print random.random()*100 - -#----------------------------------------# -Question: - -Please generate a random float where the value is between 5 and 95 using Python math module. - - - -Hints: -Use random.random() to generate a random float in [0,1]. - - -Solution: - -import random -print random.random()*100-5 - - -#----------------------------------------# -Question: - -Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension. - - - -Hints: -Use random.choice() to a random element from a list. - - -Solution: - -import random -print random.choice([i for i in range(11) if i%2==0]) - - -#----------------------------------------# -Question: - -Please write a program to output a random number, which is divisible by 5 and 7, between 0 and 10 inclusive using random module and list comprehension. - - - -Hints: -Use random.choice() to a random element from a list. - - -Solution: - -import random -print (random.choice([i for i in range(201) if i%5==0 and i%7==0])) - - - -#----------------------------------------# - -Question: - -Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive. - - - -Hints: -Use random.sample() to generate a list of random values. - - -Solution: - -import random -print random.sample(range(100), 5) - -#----------------------------------------# -Question: - -Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive. - - - -Hints: -Use random.sample() to generate a list of random values. - - -Solution: - -import random -print random.sample([i for i in range(100,201) if i%2==0], 5) - - -#----------------------------------------# -Question: - -Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive. - - - -Hints: -Use random.sample() to generate a list of random values. - - -Solution: - -import random -print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5) - -#----------------------------------------# - -Question: - -Please write a program to randomly print a integer number between 7 and 15 inclusive. - - - -Hints: -Use random.randrange() to a random integer in a given range. - - -Solution: - -import random -print random.randrange(7,16) - -#----------------------------------------# - -Question: - -Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!". - - - -Hints: -Use zlib.compress() and zlib.decompress() to compress and decompress a string. - - -Solution: - -import zlib -s = 'hello world!hello world!hello world!hello world!' -t = zlib.compress(s) -print t -print zlib.decompress(t) - -#----------------------------------------# -Question: - -Please write a program to print the running time of execution of "1+1" for 100 times. - - - -Hints: -Use timeit() function to measure the running time. - -Solution: - -from timeit import Timer -t = Timer("for i in range(100):1+1") -print t.timeit() - -#----------------------------------------# -Question: - -Please write a program to shuffle and print the list [3,6,7,8]. - - - -Hints: -Use shuffle() function to shuffle a list. - -Solution: - -from random import shuffle -li = [3,6,7,8] -shuffle(li) -print li - -#----------------------------------------# -Question: - -Please write a program to shuffle and print the list [3,6,7,8]. - - - -Hints: -Use shuffle() function to shuffle a list. - -Solution: - -from random import shuffle -li = [3,6,7,8] -shuffle(li) -print li - - - -#----------------------------------------# -Question: - -Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"]. - -Hints: -Use list[index] notation to get a element from a list. - -Solution: - -subjects=["I", "You"] -verbs=["Play", "Love"] -objects=["Hockey","Football"] -for i in range(len(subjects)): - for j in range(len(verbs)): - for k in range(len(objects)): - sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k]) - print sentence - - -#----------------------------------------# -Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24]. - -Hints: -Use list comprehension to delete a bunch of element from a list. - -Solution: - -li = [5,6,77,45,22,12,24] -li = [x for x in li if x%2!=0] -print li - -#----------------------------------------# -Question: - -By using list comprehension, please write a program to print the list after removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155]. - -Hints: -Use list comprehension to delete a bunch of element from a list. - -Solution: - -li = [12,24,35,70,88,120,155] -li = [x for x in li if x%5!=0 and x%7!=0] -print li - - -#----------------------------------------# -Question: - -By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155]. - -Hints: -Use list comprehension to delete a bunch of element from a list. -Use enumerate() to get (index, value) tuple. - -Solution: - -li = [12,24,35,70,88,120,155] -li = [x for (i,x) in enumerate(li) if i%2!=0] -print li - -#----------------------------------------# - -Question: - -By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0. - -Hints: -Use list comprehension to make an array. - -Solution: - -array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] -print array - -#----------------------------------------# -Question: - -By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155]. - -Hints: -Use list comprehension to delete a bunch of element from a list. -Use enumerate() to get (index, value) tuple. - -Solution: - -li = [12,24,35,70,88,120,155] -li = [x for (i,x) in enumerate(li) if i not in (0,4,5)] -print li - - - -#----------------------------------------# - -Question: - -By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155]. - -Hints: -Use list's remove method to delete a value. - -Solution: - -li = [12,24,35,24,88,120,155] -li = [x for x in li if x!=24] -print li - - -#----------------------------------------# -Question: - -With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists. - -Hints: -Use set() and "&=" to do set intersection operation. - -Solution: - -set1=set([1,3,6,78,35,55]) -set2=set([12,24,35,24,88,120,155]) -set1 &= set2 -li=list(set1) -print li - -#----------------------------------------# - -With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved. - -Hints: -Use set() to store a number of values without duplicate. - -Solution: - -def removeDuplicate( li ): - newli=[] - seen = set() - for item in li: - if item not in seen: - seen.add( item ) - newli.append(item) - - return newli - -li=[12,24,35,24,88,120,155,88,120,155] -print removeDuplicate(li) - - -#----------------------------------------# -Question: - -Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class. - -Hints: -Use Subclass(Parentclass) to define a child class. - -Solution: - -class Person(object): - def getGender( self ): - return "Unknown" - -class Male( Person ): - def getGender( self ): - return "Male" - -class Female( Person ): - def getGender( self ): - return "Female" - -aMale = Male() -aFemale= Female() -print aMale.getGender() -print aFemale.getGender() - - - -#----------------------------------------# -Question: - -Please write a program which count and print the numbers of each character in a string input by console. - -Example: -If the following string is given as input to the program: - -abcdefgabc - -Then, the output of the program should be: - -a,2 -c,2 -b,2 -e,1 -d,1 -g,1 -f,1 - -Hints: -Use dict to store key/value pairs. -Use dict.get() method to lookup a key with default value. - -Solution: - -dic = {} -s=raw_input() -for s in s: - dic[s] = dic.get(s,0)+1 -print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]) - -#----------------------------------------# - -Question: - -Please write a program which accepts a string from console and print it in reverse order. - -Example: -If the following string is given as input to the program: - -rise to vote sir - -Then, the output of the program should be: - -ris etov ot esir - -Hints: -Use list[::-1] to iterate a list in a reverse order. - -Solution: - -s=raw_input() -s = s[::-1] -print s - -#----------------------------------------# - -Question: - -Please write a program which accepts a string from console and print the characters that have even indexes. - -Example: -If the following string is given as input to the program: - -H1e2l3l4o5w6o7r8l9d - -Then, the output of the program should be: - -Helloworld - -Hints: -Use list[::2] to iterate a list by step 2. - -Solution: - -s=raw_input() -s = s[::2] -print s -#----------------------------------------# - - -Question: - -Please write a program which prints all permutations of [1,2,3] - - -Hints: -Use itertools.permutations() to get permutations of list. - -Solution: - -import itertools -print list(itertools.permutations([1,2,3])) - -#----------------------------------------# -Question: - -Write a program to solve a classic ancient Chinese puzzle: -We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have? - -Hint: -Use for loop to iterate all possible solutions. - -Solution: - -def solve(numheads,numlegs): - ns='No solutions!' - for i in range(numheads+1): - j=numheads-i - if 2*i+4*j==numlegs: - return i,j - return ns,ns - -numheads=35 -numlegs=94 -solutions=solve(numheads,numlegs) -print solutions - -#----------------------------------------# - - From ac8b69d4a4de37a480d36aab5b2aac08711f7070 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 12:44:19 +0530 Subject: [PATCH 65/75] Rename 31- 40 python programming exercise( in Python 3x ).ipynb to 31-40 python programming exercise( in Python 3x ).ipynb --- ...ynb => 31-40 python programming exercise( in Python 3x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 31- 40 python programming exercise( in Python 3x ).ipynb => 31-40 python programming exercise( in Python 3x ).ipynb (100%) diff --git a/31- 40 python programming exercise( in Python 3x ).ipynb b/31-40 python programming exercise( in Python 3x ).ipynb similarity index 100% rename from 31- 40 python programming exercise( in Python 3x ).ipynb rename to 31-40 python programming exercise( in Python 3x ).ipynb From cfdc9e01329c504f69cf2527c447d0a5e2c9504c Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 12:44:53 +0530 Subject: [PATCH 66/75] Rename 41 - 50 python programming exercise(in Pyhton 3.x).ipynb to 41-50 python programming exercise(in Pyhton 3.x).ipynb --- ...nb => 41-50 python programming exercise(in Pyhton 3.x).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 41 - 50 python programming exercise(in Pyhton 3.x).ipynb => 41-50 python programming exercise(in Pyhton 3.x).ipynb (99%) diff --git a/41 - 50 python programming exercise(in Pyhton 3.x).ipynb b/41-50 python programming exercise(in Pyhton 3.x).ipynb similarity index 99% rename from 41 - 50 python programming exercise(in Pyhton 3.x).ipynb rename to 41-50 python programming exercise(in Pyhton 3.x).ipynb index a076c826..70c74bfc 100644 --- a/41 - 50 python programming exercise(in Pyhton 3.x).ipynb +++ b/41-50 python programming exercise(in Pyhton 3.x).ipynb @@ -521,4 +521,4 @@ ] } ] -} \ No newline at end of file +} From 67321cff39f53523797b62dbb160dc3e3a9aa27f Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 12:45:26 +0530 Subject: [PATCH 67/75] Rename 51 - 60 python programming exercise( in Python 3x ).ipynb to 51-60 python programming exercise( in Python 3x ).ipynb --- ...b => 51-60 python programming exercise( in Python 3x ).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 51 - 60 python programming exercise( in Python 3x ).ipynb => 51-60 python programming exercise( in Python 3x ).ipynb (99%) diff --git a/51 - 60 python programming exercise( in Python 3x ).ipynb b/51-60 python programming exercise( in Python 3x ).ipynb similarity index 99% rename from 51 - 60 python programming exercise( in Python 3x ).ipynb rename to 51-60 python programming exercise( in Python 3x ).ipynb index b35f7638..d44cc2d8 100644 --- a/51 - 60 python programming exercise( in Python 3x ).ipynb +++ b/51-60 python programming exercise( in Python 3x ).ipynb @@ -562,4 +562,4 @@ ] } ] -} \ No newline at end of file +} From bf16030eb77ce365778d40b9d15cc5ce9e3e8fc7 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:06:13 +0530 Subject: [PATCH 68/75] Rename 31-40 python programming exercise( in Python 3x ).ipynb to 31-40 Python Programming Exercise( in Python 3x ).ipynb --- ...ynb => 31-40 Python Programming Exercise( in Python 3x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 31-40 python programming exercise( in Python 3x ).ipynb => 31-40 Python Programming Exercise( in Python 3x ).ipynb (100%) diff --git a/31-40 python programming exercise( in Python 3x ).ipynb b/31-40 Python Programming Exercise( in Python 3x ).ipynb similarity index 100% rename from 31-40 python programming exercise( in Python 3x ).ipynb rename to 31-40 Python Programming Exercise( in Python 3x ).ipynb From 6f3a2871c985bad12cb8b4f88064c4fab00bbeb7 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:10:08 +0530 Subject: [PATCH 69/75] Added the remaining questions The python programming exercise questions from 61 to 97 are available now. Practice and enjoy the learning ! --- ...Programming Exercise (in Python 3.x).ipynb | 592 ++++++++++++++++++ ...Programming Exercise (in Python 3.x).ipynb | 434 +++++++++++++ ...Programming Exercise (in Python 3.x).ipynb | 475 ++++++++++++++ ...Programming Exercise (in Python 3.x).ipynb | 378 +++++++++++ 4 files changed, 1879 insertions(+) create mode 100644 61-70 Python Programming Exercise (in Python 3.x).ipynb create mode 100644 71-80 Python Programming Exercise (in Python 3.x).ipynb create mode 100644 81-90 Python Programming Exercise (in Python 3.x).ipynb create mode 100644 91-97 Python Programming Exercise (in Python 3.x).ipynb diff --git a/61-70 Python Programming Exercise (in Python 3.x).ipynb b/61-70 Python Programming Exercise (in Python 3.x).ipynb new file mode 100644 index 00000000..038b89ea --- /dev/null +++ b/61-70 Python Programming Exercise (in Python 3.x).ipynb @@ -0,0 +1,592 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "61 - 70 python programming exercise (in Python 3.x).ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "5PPkTgnvAAVe" + }, + "source": [ + "# **Question 61**\n", + "Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.\n", + "\n", + "**Hints:Use unicode() function to convert.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eO0lCFOLAe_2" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9H1oBnIGAgZe", + "outputId": "477996dd-d642-40ab-ca97-d2bea0fa6081", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 214 + } + }, + "source": [ + "s = input()\n", + "u = unicode( s ,'utf-8')\n", + "print(u)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "K\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "NameError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mu\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0municode\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m,\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mu\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'unicode' is not defined" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "55Ogciz2EDAs" + }, + "source": [ + "# **Question 62**\n", + "Write a special comment to indicate a Python source code file is in unicode.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XNCEB4STEK5K" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yz2HIT2JFDRl" + }, + "source": [ + "# -*- coding: utf-8 -*-\n", + "\n", + "#----------------------------------------#" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hll07VCUGVry" + }, + "source": [ + "# **Question 63**\n", + "Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).\n", + "\n", + "Example:\n", + "``` \n", + " Input: 5\n", + " Output: 3.55\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints: Use float() to convert an integer to a float**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PytyZSY_Gvp3" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dFqhnkL_GzhA", + "outputId": "7977c9c8-66d0-4b43-864f-cb2e93da1376", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "n=int(input())\n", + "sum=0.0\n", + "for i in range(1,n+1):\n", + " sum += float(float(i)/(i+1))\n", + "print(sum)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "3.5500000000000003\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7MckCnlmG7d1" + }, + "source": [ + "# **Question 64**\n", + "Write a program to compute:\n", + "\n", + "f(n)=f(n-1)+100 when n>0 and f(0)=1\n", + "\n", + "with a given n input by console (n>0).\n", + "\n", + "Example: \n", + "```\n", + " Input: 5\n", + " Output: 500\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints: We can define recursive function in Python.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_hr66-sOG-r7" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DROOM0X0JdHr", + "outputId": "30192f72-f439-4d2b-a65f-ee74bc3247df", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def f(n):\n", + " if n==0:\n", + " return 0\n", + " else:\n", + " return f(n-1)+100\n", + "\n", + "n=int(input())\n", + "print(f(n))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "500\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OPjc3W28JmHg" + }, + "source": [ + "# **Question 65**\n", + "The Fibonacci Sequence is computed based on the following formula:\n", + "\n", + "f(n)=0 if n=0 f(n)=1 if n=1 f(n)=f(n-1)+f(n-2) if n>1\n", + "\n", + "Please write a program to compute the value of f(n) with a given n input by console.\n", + "\n", + "Example:\n", + "``` \n", + " Input: 7\n", + " Output: 13\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints: We can define recursive function in Python.**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zjypOaoBKBHn" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FUbM5nG1KCbI", + "outputId": "25a7898f-28ff-4a84-e55c-c570e6e8610c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def f(n):\n", + " if n == 0: return 0\n", + " elif n == 1: return 1\n", + " else: return f(n-1)+f(n-2)\n", + "\n", + "n=int(input())\n", + "print(f(n))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "7\n", + "13\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1EyuDmg2QyFx" + }, + "source": [ + "# **Question 66**\n", + "The Fibonacci Sequence is computed based on the following formula:\n", + "\n", + "f(n)=0 if n=0 f(n)=1 if n=1 f(n)=f(n-1)+f(n-2) if n>1\n", + "\n", + "Please write a program using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console.\n", + "\n", + "Example: \n", + "```\n", + " Input: 7\n", + " output: 0,1,1,2,3,5,8,13\n", + "```\n", + "**Hints: We can define recursive function in Python. Use list comprehension to generate a list from an existing list. Use string.join() to join a list of strings.**\n", + "\n", + "In case of input data being supplied to the question, it should be assumed to be a console input." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "P-1Om8WiRHjJ" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZLYSEmBPRKm1", + "outputId": "98c0fc46-fbc9-42d3-98ef-11b27112d96d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def f(n):\n", + " if n == 0: return 0\n", + " elif n == 1: return 1\n", + " else: return f(n-1)+f(n-2)\n", + "\n", + "n=int(input())\n", + "values = [str(f(x)) for x in range(0, n+1)]\n", + "print(\",\".join(values))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "7\n", + "0,1,1,2,3,5,8,13\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z8sR2YUtRO5x" + }, + "source": [ + "# **Question 67**\n", + "Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.\n", + "\n", + "Example: \n", + "```\n", + " Input: 10\n", + " output: 0,2,4,6,8,10\n", + "```\n", + "**Hints: Use yield to produce the next value in generator.**\n", + "\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hy3mKd9VRjGo" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "O2BjsqXzRh7D", + "outputId": "e447a67c-5c76-426a-ccbd-d63e68a529c6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def EvenGenerator(n):\n", + " i=0\n", + " while i<=n:\n", + " if i%2==0:\n", + " yield i\n", + " i+=1\n", + "\n", + "\n", + "n=int(input())\n", + "values = []\n", + "for i in EvenGenerator(n):\n", + " values.append(str(i))\n", + "\n", + "print(\",\".join(values))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "0,2,4,6,8,10\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mTM_z1cxRuPI" + }, + "source": [ + "# **Question 68**\n", + "Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.\n", + "\n", + "Example: \n", + "```\n", + " Input: 100\n", + " Output: 0,35,70\n", + "```\n", + "**Hints: Use yield to produce the next value in generator.**\n", + "\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pHfqRsvFSEAP" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zKxWKV7KSHuB", + "outputId": "2e42f1f3-890b-4a33-def6-522a6387de46", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def NumGenerator(n):\n", + " for i in range(n+1):\n", + " if i%5==0 and i%7==0:\n", + " yield i\n", + "\n", + "n=int(input())\n", + "values = []\n", + "for i in NumGenerator(n):\n", + " values.append(str(i))\n", + "\n", + "print(\",\".join(values))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "100\n", + "0,35,70\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "88AwkOoiSKwA" + }, + "source": [ + "# **Question 69**\n", + "Please write assert statements to verify that every number in the list [2,4,6,8] is even.\n", + "\n", + "**Hints: Use \"assert expression\" to make assertion.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1ZT3f4bgST6H" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9s39AUfBSXe6", + "outputId": "faf218ec-64c2-411e-840c-85003ad26df7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + } + }, + "source": [ + "li = [2,4,6,8]\n", + "for i in li:\n", + " assert i%2==0\n", + " print(\"even\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "even\n", + "even\n", + "even\n", + "even\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BHVwqcyaTC9l" + }, + "source": [ + "# **Question 70**\n", + "Please write a program which accepts basic mathematic expression from console and print the evaluation result.\n", + "\n", + "Example: \n", + "```\n", + " Input: 35+3\n", + " Output: 38\n", + "```\n", + "**Hints: Use eval() to evaluate an expression.**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MSpuDCBTThOt" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TpXByB2jTjnv", + "outputId": "4d047eb6-ef76-4a79-b16b-8ef1ea87b904", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "expression =input()\n", + "print(eval(expression))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "35+3\n", + "38\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/71-80 Python Programming Exercise (in Python 3.x).ipynb b/71-80 Python Programming Exercise (in Python 3.x).ipynb new file mode 100644 index 00000000..8186d16a --- /dev/null +++ b/71-80 Python Programming Exercise (in Python 3.x).ipynb @@ -0,0 +1,434 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "71 - 80 python programming exercise (in Python 3.x).ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "FPjlx8QY0Qhi" + }, + "source": [ + "# **Question 71**\n", + "Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.\n", + "\n", + "**Hints: Use if/elif to deal with conditions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0SlSfkKQ0axS" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A8P0P3z20Ioz", + "outputId": "20ccb9be-0ebf-4184-db3b-e6c7d0010212", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import math\n", + "def bin_search(li, element):\n", + " bottom = 0\n", + " top = len(li)-1\n", + " index = -1\n", + " while top>=bottom and index==-1:\n", + " mid = int(math.floor((top+bottom)/2.0))\n", + " if li[mid]==element:\n", + " index = mid\n", + " elif li[mid]>element:\n", + " top = mid-1\n", + " else:\n", + " bottom = mid+1\n", + "\n", + " return index\n", + "\n", + "li=[2,5,7,9,11,17,222]\n", + "print(bin_search(li,11))\n", + "print(bin_search(li,12))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "4\n", + "-1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KvIg3Kw014cx" + }, + "source": [ + "# **Question 72**\n", + "Please generate a random float where the value is between 10 and 100 using Python math module.\n", + "\n", + "**Hints: Use random.random() to generate a random float in [0,1].**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n0h3G2R60y_k" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zkmsfFnf16EW", + "outputId": "6e54e58c-bccd-46b9-f9f1-2630504614a2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "import random\n", + "print(random.random()*100)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "14.506526550602207\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d_Z7tmGb2vJ_" + }, + "source": [ + "# **Question 73**\n", + "Please generate a random float where the value is between 5 and 95 using Python math module.\n", + "\n", + "**Hints: Use random.random() to generate a random float in [0,1].**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VRb55UYA27yk" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VaVa3j3Y3Yy3" + }, + "source": [ + "import random\n", + "print(random.random()*100-5)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kW5Ivtsa3jV9" + }, + "source": [ + "# **Question 74**\n", + "Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.\n", + "\n", + "**Hints: Use random.choice() to a random element from a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "inw5DqP63pUs" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1382AXe13upT" + }, + "source": [ + "import random\n", + "print(random.choice([i for i in range(11) if i%2==0]))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tvo2By5S3ys8" + }, + "source": [ + "# **Question 75**\n", + "Please write a program to output a random number, which is divisible by 5 and 7, between 0 and 10 inclusive using random module and list comprehension.\n", + "\n", + "**Hints: Use random.choice() to a random element from a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zqPa68o339Gx" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aZPjY1Cy3-kJ" + }, + "source": [ + "import random\n", + "print(random.choice([i for i in range(201) if i%5==0 and i%7==0]))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UtHU2OPC4Bf5" + }, + "source": [ + "# **Question 76**\n", + "Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.\n", + "\n", + "**Hints: Use random.sample() to generate a list of random values.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nc01_e8e4KsN" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XM3RFgK_4MV5", + "outputId": "9297bc33-7deb-40c6-8eda-ed2a10baa6f0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "import random\n", + "print(random.sample(range(100), 5))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[14, 0, 39, 6, 35]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kO_9dbF-4Vua" + }, + "source": [ + "# **Question 77**\n", + "Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.\n", + "\n", + "**Hints: Use random.sample() to generate a list of random values.**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sEBz-43g4dKL" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DntP8_DB4eup", + "outputId": "a1413e7e-9edb-4564-af08-35c493a8405a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "import random\n", + "print(random.sample([i for i in range(100,201) if i%2==0], 5))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[160, 144, 124, 166, 156]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FLYeLlLF4r3K" + }, + "source": [ + "# **Question 78**\n", + "Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.\n", + "\n", + "**Hints: Use random.sample() to generate a list of random values.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MS9aJDwE4xmR" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "be5jlbgq40B8" + }, + "source": [ + "import random\n", + "print(random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O6oo-esg435R" + }, + "source": [ + "# **Question 79**\n", + "Please write a program to randomly print a integer number between 7 and 15 inclusive.\n", + "\n", + "**Hints: Use random.randrange() to a random integer in a given range.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "w3KIbfis49BD" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZFLW1kHW5AXD" + }, + "source": [ + "import random\n", + "print(random.randrange(7,16))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nUH_0p-n5E9g" + }, + "source": [ + "# **Question 80**\n", + "Please write a program to compress and decompress the string \"hello world!hello world!hello world!hello world!\".\n", + "\n", + "**Hints: Use zlib.compress() and zlib.decompress() to compress and decompress a string.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6lDBcZve5I73" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rWz7FhzP5KKP", + "outputId": "ff742339-fd62-4091-b727-7bc20296626c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import zlib\n", + "s = b'hello world!hello world!hello world!hello world!'\n", + "t = zlib.compress(s)\n", + "print(t)\n", + "print(zlib.decompress(t))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "b'x\\x9c\\xcbH\\xcd\\xc9\\xc9W(\\xcf/\\xcaIQ\\xcc \\x82\\r\\x00\\xbd[\\x11\\xf5'\n", + "b'hello world!hello world!hello world!hello world!'\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/81-90 Python Programming Exercise (in Python 3.x).ipynb b/81-90 Python Programming Exercise (in Python 3.x).ipynb new file mode 100644 index 00000000..bc9f1f3b --- /dev/null +++ b/81-90 Python Programming Exercise (in Python 3.x).ipynb @@ -0,0 +1,475 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "81 - 90 Python programming exercises ( in Python 3.x).ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "XYtRUL_Ifz5Z" + }, + "source": [ + "# **Question 81**\n", + "Please write a program to print the running time of execution of \"1+1\" for 100 times.\n", + "\n", + "**Hints: Use timeit() function to measure the running time.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S5l1uLz9gFSS" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IgkL3YPBfFVK" + }, + "source": [ + "from timeit import Timer\n", + "t = Timer(\"for i in range(100):1+1\")\n", + "print(t.timeit())" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SOSHIbZSgU4F" + }, + "source": [ + "# **Question 82**\n", + "Please write a program to shuffle and print the list [3,6,7,8].\n", + "\n", + "**Hints: Use shuffle() function to shuffle a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mk3u51HYgfIr" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JASniDWqgkHx" + }, + "source": [ + "from random import shuffle\n", + "li = [3,6,7,8]\n", + "shuffle(li)\n", + "print(li)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "orN1cVckgrcu" + }, + "source": [ + "# **Question 83**\n", + "Please write a program to generate all sentences where subject is in [\"I\", \"You\"] and verb is in [\"Play\", \"Love\"] and the object is in [\"Hockey\",\"Football\"].\n", + "\n", + "**Hints: Use list[index] notation to get a element from a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RZre6CRrg00A" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PpVuQntmg2ee", + "outputId": "10d5033f-1c21-4768-8475-463834c5e97d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + } + }, + "source": [ + "subjects=[\"I\", \"You\"]\n", + "verbs=[\"Play\", \"Love\"]\n", + "objects=[\"Hockey\",\"Football\"]\n", + "for i in range(len(subjects)):\n", + " for j in range(len(verbs)):\n", + " for k in range(len(objects)):\n", + " sentence = \"%s %s %s.\" % (subjects[i], verbs[j], objects[k])\n", + " print(sentence)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "I Play Hockey.\n", + "I Play Football.\n", + "I Love Hockey.\n", + "I Love Football.\n", + "You Play Hockey.\n", + "You Play Football.\n", + "You Love Hockey.\n", + "You Love Football.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VWDh7QYXiDIb" + }, + "source": [ + "# **Question 84**\n", + "Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24].\n", + "\n", + "**Hints: Use list comprehension to delete a bunch of element from a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aHBZSKKUiU3h" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yxnJXyHHiaaV", + "outputId": "3d0a4769-b5f7-49a0-c76a-ffaeca86960f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [5,6,77,45,22,12,24]\n", + "li = [x for x in li if x%2!=0]\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[5, 77, 45]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E2CRY5rEip0R" + }, + "source": [ + "# **Question 85**\n", + "By using list comprehension, please write a program to print the list after removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].\n", + "\n", + "**Hints: Use list comprehension to delete a bunch of element from a list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9EAPchjii6Yl" + }, + "source": [ + "# **Soltuion:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bn3UE3VWi9Sg", + "outputId": "9aa22df3-77d6-435f-e607-2b9559b1bbdc", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [12,24,35,70,88,120,155]\n", + "li = [x for x in li if x%5!=0 and x%7!=0]\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 24, 88]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dxBZhPK1jE1S" + }, + "source": [ + "# **Question 86**\n", + "By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].\n", + "\n", + "**Hints: Use list comprehension to delete a bunch of element from a list. Use enumerate() to get (index, value) tuple.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "af6BUbQkjOhb" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wFIigS34jQX_", + "outputId": "1e630f4a-3d20-40e0-d964-37a3829bd841", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [12,24,35,70,88,120,155]\n", + "li = [x for (i,x) in enumerate(li) if i%2!=0]\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[24, 70, 120]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d71Csg6Zjm-I" + }, + "source": [ + "# **Question 87**\n", + "By using list comprehension, please write a program to generate a 3,5,8 3D array whose each element is 0.\n", + "\n", + "**Hints: Use list comprehension to make an array.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ENYVXhLsjusF" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gjkzZcaSjwPK", + "outputId": "dce056ed-e09d-44b3-fd64-51e68c0e210b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]\n", + "print(array)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "elGto-vjXt2l" + }, + "source": [ + "# **Question 88**\n", + "By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].\n", + "\n", + "**Hints: Use list comprehension to delete a bunch of element from a list. Use enumerate() to get (index, value) tuple.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_blzhiGuYJyi" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-l13we4tYOJe", + "outputId": "4cb7370b-b9af-4863-90d3-8fb339dde04c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [12,24,35,70,88,120,155]\n", + "li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[24, 35, 70, 155]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uL-aR1xPYaTs" + }, + "source": [ + "# **Question 89**\n", + "By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].\n", + "\n", + "**Hints: Use list's remove method to delete a value.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uXWfaqmlYhEO" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2QCvgXHeYkR5", + "outputId": "4cdbda5a-649b-49c2-8467-1bff1af12110", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "li = [12,24,35,24,88,120,155]\n", + "li = [x for x in li if x!=24]\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 35, 88, 120, 155]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7IZtgrqEYo5C" + }, + "source": [ + "# **Question 90**\n", + "With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.\n", + "\n", + "**Hints: Use set() and \"&=\" to do set intersection operation.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rJygUSz4Zciv" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "W5D4BjPBZf9U", + "outputId": "8f37f456-dd15-4059-dd06-62eec0a57551", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "set1=set([1,3,6,78,35,55])\n", + "set2=set([12,24,35,24,88,120,155])\n", + "set1 &= set2\n", + "li=list(set1)\n", + "print(li)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[35]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/91-97 Python Programming Exercise (in Python 3.x).ipynb b/91-97 Python Programming Exercise (in Python 3.x).ipynb new file mode 100644 index 00000000..36916766 --- /dev/null +++ b/91-97 Python Programming Exercise (in Python 3.x).ipynb @@ -0,0 +1,378 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "91 - 97 python programming exercise( in Python 3x ).ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "xL3VHbHey50o" + }, + "source": [ + "# **Question 91**\n", + "With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.\n", + "\n", + "**Hints: Use set() to store a number of values without duplicate.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1V_O1rbwzeR4" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BtLgWhu7zh4_", + "outputId": "7cb6062b-ba6f-4cd9-ed2b-d68499ed4eb8", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "def removeDuplicate( li ):\n", + " newli=[]\n", + " seen = set()\n", + " for item in li:\n", + " if item not in seen:\n", + " seen.add( item )\n", + " newli.append(item)\n", + "\n", + " return newli\n", + "\n", + "li=[12,24,35,24,88,120,155,88,120,155]\n", + "print(removeDuplicate(li))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 24, 35, 88, 120, 155]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0W71XnaLzmlf" + }, + "source": [ + "# **Question 92**\n", + "Define a class Person and its two child classes: Male and Female. All classes have a method \"getGender\" which can print \"Male\" for Male class and \"Female\" for Female class.\n", + "\n", + "**Hints: Use Subclass(Parentclass) to define a child class.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jCijO-Ztzwov" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yWh34PCpz0IK", + "outputId": "15dab1c6-ba8f-45d8-b2cc-7b5bb0546741", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "class Person(object):\n", + " def getGender( self ):\n", + " return \"Unknown\"\n", + "\n", + "class Male( Person ):\n", + " def getGender( self ):\n", + " return \"Male\"\n", + "\n", + "class Female( Person ):\n", + " def getGender( self ):\n", + " return \"Female\"\n", + "\n", + "aMale = Male()\n", + "aFemale= Female()\n", + "print(aMale.getGender())\n", + "print(aFemale.getGender())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Male\n", + "Female\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PvHydFUQz5qg" + }, + "source": [ + "# **Question 93**\n", + "Please write a program which count and print the numbers of each character in a string input by console.\n", + "\n", + "Example: \n", + "```\n", + " input: abcdefgabc\n", + " output: a,2 c,2 b,2 e,1 d,1 g,1 f,1\n", + "```\n", + "**Hints: Use dict to store key/value pairs. Use dict.get() method to lookup a key with default value.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Oo9CGj-s0Q18" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ctSRjKOW0TND", + "outputId": "d42e7d0e-4e45-49d9-90dd-324b7849ce88", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "dic = {}\n", + "s=input()\n", + "for s in s:\n", + " dic[s] = dic.get(s,0)+1\n", + "print('\\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "abcdefgabc\n", + "a,2\n", + "b,2\n", + "c,2\n", + "d,1\n", + "e,1\n", + "f,1\n", + "g,1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "quPeVXQP0c6Z" + }, + "source": [ + "# **Question 94**\n", + "Please write a program which accepts a string from console and print it in reverse order.\n", + "\n", + "Example:\n", + "```\n", + " input: rise to vote sir\n", + " output: ris etov ot esir\n", + "```\n", + "**Hints: Use list[::-1] to iterate a list in a reverse order.**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e9pHmyb702P4", + "outputId": "95a8c4b6-bfcf-439c-e576-317825973af9", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "s=input()\n", + "s = s[::-1]\n", + "print(s)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "rise to vote sir\n", + "ris etov ot esir\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z-V20iCX08z_" + }, + "source": [ + "# **Question 95**\n", + "Please write a program which accepts a string from console and print the characters that have even indexes.\n", + "\n", + "Example: \n", + "```\n", + " Input : H1e2l3l4o5w6o7r8l9d\n", + " Output : Helloworld\n", + "```\n", + "**Hints: Use list[::2] to iterate a list by step 2.**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4yuc02OR1TNe", + "outputId": "a0c0f601-2977-4756-e2dc-d16dbdafe764", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "s=input()\n", + "s = s[::2]\n", + "print(s)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "H1e2l3l4o5w6o7r8l9d\n", + "Helloworld\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ElNXsUwy1a7Q" + }, + "source": [ + "# **Question 96**\n", + "Please write a program which prints all permutations of [1,2,3]\n", + "\n", + "**Hints: Use itertools.permutations() to get permutations of list.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fCLMDepk1kFl" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HLVeiDog1mDn", + "outputId": "1c873bfc-2958-4e0d-98d5-e073a26f7831", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "import itertools\n", + "print(list(itertools.permutations([1,2,3])))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "19xpFIvs1pzr" + }, + "source": [ + "# **Question 97**\n", + "Write a program to solve a classic ancient Chinese puzzle: We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?\n", + "\n", + "**Hint: Use for loop to iterate all possible solutions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cKjD49db1yGa" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6rG3dkbH10Ye", + "outputId": "67b88a85-9dda-42ed-f90e-fa026883bb55", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "def solve(numheads,numlegs):\n", + " ns='No solutions!'\n", + " for i in range(numheads+1):\n", + " j=numheads-i\n", + " if 2*i+4*j==numlegs:\n", + " return i,j\n", + " return ns,ns\n", + "\n", + "numheads=35\n", + "numlegs=94\n", + "solutions=solve(numheads,numlegs)\n", + "print(solutions)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(23, 12)\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From dc12ae45cb6161c706b16a2e8047e2f8dcfcdcea Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:10:56 +0530 Subject: [PATCH 70/75] Rename 51-60 python programming exercise( in Python 3x ).ipynb to 51-60 Python Programming Exercise ( in Python 3.x ).ipynb --- ...b => 51-60 Python Programming Exercise ( in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 51-60 python programming exercise( in Python 3x ).ipynb => 51-60 Python Programming Exercise ( in Python 3.x ).ipynb (100%) diff --git a/51-60 python programming exercise( in Python 3x ).ipynb b/51-60 Python Programming Exercise ( in Python 3.x ).ipynb similarity index 100% rename from 51-60 python programming exercise( in Python 3x ).ipynb rename to 51-60 Python Programming Exercise ( in Python 3.x ).ipynb From 83f21efac5eeb327e60944942dc9edb06b78a360 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:11:55 +0530 Subject: [PATCH 71/75] Rename 41-50 python programming exercise(in Pyhton 3.x).ipynb to 41-50 Python Programming Exercise (in Python 3.x ).ipynb --- ...nb => 41-50 Python Programming Exercise (in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 41-50 python programming exercise(in Pyhton 3.x).ipynb => 41-50 Python Programming Exercise (in Python 3.x ).ipynb (100%) diff --git a/41-50 python programming exercise(in Pyhton 3.x).ipynb b/41-50 Python Programming Exercise (in Python 3.x ).ipynb similarity index 100% rename from 41-50 python programming exercise(in Pyhton 3.x).ipynb rename to 41-50 Python Programming Exercise (in Python 3.x ).ipynb From 4f0f60fb42f74451a1f2cb6e2e7e301ca49b1fd1 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:12:52 +0530 Subject: [PATCH 72/75] Rename 21-30 Python challenging programming exercises(in Python 3x).ipynb to 21-30 Python Programming Exercises ( in Python 3.x ).ipynb --- ...> 21-30 Python Programming Exercises ( in Python 3.x ).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 21-30 Python challenging programming exercises(in Python 3x).ipynb => 21-30 Python Programming Exercises ( in Python 3.x ).ipynb (99%) diff --git a/21-30 Python challenging programming exercises(in Python 3x).ipynb b/21-30 Python Programming Exercises ( in Python 3.x ).ipynb similarity index 99% rename from 21-30 Python challenging programming exercises(in Python 3x).ipynb rename to 21-30 Python Programming Exercises ( in Python 3.x ).ipynb index f3fbeb52..a96530c5 100644 --- a/21-30 Python challenging programming exercises(in Python 3x).ipynb +++ b/21-30 Python Programming Exercises ( in Python 3.x ).ipynb @@ -657,4 +657,4 @@ ] } ] -} \ No newline at end of file +} From 18a0576a38f19cfb9d6ccacdd96363dc8d727aca Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:13:36 +0530 Subject: [PATCH 73/75] Rename 21-30 Python Programming Exercises ( in Python 3.x ).ipynb to 21-30 Python Programming Exercise ( in Python 3.x ).ipynb --- ...b => 21-30 Python Programming Exercise ( in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 21-30 Python Programming Exercises ( in Python 3.x ).ipynb => 21-30 Python Programming Exercise ( in Python 3.x ).ipynb (100%) diff --git a/21-30 Python Programming Exercises ( in Python 3.x ).ipynb b/21-30 Python Programming Exercise ( in Python 3.x ).ipynb similarity index 100% rename from 21-30 Python Programming Exercises ( in Python 3.x ).ipynb rename to 21-30 Python Programming Exercise ( in Python 3.x ).ipynb From f13f11d5c164a5e0f7ae9006853b29314f444dd6 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:14:12 +0530 Subject: [PATCH 74/75] Rename 11-20 Python challenging programming exercises(in Python 3x).ipynb to 11-20 Python Programming Exercise ( in Python 3.x ).ipynb --- ...b => 11-20 Python Programming Exercise ( in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 11-20 Python challenging programming exercises(in Python 3x).ipynb => 11-20 Python Programming Exercise ( in Python 3.x ).ipynb (100%) diff --git a/11-20 Python challenging programming exercises(in Python 3x).ipynb b/11-20 Python Programming Exercise ( in Python 3.x ).ipynb similarity index 100% rename from 11-20 Python challenging programming exercises(in Python 3x).ipynb rename to 11-20 Python Programming Exercise ( in Python 3.x ).ipynb From 83f033c96ec5eae9d74db00c3ba7789d2e4a6587 Mon Sep 17 00:00:00 2001 From: Ashwani Kumar Date: Wed, 16 Dec 2020 13:14:56 +0530 Subject: [PATCH 75/75] Rename 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb to 1-10 Python Programming Exercise ( in Python 3.x ).ipynb --- ...nb => 1-10 Python Programming Exercise ( in Python 3.x ).ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb => 1-10 Python Programming Exercise ( in Python 3.x ).ipynb (100%) diff --git a/1-10 Python challenging programming exercises ( in Python 3.x ).ipynb b/1-10 Python Programming Exercise ( in Python 3.x ).ipynb similarity index 100% rename from 1-10 Python challenging programming exercises ( in Python 3.x ).ipynb rename to 1-10 Python Programming Exercise ( in Python 3.x ).ipynb