├── 1.Introduction to Python
├── Arithmetic Progression.ipynb
├── Find X raised to power N.ipynb
├── Find average Marks.ipynb
└── Rectangular Area.ipynb
├── 10.Strings
├── Check Palindrome.ipynb
├── Check Permutation.ipynb
├── Compress the String.ipynb
├── Highest Occurring Character.ipynb
├── Remove Consecutive Duplicates.ipynb
├── Remove character.ipynb
└── Reverse Each Word.ipynb
├── 2.Conditionals and Loops
├── Calculator.ipynb
├── Check number.ipynb
├── Fahrenheit to Celsius.ipynb
├── Nth Fibonacci number.ipynb
├── Palindrome number.ipynb
├── Reverse of a number.ipynb
├── Sum of Even Numbers.ipynb
├── Sum of even & odd.ipynb
└── Sum of n numbers.ipynb
├── 2048 Game.ipynb
├── 3. Pattern 1
├── Alpha Pattern.ipynb
├── Code Character Pattern.ipynb
├── Code Reverse Number Pattern.ipynb
├── Code Square Pattern.ipynb
├── Code Triangle Number Pattern.ipynb
├── Code Triangular Star Pattern.ipynb
├── Code Interesting Alphabets.ipynb
├── Number Pattern 1.ipynb
├── Number Pattern 2.ipynb
├── Number Pattern 3.ipynb
└── Number Pattern.ipynb
├── 4.Pattern 2
├── Arrow pattern.ipynb
├── Code Diamond of stars.ipynb
├── Code Mirror Number Pattern.ipynb
├── Code Star Pattern.ipynb
├── Code Triangle of Numbers.ipynb
├── Code Inverted Number Pattern.ipynb
├── Number Pattern.ipynb
├── Pyramid Number Pattern.ipynb
└── Zeros and Stars Pattern.ipynb
├── 5.More on Loops
├── Binary Pattern.ipynb
├── Diamond of Stars.ipynb
├── Print Number Pyramid.ipynb
├── Print the pattern.ipynb
└── Rectangular numbers.ipynb
├── 6.Functions
├── Check Armstrong.ipynb
├── Fahrenheit to Celsius Function.ipynb
├── Fibonacci Member.ipynb
└── Palindrome number.ipynb
├── 7.Arrays & Lists
├── Array Intersection.ipynb
├── Array Sum.ipynb
├── Find Duplicate.ipynb
├── Pair Sum.ipynb
├── Sort 0 1.ipynb
├── Swap Alternate.ipynb
└── Triplet Sum.ipynb
├── 8.Searching & Sorting
├── Check Array Rotation.ipynb
├── Code Binary Search.ipynb
├── Code Bubble Sort.ipynb
├── Code Insertion Sort.ipynb
├── Code Merge Two Sorted Arrays.ipynb
├── Push Zeros to end.ipynb
├── Rotate array.ipynb
├── Second Largest in array.ipynb
├── Sort 0 1 2.ipynb
└── Sum of Two Arrays.ipynb
├── 9.Two Dimensional Lists
├── Largest Row or Column.ipynb
├── Row Wise Sum.ipynb
├── Spiral Print.ipynb
└── Wave Print.ipynb
└── README.md
/1.Introduction to Python/Arithmetic Progression.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Arithmetic Progression\n",
8 | "You are given first three entries of an arithmetic progression. You have to calculate the common difference and print it.
\n",
9 | "
\n",
10 | "Input format:
\n",
11 | "The first line of input contains an integer a (1 <= a <= 100)
\n",
12 | "The second line of input contains an integer b (1 <= b <= 100)
\n",
13 | "The third line of input contains an integer c (1 <= c <= 100)
\n",
14 | "
\n",
15 | "Constraints:
\n",
16 | "Time Limit: 1 second
\n",
17 | "
\n",
18 | "Output format:
\n",
19 | "The first and only line of output contains the result.
\n",
20 | "
\n",
21 | "Sample Input:
\n",
22 | "1
\n",
23 | "3
\n",
24 | "5
\n",
25 | "Sample Output:
\n",
26 | "2"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "a=int(input())\n",
36 | "b=int(input())\n",
37 | "c=int(input())\n",
38 | "print(b-a)"
39 | ]
40 | }
41 | ],
42 | "metadata": {
43 | "kernelspec": {
44 | "display_name": "Python 3",
45 | "language": "python",
46 | "name": "python3"
47 | },
48 | "language_info": {
49 | "codemirror_mode": {
50 | "name": "ipython",
51 | "version": 3
52 | },
53 | "file_extension": ".py",
54 | "mimetype": "text/x-python",
55 | "name": "python",
56 | "nbconvert_exporter": "python",
57 | "pygments_lexer": "ipython3",
58 | "version": "3.8.6"
59 | }
60 | },
61 | "nbformat": 4,
62 | "nbformat_minor": 4
63 | }
64 |
--------------------------------------------------------------------------------
/1.Introduction to Python/Find X raised to power N.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Find X raised to power N\n",
8 | "You are given two integers: X and N. You have to calculate X raised to power N and print it.
\n",
9 | "
\n",
10 | "Input format:
\n",
11 | "The first line of input contains an integer X (1 <= X <= 100)
\n",
12 | "The second line of input contains an integer N (1 <= N <= 10)
\n",
13 | "
\n",
14 | "Constraints:
\n",
15 | "Time Limit: 1 second
\n",
16 | "
\n",
17 | "Output format:
\n",
18 | "The first and only line of output contains the result.
\n",
19 | "
\n",
20 | "Sample Input:
\n",
21 | "10
\n",
22 | "4
\n",
23 | "Sample Output:
\n",
24 | "10000"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "X=int(input())\n",
34 | "N=int(input())\n",
35 | "\n",
36 | "print(X**N)"
37 | ]
38 | }
39 | ],
40 | "metadata": {
41 | "kernelspec": {
42 | "display_name": "Python 3",
43 | "language": "python",
44 | "name": "python3"
45 | },
46 | "language_info": {
47 | "codemirror_mode": {
48 | "name": "ipython",
49 | "version": 3
50 | },
51 | "file_extension": ".py",
52 | "mimetype": "text/x-python",
53 | "name": "python",
54 | "nbconvert_exporter": "python",
55 | "pygments_lexer": "ipython3",
56 | "version": "3.8.6"
57 | }
58 | },
59 | "nbformat": 4,
60 | "nbformat_minor": 4
61 | }
62 |
--------------------------------------------------------------------------------
/1.Introduction to Python/Find average Marks.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Find average Marks\n",
8 | "Write a program to input marks of three tests of a student (all integers). Then calculate and print the average of all test marks.
\n",
9 | "
\n",
10 | "Input format :
\n",
11 | "3 Test marks (in different lines)
\n",
12 | "Output format :
\n",
13 | "Average
\n",
14 | "
\n",
15 | "Sample Input 1 :
\n",
16 | "3
\n",
17 | "4
\n",
18 | "6
\n",
19 | "Sample Output 1 :
\n",
20 | "4.333333333333333
\n",
21 | "
\n",
22 | "Sample Input 2 :
\n",
23 | "5
\n",
24 | "10
\n",
25 | "5
\n",
26 | "Sample Output 2 :
\n",
27 | "6.666666666666667"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "sub_1=int(input(\"\"))\n",
37 | "sub_2=int(input(\"\"))\n",
38 | "sub_3=int(input(\"\"))\n",
39 | "Average_of_3_subjects=(sub_1+sub_2+sub_3)\n",
40 | "print(Average_of_3_subjects/3)"
41 | ]
42 | }
43 | ],
44 | "metadata": {
45 | "kernelspec": {
46 | "display_name": "Python 3",
47 | "language": "python",
48 | "name": "python3"
49 | },
50 | "language_info": {
51 | "codemirror_mode": {
52 | "name": "ipython",
53 | "version": 3
54 | },
55 | "file_extension": ".py",
56 | "mimetype": "text/x-python",
57 | "name": "python",
58 | "nbconvert_exporter": "python",
59 | "pygments_lexer": "ipython3",
60 | "version": "3.8.6"
61 | }
62 | },
63 | "nbformat": 4,
64 | "nbformat_minor": 4
65 | }
66 |
--------------------------------------------------------------------------------
/1.Introduction to Python/Rectangular Area.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Rectangular Area\n",
8 | "You are given a rectangle in a plane. The corner coordinates of this rectangle is provided to you. You have to print the amount of area of the plane covered by this rectangles.
\n",
9 | "
\n",
10 | "The end coordinates are provided as four integral values: x1, y1, x2, y2. It is given that x1 < x2 and y1 < y2.
\n",
11 | "
\n",
12 | "Input format:
\n",
13 | "The first line of input contains an integer x1 (1 <= x1 <= 10)
\n",
14 | "The second line of input contains an integer y1 (1 <= y1 <= 10)
\n",
15 | "The third line of input contains an integer x2 (1 <= x2 <= 10)
\n",
16 | "The fourth line of input contains an integer y2 (1 <= y2 <= 10)
\n",
17 | "
\n",
18 | "Constraints:
\n",
19 | "Time Limit: 1 second
\n",
20 | "
\n",
21 | "Output format:
\n",
22 | "The first and only line of output contains the result.
\n",
23 | "
\n",
24 | "Sample Input:
\n",
25 | "1
\n",
26 | "1
\n",
27 | "3
\n",
28 | "3
\n",
29 | "Sample Output:
\n",
30 | "4"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": null,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "x1=int(input())\n",
40 | "y1=int(input())\n",
41 | "x2=int(input())\n",
42 | "y2=int(input())\n",
43 | "print((x2-x1)*(y2 - y1))"
44 | ]
45 | }
46 | ],
47 | "metadata": {
48 | "kernelspec": {
49 | "display_name": "Python 3",
50 | "language": "python",
51 | "name": "python3"
52 | },
53 | "language_info": {
54 | "codemirror_mode": {
55 | "name": "ipython",
56 | "version": 3
57 | },
58 | "file_extension": ".py",
59 | "mimetype": "text/x-python",
60 | "name": "python",
61 | "nbconvert_exporter": "python",
62 | "pygments_lexer": "ipython3",
63 | "version": "3.8.6"
64 | }
65 | },
66 | "nbformat": 4,
67 | "nbformat_minor": 4
68 | }
69 |
--------------------------------------------------------------------------------
/10.Strings/Check Palindrome.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Check Palindrome\n",
8 | "Given a String s, check it its palindrome. Return true if string is palindrome, else return false.
\n",
9 | "Palindrome strings are those, where string s and its reverse is exactly same.
\n",
10 | "
\n",
11 | "Input Format :
\n",
12 | " String S
\n",
13 | "
\n",
14 | "Output Format :
\n",
15 | "\"true\" if S is palindrome, else \"false\"
\n",
16 | "
\n",
17 | "Constraints :
\n",
18 | "0 <= |S| <= 10^7
\n",
19 | "where |S| represents the length of string, S.
\n",
20 | "
\n",
21 | "Sample Input 1 :
\n",
22 | "abcdcba
\n",
23 | "Sample Output 1 :
\n",
24 | "true
\n",
25 | "Sample Input 1 :
\n",
26 | "abcd
\n",
27 | "Sample Output 1 :
\n",
28 | "false"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "str=input()\n",
38 | "if str==str[::-1]:\n",
39 | " print(\"true\")\n",
40 | "else:\n",
41 | " print(\"false\")"
42 | ]
43 | }
44 | ],
45 | "metadata": {
46 | "kernelspec": {
47 | "display_name": "Python 3",
48 | "language": "python",
49 | "name": "python3"
50 | },
51 | "language_info": {
52 | "codemirror_mode": {
53 | "name": "ipython",
54 | "version": 3
55 | },
56 | "file_extension": ".py",
57 | "mimetype": "text/x-python",
58 | "name": "python",
59 | "nbconvert_exporter": "python",
60 | "pygments_lexer": "ipython3",
61 | "version": "3.8.6"
62 | }
63 | },
64 | "nbformat": 4,
65 | "nbformat_minor": 4
66 | }
67 |
--------------------------------------------------------------------------------
/10.Strings/Check Permutation.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Check Permutation\n",
8 | "Given two strings, S and T, check if they are permutations of each other. Return true or false.
\n",
9 | "Permutation means - length of both the strings should same and should contain same set of characters. Order of characters doesn't matter.
\n",
10 | "Note : Input strings contain only lowercase english alphabets.
\n",
11 | "
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "Line 1 : String 1
\n",
15 | "Line 2 : String 2
\n",
16 | "
\n",
17 | "Output format :
\n",
18 | "'true' or 'false'
\n",
19 | "
\n",
20 | "Constraints :
\n",
21 | "0 <= |S| <= 10^7
\n",
22 | "0 <= |T| <= 10^7
\n",
23 | "where |S| represents the length of string, S.
\n",
24 | "
\n",
25 | "Sample Input 1 :
\n",
26 | "abcde
\n",
27 | "baedc
\n",
28 | "Sample Output 1 :
\n",
29 | "true
\n",
30 | "Sample Input 2 :
\n",
31 | "abc
\n",
32 | "cbd
\n",
33 | "Sample Output 2 :
\n",
34 | "false"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "s=input()\n",
44 | "d=input()\n",
45 | "if sorted(d)==sorted(s):\n",
46 | " print(\"true\")\n",
47 | "else:\n",
48 | " print(\"false\")"
49 | ]
50 | }
51 | ],
52 | "metadata": {
53 | "kernelspec": {
54 | "display_name": "Python 3",
55 | "language": "python",
56 | "name": "python3"
57 | },
58 | "language_info": {
59 | "codemirror_mode": {
60 | "name": "ipython",
61 | "version": 3
62 | },
63 | "file_extension": ".py",
64 | "mimetype": "text/x-python",
65 | "name": "python",
66 | "nbconvert_exporter": "python",
67 | "pygments_lexer": "ipython3",
68 | "version": "3.8.6"
69 | }
70 | },
71 | "nbformat": 4,
72 | "nbformat_minor": 4
73 | }
74 |
--------------------------------------------------------------------------------
/10.Strings/Compress the String.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Compress the String\n",
8 | "Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.
\n",
9 | "
\n",
10 | "Exmple:
\n",
11 | "If a String has 'x' repeated 5 times, replace this \"xxxxx\" with \"x5\".
\n",
12 | "
\n",
13 | "The string is compressed only when the repeated character count is more than 1.
\n",
14 | "
\n",
15 | "Note :
\n",
16 | "Consecutive count of every character in the input string is less than equal to 9.
\n",
17 | "
\n",
18 | "
\n",
19 | "Input Format :
\n",
20 | "The first and the only line of input contains a string(no spaces in between).
\n",
21 | "
\n",
22 | "Output Format :
\n",
23 | "The only line of output print the compressed string.
\n",
24 | "
\n",
25 | "Note:
\n",
26 | "Return the compressed string and hence, no need to print.
\n",
27 | "
\n",
28 | "Constraints :
\n",
29 | "0 <= |S| <= 10^7
\n",
30 | "Where |S| represents the length of string, S.
\n",
31 | "
\n",
32 | "Time Limit: 1sec
\n",
33 | "Sample Input 1 :
\n",
34 | "aaabbccdsa
\n",
35 | "Sample Output 1 :
\n",
36 | "a3b2c2dsa
\n",
37 | "Sample Input 2 :
\n",
38 | "aaabbcddeeeee
\n",
39 | "Sample Output 2 :
\n",
40 | "a3b2cd2e5"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": null,
46 | "metadata": {},
47 | "outputs": [],
48 | "source": [
49 | "def ab(a):\n",
50 | " i=0\n",
51 | " x=''\n",
52 | " while(i\n",
9 | "If there are 2 characters in the input string with same frequency, return the character which comes first.
\n",
10 | "
\n",
11 | "Note : Assume all the characters in the given string are lowercase.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "String S
\n",
15 | "
\n",
16 | "Output format :
\n",
17 | "Highest occurring character
\n",
18 | "
\n",
19 | "Constraints :
\n",
20 | "0 <= |S| <= 10^7
\n",
21 | "where |S| represents the length of string, S.
\n",
22 | "
\n",
23 | "Sample Input 1:
\n",
24 | "abdefgbabfba
\n",
25 | "Sample Output 1:
\n",
26 | "b
\n",
27 | "Sample Input 2:
\n",
28 | "xy
\n",
29 | "Sample Output 2:
\n",
30 | "x"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": null,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "myList=input()\n",
40 | "newlist_d=[]\n",
41 | "maxalpha=\"\"\n",
42 | "maxno=0\n",
43 | "\n",
44 | "#Give list of all distinct words\n",
45 | "for i in (myList):\n",
46 | " if i not in newlist_d:\n",
47 | " newlist_d.append(i)\n",
48 | "#now let count\n",
49 | "for ele in newlist_d:\n",
50 | " count = 0\n",
51 | " for l in range(len(myList)):\n",
52 | " if ele==myList[l]:\n",
53 | " count+=1\n",
54 | " if count>maxno:\n",
55 | " maxno=count\n",
56 | " maxalpha=ele\n",
57 | "print(maxalpha)"
58 | ]
59 | }
60 | ],
61 | "metadata": {
62 | "kernelspec": {
63 | "display_name": "Python 3",
64 | "language": "python",
65 | "name": "python3"
66 | },
67 | "language_info": {
68 | "codemirror_mode": {
69 | "name": "ipython",
70 | "version": 3
71 | },
72 | "file_extension": ".py",
73 | "mimetype": "text/x-python",
74 | "name": "python",
75 | "nbconvert_exporter": "python",
76 | "pygments_lexer": "ipython3",
77 | "version": "3.8.6"
78 | }
79 | },
80 | "nbformat": 4,
81 | "nbformat_minor": 4
82 | }
83 |
--------------------------------------------------------------------------------
/10.Strings/Remove Consecutive Duplicates.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Remove Consecutive Duplicates\n",
8 | "Given a string, S, remove all the consecutive duplicates that are present in the given string. That means, if 'aaa' is present in the string then it should become 'a' in the output string.
\n",
9 | "
\n",
10 | "Input format :
\n",
11 | "String S
\n",
12 | "
\n",
13 | "Output format :
\n",
14 | "Modified string
\n",
15 | "
\n",
16 | "Constraints :
\n",
17 | "0 <= |S| <= 10^7
\n",
18 | "where |S| represents the length of string, S.
\n",
19 | "
\n",
20 | "Sample Input 1:
\n",
21 | "aabccbaa
\n",
22 | "Sample Output 1:
\n",
23 | "abcba
\n",
24 | "Sample Input 2:
\n",
25 | "xxyyzxx
\n",
26 | "Sample Output 2:
\n",
27 | "xyzx"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "s=input()\n",
37 | "s=s+\" \"\n",
38 | "for i in range(len(s)-1):\n",
39 | " if s[i]!=s[i+1]:\n",
40 | " print(s[i],end=\"\")"
41 | ]
42 | }
43 | ],
44 | "metadata": {
45 | "kernelspec": {
46 | "display_name": "Python 3",
47 | "language": "python",
48 | "name": "python3"
49 | },
50 | "language_info": {
51 | "codemirror_mode": {
52 | "name": "ipython",
53 | "version": 3
54 | },
55 | "file_extension": ".py",
56 | "mimetype": "text/x-python",
57 | "name": "python",
58 | "nbconvert_exporter": "python",
59 | "pygments_lexer": "ipython3",
60 | "version": "3.8.6"
61 | }
62 | },
63 | "nbformat": 4,
64 | "nbformat_minor": 4
65 | }
66 |
--------------------------------------------------------------------------------
/10.Strings/Remove character.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Remove character\n",
8 | "Given a string and a character x. Write a function to remove all occurrences of x character from the given string.
\n",
9 | "Leave the string as it is, if the given character is not present in the string.
\n",
10 | "
\n",
11 | "
\n",
12 | "Input Format :
\n",
13 | "Line 1 : String S
\n",
14 | "Line 2 : Character c
\n",
15 | "
\n",
16 | "Output Format :
\n",
17 | "Modified string
\n",
18 | "
\n",
19 | "Constraints :
\n",
20 | "0 <= |S| <= 10^7
\n",
21 | "where |S| represents the length of string, S.
\n",
22 | "
\n",
23 | "Sample Input 1:
\n",
24 | "welcome to coding ninjas
\n",
25 | "o
\n",
26 | "Sample Output 1:
\n",
27 | "welcme t cding ninjas
\n",
28 | "Sample Input 2:
\n",
29 | "Think of edge cases before submitting solutions
\n",
30 | "x
\n",
31 | "Sample Output 2:
\n",
32 | "Think of edge cases before submitting solutions"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "s=input()\n",
42 | "r=input()\n",
43 | "for i in range(len(s)):\n",
44 | " if s[i]!=r:\n",
45 | " print(s[i],end=\"\")"
46 | ]
47 | }
48 | ],
49 | "metadata": {
50 | "kernelspec": {
51 | "display_name": "Python 3",
52 | "language": "python",
53 | "name": "python3"
54 | },
55 | "language_info": {
56 | "codemirror_mode": {
57 | "name": "ipython",
58 | "version": 3
59 | },
60 | "file_extension": ".py",
61 | "mimetype": "text/x-python",
62 | "name": "python",
63 | "nbconvert_exporter": "python",
64 | "pygments_lexer": "ipython3",
65 | "version": "3.8.6"
66 | }
67 | },
68 | "nbformat": 4,
69 | "nbformat_minor": 4
70 | }
71 |
--------------------------------------------------------------------------------
/10.Strings/Reverse Each Word.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Reverse Each Word\n",
8 | "Given a string S, reverse each word of a string individually. For eg. if a string is \"abc def\", reversed string should be \"cba fed\".
\n",
9 | "
\n",
10 | "Input Format :
\n",
11 | "String S
\n",
12 | "
\n",
13 | "Output Format :
\n",
14 | "Modified string
\n",
15 | "
\n",
16 | "Constraints :
\n",
17 | "0 <= |S| <= 10^7
\n",
18 | "where |S| represents the length of string, S.
\n",
19 | "
\n",
20 | "Sample Input 1:
\n",
21 | "Welcome to Coding Ninjas
\n",
22 | "Sample Output 1:
\n",
23 | "emocleW ot gnidoC sajniN
\n",
24 | "Sample Input 2:
\n",
25 | "Give proper names to variables and functions
\n",
26 | "Sample Output 2:
\n",
27 | "eviG reporp seman ot selbairav dna snoitcnuf"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "s=input()\n",
37 | "new=s.split()\n",
38 | "for i in range(len(new)):\n",
39 | " sad=new[i][::-1]\n",
40 | " print(sad,end=\" \")"
41 | ]
42 | }
43 | ],
44 | "metadata": {
45 | "kernelspec": {
46 | "display_name": "Python 3",
47 | "language": "python",
48 | "name": "python3"
49 | },
50 | "language_info": {
51 | "codemirror_mode": {
52 | "name": "ipython",
53 | "version": 3
54 | },
55 | "file_extension": ".py",
56 | "mimetype": "text/x-python",
57 | "name": "python",
58 | "nbconvert_exporter": "python",
59 | "pygments_lexer": "ipython3",
60 | "version": "3.8.6"
61 | }
62 | },
63 | "nbformat": 4,
64 | "nbformat_minor": 4
65 | }
66 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Calculator.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Calculator
\n",
8 | "
\n",
9 | "Write a program that performs the tasks of a simple calculator. The program should first take an integer as input and then based on that integer perform the task as given below.
\n",
10 | "
\n",
11 | "1. If the input is 1, 2 integers are taken from the user and their sum is printed.
\n",
12 | "2. If the input is 2, 2 integers are taken from the user and their difference(1st number - 2nd number) is printed.
\n",
13 | "3. If the input is 3, 2 integers are taken from the user and their product is printed.
\n",
14 | "4. If the input is 4, 2 integers are taken from the user and the quotient obtained (on dividing 1st number by 2nd number) is printed.
\n",
15 | "5. If the input is 5, 2 integers are taken from the user and their remainder(1st number mod 2nd number) is printed.
\n",
16 | "6. If the input is 6, the program exits.
\n",
17 | "7. For any other input, print "Invalid Operation".
\n",
18 | "Note: Each answer in next line.
\n",
19 | "
\n",
20 | "Input format:
\n",
21 | "Take integers as input, in accordance to the description of the question.
\n",
22 | "
\n",
23 | "Constraints:
\n",
24 | "Time Limit: 1 second
\n",
25 | "
\n",
26 | "Output format:
\n",
27 | "The output lines must be as prescribed in the description of the question.
\n",
28 | "
\n",
29 | "Sample Input:
\n",
30 | "3
\n",
31 | "1
\n",
32 | "2
\n",
33 | "4
\n",
34 | "4
\n",
35 | "2
\n",
36 | "1
\n",
37 | "3
\n",
38 | "2
\n",
39 | "7
\n",
40 | "6
\n",
41 | "Sample Output:
\n",
42 | "2
\n",
43 | "2
\n",
44 | "5
\n",
45 | "Invalid Operation"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {},
52 | "outputs": [],
53 | "source": [
54 | "while True:\n",
55 | " n = int(input())\n",
56 | " if n == 1:\n",
57 | " n1 = int(input())\n",
58 | " n2 = int(input())\n",
59 | " print(int(n1+n2))\n",
60 | " elif n == 2:\n",
61 | " n1 = int(input())\n",
62 | " n2 = int(input())\n",
63 | " print(int(n1-n2))\n",
64 | " elif n == 3:\n",
65 | " n1 = int(input())\n",
66 | " n2 = int(input())\n",
67 | " print(int(n1*n2))\n",
68 | " elif n == 4:\n",
69 | " n1 = int(input())\n",
70 | " n2 = int(input())\n",
71 | " print(int(n1/n2))\n",
72 | " elif n == 5:\n",
73 | " n1 = int(input())\n",
74 | " n2 = int(input())\n",
75 | " print(int(n1//n2))\n",
76 | " elif n == 6:\n",
77 | " exit()\n",
78 | " else:\n",
79 | " print(\"Invalid Operation\")"
80 | ]
81 | }
82 | ],
83 | "metadata": {
84 | "kernelspec": {
85 | "display_name": "Python 3",
86 | "language": "python",
87 | "name": "python3"
88 | },
89 | "language_info": {
90 | "codemirror_mode": {
91 | "name": "ipython",
92 | "version": 3
93 | },
94 | "file_extension": ".py",
95 | "mimetype": "text/x-python",
96 | "name": "python",
97 | "nbconvert_exporter": "python",
98 | "pygments_lexer": "ipython3",
99 | "version": "3.8.6"
100 | }
101 | },
102 | "nbformat": 4,
103 | "nbformat_minor": 4
104 | }
105 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Check number.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Check number\n",
8 | "Given an integer n, find if n is positive, negative or 0.\n",
9 | "
If n is positive, print \"Positive\"\n",
10 | "
If n is negative, print \"Negative\"\n",
11 | "
And if n is equal to 0, print \"Zero\".\n",
12 | "
Input Format :\n",
13 | "
Integer n\n",
14 | "
Output Format:\n",
15 | "
\"Positive\" or \"Negative\" or \"Zero\" (without double quotes)\n",
16 | "
Sample Input 1 :\n",
17 | "
10\n",
18 | "
Sample Output 1 :\n",
19 | "
Positive"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "n=int(input())\n",
29 | "if n>0:\n",
30 | " print(\"Positive\")\n",
31 | "elif n==0:\n",
32 | " print(\"Zero\")\n",
33 | "else:\n",
34 | " print(\"Negative\")"
35 | ]
36 | }
37 | ],
38 | "metadata": {
39 | "kernelspec": {
40 | "display_name": "Python 3",
41 | "language": "python",
42 | "name": "python3"
43 | },
44 | "language_info": {
45 | "codemirror_mode": {
46 | "name": "ipython",
47 | "version": 3
48 | },
49 | "file_extension": ".py",
50 | "mimetype": "text/x-python",
51 | "name": "python",
52 | "nbconvert_exporter": "python",
53 | "pygments_lexer": "ipython3",
54 | "version": "3.8.6"
55 | }
56 | },
57 | "nbformat": 4,
58 | "nbformat_minor": 4
59 | }
60 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Fahrenheit to Celsius.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Fahrenheit to Celsius\n",
8 | "
Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), you need to convert all Fahrenheit values from Start to End at the gap of W, into their corresponding Celsius values and print the table.\n",
9 | "\n",
10 | "
Input Format :\n",
11 | "3 integers - S, E and W respectively \n",
12 | "
Output Format :\n",
13 | "## Hint print(val1, \" \", val2)\n",
14 | "
Fahrenheit to Celsius conversion table. One line for every Fahrenheit and corresponding Celsius value. On Fahrenheit value and its corresponding Celsius value should be separate by tab (\"\\t\")\n",
15 | "\n",
16 | "
Constraints :\n",
17 | "
0 <= S <= 80\n",
18 | "
S <= E <= 900\n",
19 | "
0 <= W <= 40 \n",
20 | "\n",
21 | "
Sample Input 1:\n",
22 | "
0 \n",
23 | "
100 \n",
24 | "
20\n",
25 | "
Sample Output 1:\n",
26 | "
0 -17\n",
27 | "
20 -6\n",
28 | "
40 4\n",
29 | "
60 15\n",
30 | "
80 26\n",
31 | "
100 37\n",
32 | "
Sample Input 2:\n",
33 | "
20\n",
34 | "
119\n",
35 | "
13\n",
36 | "
Sample Output 2:\n",
37 | "
20 -6\n",
38 | "
33 0 \n",
39 | "
46 7\n",
40 | "
59 15\n",
41 | "
72 22\n",
42 | "
85 29\n",
43 | "
98 36\n",
44 | "
111 43\n",
45 | "\n",
46 | "
Explanation For Input 2:\n",
47 | "
We need need to start calculating the Celsius values for each of the Fahrenheit Value which starts from 20. So starting from 20 which is the given Fahrenheit start value, we need to compute its corresponding Celsius value which computes to -6. We print this information as a tab space\"\\t\" on each line for each step of 13 we take to get the next value of Fahrenheit and extend this idea till we reach the end that is till 119 in this case. You may or may not exactly land on the end value depending on the steps you are taking."
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "Fahrenheit = int(input())\n",
57 | "E = int(input())\n",
58 | "W = int(input())\n",
59 | "while Fahrenheit <= E:\n",
60 | " Fahrenheit_to_Celsius = (((Fahrenheit - 32) * 5) / 9)\n",
61 | " print(Fahrenheit, int(Fahrenheit_to_Celsius))\n",
62 | " Fahrenheit = Fahrenheit + W"
63 | ]
64 | }
65 | ],
66 | "metadata": {
67 | "kernelspec": {
68 | "display_name": "Python 3",
69 | "language": "python",
70 | "name": "python3"
71 | },
72 | "language_info": {
73 | "codemirror_mode": {
74 | "name": "ipython",
75 | "version": 3
76 | },
77 | "file_extension": ".py",
78 | "mimetype": "text/x-python",
79 | "name": "python",
80 | "nbconvert_exporter": "python",
81 | "pygments_lexer": "ipython3",
82 | "version": "3.8.6"
83 | }
84 | },
85 | "nbformat": 4,
86 | "nbformat_minor": 4
87 | }
88 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Nth Fibonacci number.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Nth Fibonacci number
\n",
8 | "Nth term of fibonacci series F(n) is calculated using following formula -
\n",
9 | " F(n) = F(n-1) + F(n-2),
\n",
10 | " Where, F(1) = F(2) = 1
\n",
11 | "Provided N you have to find out the Nth Fibonacci Number.
\n",
12 | "
\n",
13 | "Input Format :
\n",
14 | "Integer n
\n",
15 | "Output Format :
\n",
16 | "Nth Fibonacci term i.e. F(n)
\n",
17 | "
\n",
18 | "Constraints:
\n",
19 | "1 <= n <= 25
\n",
20 | "
\n",
21 | "Sample Input 1:
\n",
22 | "4
\n",
23 | "Sample Output 2:
\n",
24 | "3
\n",
25 | "
\n",
26 | "Sample Input 1:
\n",
27 | "6
\n",
28 | "Sample Output 2:
\n",
29 | "8"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "def Fibonacci(n):\n",
39 | " if n == 1:\n",
40 | " return 1\n",
41 | " elif n == 2:\n",
42 | " return 1\n",
43 | " else:\n",
44 | " return Fibonacci(n - 1) + Fibonacci(n - 2) \n",
45 | " \n",
46 | "n=int(input())\n",
47 | "print(Fibonacci(n))"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "# Use This To Solve Time Exceed Error\n",
57 | "n = int(input())\n",
58 | "a = 0\n",
59 | "b = 1\n",
60 | "c = -1\n",
61 | "for i in range(n) :\n",
62 | " c = a + b\n",
63 | " a = b\n",
64 | " b = c \n",
65 | "print(a)"
66 | ]
67 | }
68 | ],
69 | "metadata": {
70 | "kernelspec": {
71 | "display_name": "Python 3",
72 | "language": "python",
73 | "name": "python3"
74 | },
75 | "language_info": {
76 | "codemirror_mode": {
77 | "name": "ipython",
78 | "version": 3
79 | },
80 | "file_extension": ".py",
81 | "mimetype": "text/x-python",
82 | "name": "python",
83 | "nbconvert_exporter": "python",
84 | "pygments_lexer": "ipython3",
85 | "version": "3.8.6"
86 | }
87 | },
88 | "nbformat": 4,
89 | "nbformat_minor": 4
90 | }
91 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Palindrome number.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Palindrome number
\n",
8 | "Write a program to determine if given number is palindrome or not. Print true if it is palindrome, false otherwise.
\n",
9 | "1.Palindrome are the numbers for which reverse is exactly same as the original one. For eg. 121
\n",
10 | "
\n",
11 | "Sample Input 1 :
\n",
12 | "121
\n",
13 | "Sample Output 1 :
\n",
14 | "true
\n",
15 | "
\n",
16 | "Sample Input 2 :
\n",
17 | "1032
\n",
18 | "Sample Output 2 :
\n",
19 | "false"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "def checkPalindrome(n):\n",
29 | " Revese = 0\n",
30 | " while n > 0:\n",
31 | " Remainder = n % 10\n",
32 | " n = int(n / 10)\n",
33 | " Revese = Revese * 10 + Remainder\n",
34 | " return Revese\n",
35 | "\n",
36 | "\n",
37 | "num = int(input())\n",
38 | "Rev=checkPalindrome(num)\n",
39 | "if (num==Rev):\n",
40 | " print('true')\n",
41 | "else:\n",
42 | " print('false')"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "# Another Solution Of Above Problem"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "num = int(input())\n",
59 | "real_num=num\n",
60 | "test_num = 0\n",
61 | "while (num > 0):\n",
62 | " remainder = num % 10\n",
63 | " test_num = (test_num * 10) + remainder\n",
64 | " num = num // 10\n",
65 | "if int(test_num)==real_num:\n",
66 | " print(\"true\")\n",
67 | "else:\n",
68 | " print(\"false\")"
69 | ]
70 | }
71 | ],
72 | "metadata": {
73 | "kernelspec": {
74 | "display_name": "Python 3",
75 | "language": "python",
76 | "name": "python3"
77 | },
78 | "language_info": {
79 | "codemirror_mode": {
80 | "name": "ipython",
81 | "version": 3
82 | },
83 | "file_extension": ".py",
84 | "mimetype": "text/x-python",
85 | "name": "python",
86 | "nbconvert_exporter": "python",
87 | "pygments_lexer": "ipython3",
88 | "version": "3.8.6"
89 | }
90 | },
91 | "nbformat": 4,
92 | "nbformat_minor": 4
93 | }
94 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Reverse of a number.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Reverse of a number
\n",
8 | "Write a program to generate the reverse of a given number N. Print the corresponding reverse number.
\n",
9 | "
\n",
10 | "1. Note : If a number has trailing zeros, then its reverse will not include them. For e.g., reverse of 10400 will be 401 instead of 00401.
\n",
11 | "
\n",
12 | "Input format :
\n",
13 | "Integer N
\n",
14 | "Output format :
\n",
15 | "Corresponding reverse number
\n",
16 | "
\n",
17 | "Constraints:
\n",
18 | "0 <= N < 10^8
\n",
19 | "
\n",
20 | "Sample Input 1 :
\n",
21 | "1234
\n",
22 | "Sample Output 1 :
\n",
23 | "4321
\n",
24 | "
\n",
25 | "Sample Input 2 :
\n",
26 | "1980
\n",
27 | "Sample Output 2 :
\n",
28 | "891"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "num = int(input())\n",
38 | "test_num = 0\n",
39 | "while (num > 0):\n",
40 | " remainder = num % 10\n",
41 | " test_num = (test_num * 10) + remainder\n",
42 | " num = num // 10\n",
43 | "\n",
44 | "print(test_num)"
45 | ]
46 | }
47 | ],
48 | "metadata": {
49 | "kernelspec": {
50 | "display_name": "Python 3",
51 | "language": "python",
52 | "name": "python3"
53 | },
54 | "language_info": {
55 | "codemirror_mode": {
56 | "name": "ipython",
57 | "version": 3
58 | },
59 | "file_extension": ".py",
60 | "mimetype": "text/x-python",
61 | "name": "python",
62 | "nbconvert_exporter": "python",
63 | "pygments_lexer": "ipython3",
64 | "version": "3.8.6"
65 | }
66 | },
67 | "nbformat": 4,
68 | "nbformat_minor": 4
69 | }
70 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Sum of Even Numbers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sum of Even Numbers\n",
8 | "\n",
9 | "
Sum of Even Numbers\n",
10 | "
Given a number N, print sum of all even numbers from 1 to N.\n",
11 | "\n",
12 | "
Input Format :\n",
13 | "
Integer N\n",
14 | "\n",
15 | "
Output Format :\n",
16 | "
Required Sum \n",
17 | "\n",
18 | "
Sample Input 1 :\n",
19 | "
6\n",
20 | "
Sample Output 1 :\n",
21 | "
12"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "n=int(input())\n",
31 | "add=0\n",
32 | "n3=2\n",
33 | "if n%2==0:\n",
34 | " while n3<=n:\n",
35 | " add=add+n3\n",
36 | " n3=n3+2\n",
37 | "else:\n",
38 | " n=n-1\n",
39 | " while n3<=n:\n",
40 | " add=add+n3\n",
41 | " n3=n3+2\n",
42 | "print(add)"
43 | ]
44 | }
45 | ],
46 | "metadata": {
47 | "kernelspec": {
48 | "display_name": "Python 3",
49 | "language": "python",
50 | "name": "python3"
51 | },
52 | "language_info": {
53 | "codemirror_mode": {
54 | "name": "ipython",
55 | "version": 3
56 | },
57 | "file_extension": ".py",
58 | "mimetype": "text/x-python",
59 | "name": "python",
60 | "nbconvert_exporter": "python",
61 | "pygments_lexer": "ipython3",
62 | "version": "3.8.6"
63 | }
64 | },
65 | "nbformat": 4,
66 | "nbformat_minor": 4
67 | }
68 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Sum of even & odd.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sum of even & odd
\n",
8 | "1.Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.
\n",
9 | "2.Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
\n",
10 | "
\n",
11 | "Input format :
\n",
12 | "Integer N
\n",
13 | "Output format :
\n",
14 | "Sum_of_Even_Digits Sum_of_Odd_Digits
\n",
15 | "(Print first even sum and then odd sum separated by space)
\n",
16 | "
\n",
17 | "Constraints
\n",
18 | "0 <= N <= 10^8
\n",
19 | "
\n",
20 | "Sample Input 1:
\n",
21 | "1234
\n",
22 | "Sample Output 1:
\n",
23 | "6 4
\n",
24 | "
\n",
25 | "Sample Input 2:
\n",
26 | "552245
\n",
27 | "Sample Output 2:
\n",
28 | "8 15
\n",
29 | "
\n",
30 | "3.Explanation for Input 2:
\n",
31 | "For the given input, the even digits are 2, 2 and 4 and if we take the sum of these digits it will come out to be 8(2 + 2 + 4) and similarly, if we look at the odd digits, they are, 5, 5 and 5 which makes a sum of 15(5 + 5 + 5). Hence the answer would be, 8(evenSum) 15(oddSum)"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": null,
37 | "metadata": {},
38 | "outputs": [],
39 | "source": [
40 | "n = int(input())\n",
41 | "sum_of_even = 0\n",
42 | "sum_of_odd = 0\n",
43 | "while n > 0:\n",
44 | " r = (n%10)\n",
45 | " if r % 2 == 0:\n",
46 | " sum_of_even = sum_of_even + r\n",
47 | " else:\n",
48 | " sum_of_odd = sum_of_odd + r\n",
49 | " n = (n // 10) \n",
50 | "print(sum_of_even,sum_of_odd)"
51 | ]
52 | }
53 | ],
54 | "metadata": {
55 | "kernelspec": {
56 | "display_name": "Python 3",
57 | "language": "python",
58 | "name": "python3"
59 | },
60 | "language_info": {
61 | "codemirror_mode": {
62 | "name": "ipython",
63 | "version": 3
64 | },
65 | "file_extension": ".py",
66 | "mimetype": "text/x-python",
67 | "name": "python",
68 | "nbconvert_exporter": "python",
69 | "pygments_lexer": "ipython3",
70 | "version": "3.8.6"
71 | }
72 | },
73 | "nbformat": 4,
74 | "nbformat_minor": 4
75 | }
76 |
--------------------------------------------------------------------------------
/2.Conditionals and Loops/Sum of n numbers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sum of n numbers\n",
8 | "
Given an integer n, find and print the sum of numbers from 1 to n.\n",
9 | "
Note : Use while loop only.\n",
10 | "
Input Format :\n",
11 | "
Integer n\n",
12 | "
Output Format :\n",
13 | "
Sum\n",
14 | "
Sample Input :\n",
15 | "
10\n",
16 | "
Sample Output :\n",
17 | "
55"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "n=int(input())\n",
27 | "add=0\n",
28 | "n3=1\n",
29 | "while n3<=n:\n",
30 | " add=add+n3\n",
31 | " n3=n3+1\n",
32 | "print(add)"
33 | ]
34 | }
35 | ],
36 | "metadata": {
37 | "kernelspec": {
38 | "display_name": "Python 3",
39 | "language": "python",
40 | "name": "python3"
41 | },
42 | "language_info": {
43 | "codemirror_mode": {
44 | "name": "ipython",
45 | "version": 3
46 | },
47 | "file_extension": ".py",
48 | "mimetype": "text/x-python",
49 | "name": "python",
50 | "nbconvert_exporter": "python",
51 | "pygments_lexer": "ipython3",
52 | "version": "3.8.6"
53 | }
54 | },
55 | "nbformat": 4,
56 | "nbformat_minor": 4
57 | }
58 |
--------------------------------------------------------------------------------
/2048 Game.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "# 2048 Game
\n",
9 | "\n",
10 | "2048 is a game and you are expected to implement the move function for this game.
\n",
11 | "Arguments passed to the four functions is the matrix which we are using for 2048
\n",
12 | "The move function will be returning new matrix after moving the corresponding move.
\n",
13 | "Implement All The Four Moves Using These Functions -
\n",
14 | "1. move_up
\n",
15 | "2. move_down
\n",
16 | "3. move_left
\n",
17 | "4. move_right"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 1,
23 | "metadata": {},
24 | "outputs": [
25 | {
26 | "name": "stdout",
27 | "output_type": "stream",
28 | "text": [
29 | "1 2 4 3 2 4 3 1 4\n",
30 | "[[4, 4, 2, 2], [0, 8, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n",
31 | "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 4, 0, 0], [4, 8, 2, 2]]\n",
32 | "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 4], [0, 4, 8, 4]]\n",
33 | "[[0, 0, 0, 0], [0, 0, 0, 0], [4, 0, 0, 0], [4, 8, 4, 0]]\n",
34 | "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [8, 8, 4, 0]]\n",
35 | "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 16, 4]]\n",
36 | "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [16, 4, 0, 0]]\n",
37 | "[[16, 4, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n",
38 | "[[0, 0, 16, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n"
39 | ]
40 | }
41 | ],
42 | "source": [
43 | "import random\n",
44 | "def start_game():\n",
45 | " mat = []\n",
46 | " for i in range(4):\n",
47 | " mat.append([0]*4)\n",
48 | " return mat\n",
49 | "\n",
50 | "def compress(mat):\n",
51 | " new_mat = []\n",
52 | " for i in range(4):\n",
53 | " new_mat.append([0] * 4)\n",
54 | " for i in range(4):\n",
55 | " pos = 0\n",
56 | " for j in range(4):\n",
57 | " if mat[i][j] != 0:\n",
58 | " new_mat[i][pos] = mat[i][j]\n",
59 | " pos = pos + 1\n",
60 | " return new_mat\n",
61 | "\n",
62 | "def merge(mat):\n",
63 | " for i in range(4):\n",
64 | " for j in range(3):\n",
65 | " if mat[i][j] == mat[i][j + 1]:\n",
66 | " mat[i][j] = (mat[i][j]) * 2\n",
67 | " mat[i][j + 1] = 0\n",
68 | " return mat\n",
69 | "\n",
70 | "def reverse(mat):\n",
71 | " new_mat = []\n",
72 | " for i in range(4):\n",
73 | " new_mat.append(mat[i][::-1])\n",
74 | " return new_mat\n",
75 | "\n",
76 | "def transpose(mat):\n",
77 | " new_mat = []\n",
78 | " for i in range(4):\n",
79 | " new_mat.append([])\n",
80 | " for j in range(4):\n",
81 | " new_mat[i].append(mat[j][i])\n",
82 | " return new_mat\n",
83 | "\n",
84 | "def move_up(grid):\n",
85 | " transposed_grid = transpose(grid)\n",
86 | " new_grid = compress(transposed_grid)\n",
87 | " new_grid = merge(new_grid)\n",
88 | " new_grid = compress(new_grid)\n",
89 | " final_grid = transpose(new_grid)\n",
90 | " return final_grid\n",
91 | "\n",
92 | "def move_down(grid):\n",
93 | " transposed_grid = transpose(grid)\n",
94 | " reverse_grid = reverse(transposed_grid)\n",
95 | " new_grid = compress(reverse_grid)\n",
96 | " new_grid = merge(new_grid)\n",
97 | " new_grid = compress(new_grid)\n",
98 | " new_reverse = reverse(new_grid)\n",
99 | " final_grid = transpose(new_reverse)\n",
100 | " return final_grid\n",
101 | "\n",
102 | "def move_right(grid):\n",
103 | " reverse_grid = reverse(grid)\n",
104 | " new_grid = compress(reverse_grid)\n",
105 | " new_grid = merge(new_grid)\n",
106 | " new_grid = compress(new_grid)\n",
107 | " final_grid = reverse(new_grid)\n",
108 | " return final_grid\n",
109 | "\n",
110 | "def move_left(grid):\n",
111 | " new_grid = compress(grid)\n",
112 | " new_grid = merge(new_grid)\n",
113 | " new_grid = compress(new_grid)\n",
114 | " return new_grid\n",
115 | "\n",
116 | "\n",
117 | "mat = start_game()\n",
118 | "mat[1][3] = 2\n",
119 | "mat[2][2] = 2\n",
120 | "mat[3][0] = 4\n",
121 | "mat[3][1] = 8\n",
122 | "mat[2][1] = 4\n",
123 | "\n",
124 | "inputs = [int(ele) for ele in input().split()]\n",
125 | "for ele in inputs:\n",
126 | " if ele == 1:\n",
127 | " mat = move_up(mat)\n",
128 | " elif ele == 2:\n",
129 | " mat = move_down(mat)\n",
130 | " elif ele == 3:\n",
131 | " mat = move_left(mat)\n",
132 | " else:\n",
133 | " mat = move_right(mat)\n",
134 | " print(mat)"
135 | ]
136 | }
137 | ],
138 | "metadata": {
139 | "kernelspec": {
140 | "display_name": "Python 3",
141 | "language": "python",
142 | "name": "python3"
143 | },
144 | "language_info": {
145 | "codemirror_mode": {
146 | "name": "ipython",
147 | "version": 3
148 | },
149 | "file_extension": ".py",
150 | "mimetype": "text/x-python",
151 | "name": "python",
152 | "nbconvert_exporter": "python",
153 | "pygments_lexer": "ipython3",
154 | "version": "3.8.6"
155 | }
156 | },
157 | "nbformat": 4,
158 | "nbformat_minor": 4
159 | }
160 |
--------------------------------------------------------------------------------
/3. Pattern 1/Alpha Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Alpha Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 3
\n",
11 | "A
\n",
12 | "BB
\n",
13 | "CCC
\n",
14 | "
\n",
15 | "Input format :
\n",
16 | "Integer N (Total no. of rows)
\n",
17 | "
\n",
18 | "Output format :
\n",
19 | "Pattern in N lines
\n",
20 | "
\n",
21 | "Constraints
\n",
22 | "0 <= N <= 26
\n",
23 | "
\n",
24 | "Sample Input 1:
\n",
25 | "7
\n",
26 | "Sample Output 1:
\n",
27 | "A
\n",
28 | "BB
\n",
29 | "CCC
\n",
30 | "DDDD
\n",
31 | "EEEEE
\n",
32 | "FFFFFF
\n",
33 | "GGGGGGG
\n",
34 | "Sample Input 2:
\n",
35 | "6
\n",
36 | "Sample Output 2:
\n",
37 | "A
\n",
38 | "BB
\n",
39 | "CCC
\n",
40 | "DDDD
\n",
41 | "EEEEE
\n",
42 | "FFFFFF"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "n=int(input())\n",
52 | "currRow=1\n",
53 | "while currRow<=n:\n",
54 | " currCol=1\n",
55 | " char=ord('A')+currRow-1\n",
56 | " while currCol<=currRow:\n",
57 | " print(chr(char),end=\"\")\n",
58 | " currCol+=1\n",
59 | " print()\n",
60 | " currRow+=1"
61 | ]
62 | }
63 | ],
64 | "metadata": {
65 | "kernelspec": {
66 | "display_name": "Python 3",
67 | "language": "python",
68 | "name": "python3"
69 | },
70 | "language_info": {
71 | "codemirror_mode": {
72 | "name": "ipython",
73 | "version": 3
74 | },
75 | "file_extension": ".py",
76 | "mimetype": "text/x-python",
77 | "name": "python",
78 | "nbconvert_exporter": "python",
79 | "pygments_lexer": "ipython3",
80 | "version": "3.8.6"
81 | }
82 | },
83 | "nbformat": 4,
84 | "nbformat_minor": 4
85 | }
86 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Character Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Character Pattern\n",
8 | "Code : Character Pattern
\n",
9 | "
\n",
10 | "Print the following pattern for the given N number of rows.
\n",
11 | "
\n",
12 | "Pattern for N = 4
\n",
13 | "A
\n",
14 | "BC
\n",
15 | "CDE
\n",
16 | "DEFG
\n",
17 | "
\n",
18 | "Input format :
\n",
19 | "Integer N (Total no. of rows)
\n",
20 | "
\n",
21 | "Output format :
\n",
22 | "Pattern in N lines
\n",
23 | "
\n",
24 | "Constraints
\n",
25 | "0 <= N <= 13
\n",
26 | "
\n",
27 | "Sample Input 1:
\n",
28 | "5
\n",
29 | "Sample Output 1:
\n",
30 | "A
\n",
31 | "BC
\n",
32 | "CDE
\n",
33 | "DEFG
\n",
34 | "EFGHI
\n",
35 | "Sample Input 2:
\n",
36 | "6
\n",
37 | "Sample Output 2:
\n",
38 | "A
\n",
39 | "BC
\n",
40 | "CDE
\n",
41 | "DEFG
\n",
42 | "EFGHI
\n",
43 | "FGHIJK"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "n=int(input())\n",
53 | "currRow=1\n",
54 | "while currRow<=n:\n",
55 | " currCol=1\n",
56 | " char=ord('A')+currRow-1\n",
57 | " while currCol<=currRow:\n",
58 | " print(chr(char+currCol-1),end=\"\")\n",
59 | " currCol+=1\n",
60 | " print()\n",
61 | " currRow+=1"
62 | ]
63 | }
64 | ],
65 | "metadata": {
66 | "kernelspec": {
67 | "display_name": "Python 3",
68 | "language": "python",
69 | "name": "python3"
70 | },
71 | "language_info": {
72 | "codemirror_mode": {
73 | "name": "ipython",
74 | "version": 3
75 | },
76 | "file_extension": ".py",
77 | "mimetype": "text/x-python",
78 | "name": "python",
79 | "nbconvert_exporter": "python",
80 | "pygments_lexer": "ipython3",
81 | "version": "3.8.6"
82 | }
83 | },
84 | "nbformat": 4,
85 | "nbformat_minor": 4
86 | }
87 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Reverse Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Reverse Number Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1
\n",
12 | "21
\n",
13 | "321
\n",
14 | "4321
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Constraints
\n",
23 | "0 <= N <= 50
\n",
24 | "
\n",
25 | "Sample Input 1:
\n",
26 | "5
\n",
27 | "Sample Output 1:
\n",
28 | "1
\n",
29 | "21
\n",
30 | "321
\n",
31 | "4321
\n",
32 | "54321
\n",
33 | "Sample Input 2:
\n",
34 | "6
\n",
35 | "Sample Output 2:
\n",
36 | "1
\n",
37 | "21
\n",
38 | "321
\n",
39 | "4321
\n",
40 | "54321
\n",
41 | "654321"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": null,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "n=int(input())\n",
51 | "\n",
52 | "currRow=1\n",
53 | "while currRow<=n:\n",
54 | " currCol=currRow\n",
55 | " \n",
56 | " while currCol>=1:\n",
57 | " print(currCol,end=\"\")\n",
58 | " currCol-=1\n",
59 | " print()\n",
60 | " currRow+=1"
61 | ]
62 | }
63 | ],
64 | "metadata": {
65 | "kernelspec": {
66 | "display_name": "Python 3",
67 | "language": "python",
68 | "name": "python3"
69 | },
70 | "language_info": {
71 | "codemirror_mode": {
72 | "name": "ipython",
73 | "version": 3
74 | },
75 | "file_extension": ".py",
76 | "mimetype": "text/x-python",
77 | "name": "python",
78 | "nbconvert_exporter": "python",
79 | "pygments_lexer": "ipython3",
80 | "version": "3.8.6"
81 | }
82 | },
83 | "nbformat": 4,
84 | "nbformat_minor": 4
85 | }
86 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Square Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Square Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "4444
\n",
12 | "4444
\n",
13 | "4444
\n",
14 | "4444
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Constraints
\n",
23 | "0 <= N <= 50
\n",
24 | "
\n",
25 | "Sample Input 1:
\n",
26 | "7
\n",
27 | "Sample Output 1:
\n",
28 | "7777777
\n",
29 | "7777777
\n",
30 | "7777777
\n",
31 | "7777777
\n",
32 | "7777777
\n",
33 | "7777777
\n",
34 | "7777777
\n",
35 | "Sample Input 1:
\n",
36 | "6
\n",
37 | "Sample Output 1:
\n",
38 | "666666
\n",
39 | "666666
\n",
40 | "666666
\n",
41 | "666666
\n",
42 | "666666
\n",
43 | "666666"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "n=int(input())\n",
53 | "i=1\n",
54 | "while i<=n:\n",
55 | " j=1\n",
56 | " while j<=n:\n",
57 | " print(n,end=\"\")\n",
58 | " j=j+1\n",
59 | "\n",
60 | " print()\n",
61 | " i=i+1"
62 | ]
63 | }
64 | ],
65 | "metadata": {
66 | "kernelspec": {
67 | "display_name": "Python 3",
68 | "language": "python",
69 | "name": "python3"
70 | },
71 | "language_info": {
72 | "codemirror_mode": {
73 | "name": "ipython",
74 | "version": 3
75 | },
76 | "file_extension": ".py",
77 | "mimetype": "text/x-python",
78 | "name": "python",
79 | "nbconvert_exporter": "python",
80 | "pygments_lexer": "ipython3",
81 | "version": "3.8.6"
82 | }
83 | },
84 | "nbformat": 4,
85 | "nbformat_minor": 4
86 | }
87 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Triangle Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Triangle Number Pattern\n",
8 | "Code : Triangle Number Pattern
\n",
9 | "
\n",
10 | "Print the following pattern for the given N number of rows.
\n",
11 | "
\n",
12 | "Pattern for N = 4
\n",
13 | "1
\n",
14 | "22
\n",
15 | "333
\n",
16 | "4444
\n",
17 | "
\n",
18 | "Input format :
\n",
19 | "Integer N (Total no. of rows)
\n",
20 | "
\n",
21 | "Output format :
\n",
22 | "Pattern in N lines
\n",
23 | "
\n",
24 | "Constraints
\n",
25 | "0 <= N <= 50
\n",
26 | "
\n",
27 | "Sample Input 1:
\n",
28 | "5
\n",
29 | "Sample Output 1:
\n",
30 | "1
\n",
31 | "22
\n",
32 | "333
\n",
33 | "4444
\n",
34 | "55555
\n",
35 | "Sample Input 2:
\n",
36 | "6
\n",
37 | "Sample Output 2:
\n",
38 | "1
\n",
39 | "22
\n",
40 | "333
\n",
41 | "4444
\n",
42 | "55555
\n",
43 | "666666"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "n=int(input())\n",
53 | "i=1\n",
54 | "num=1\n",
55 | "while i<=n:\n",
56 | " pattern=1\n",
57 | " while pattern<=i: #no of Colums\n",
58 | " print(num,end=\"\")\n",
59 | " pattern+=1\n",
60 | " num+=1\n",
61 | " print(\"\")\n",
62 | " i=i+1"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "#Another Solution For Above Problem"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "n = int(input())\n",
81 | "i = 1\n",
82 | "k = 1\n",
83 | "while i <= n:\n",
84 | " j = 1\n",
85 | " while j <= i: # no of Colums\n",
86 | " print(k, end=\"\")\n",
87 | " j = j + 1\n",
88 | " print(\"\")\n",
89 | " i = i + 1\n",
90 | " k = k + 1"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": [
99 | "#Another Solution For Above Problem"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {},
106 | "outputs": [],
107 | "source": [
108 | "n=int(input())\n",
109 | "i=1\n",
110 | "num=1\n",
111 | "while i<=n:\n",
112 | " pattern=1\n",
113 | " while pattern<=i: #no of Colums\n",
114 | " print(num,end=\"\")\n",
115 | " pattern+=1\n",
116 | " \n",
117 | " num+=1\n",
118 | " print(\"\")\n",
119 | " i=i+1"
120 | ]
121 | }
122 | ],
123 | "metadata": {
124 | "kernelspec": {
125 | "display_name": "Python 3",
126 | "language": "python",
127 | "name": "python3"
128 | },
129 | "language_info": {
130 | "codemirror_mode": {
131 | "name": "ipython",
132 | "version": 3
133 | },
134 | "file_extension": ".py",
135 | "mimetype": "text/x-python",
136 | "name": "python",
137 | "nbconvert_exporter": "python",
138 | "pygments_lexer": "ipython3",
139 | "version": "3.8.6"
140 | }
141 | },
142 | "nbformat": 4,
143 | "nbformat_minor": 4
144 | }
145 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Triangular Star Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Triangular Star Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "Pattern for N = 4
\n",
10 | "
\n",
11 | "
\n",
12 | "Note : There are no spaces between the stars.
\n",
13 | "Input format :
\n",
14 | "Integer N (Total no. of rows)
\n",
15 | "Output format :
\n",
16 | "Pattern in N lines
\n",
17 | "Constraints
\n",
18 | "0 <= N <= 50
\n",
19 | "Sample Input 1:
\n",
20 | "5
\n",
21 | "Sample Output 1:
\n",
22 | "
\n",
23 | "Sample Input 2:
\n",
24 | "6
\n",
25 | "Sample Output 2:
\n",
26 | ""
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {},
33 | "outputs": [
34 | {
35 | "name": "stdout",
36 | "output_type": "stream",
37 | "text": [
38 | "5\n",
39 | "*\n",
40 | "**\n",
41 | "***\n",
42 | "****\n",
43 | "*****\n"
44 | ]
45 | }
46 | ],
47 | "source": [
48 | "n=int(input())\n",
49 | "i=1\n",
50 | "while i<=n:\n",
51 | " star=1\n",
52 | " while star<=i:\n",
53 | " print(\"*\",end=\"\")\n",
54 | " star+=1\n",
55 | "\n",
56 | " print(\"\")\n",
57 | " i=i+1"
58 | ]
59 | }
60 | ],
61 | "metadata": {
62 | "kernelspec": {
63 | "display_name": "Python 3",
64 | "language": "python",
65 | "name": "python3"
66 | },
67 | "language_info": {
68 | "codemirror_mode": {
69 | "name": "ipython",
70 | "version": 3
71 | },
72 | "file_extension": ".py",
73 | "mimetype": "text/x-python",
74 | "name": "python",
75 | "nbconvert_exporter": "python",
76 | "pygments_lexer": "ipython3",
77 | "version": "3.8.6"
78 | }
79 | },
80 | "nbformat": 4,
81 | "nbformat_minor": 4
82 | }
83 |
--------------------------------------------------------------------------------
/3. Pattern 1/Code Interesting Alphabets.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Interesting Alphabets\n",
8 | "
\n",
9 | "Print the following pattern for the given number of rows.
\n",
10 | "
\n",
11 | "Pattern for N = 5
\n",
12 | "E
\n",
13 | "DE
\n",
14 | "CDE
\n",
15 | "BCDE
\n",
16 | "ABCDE
\n",
17 | "
\n",
18 | "Input format :
\n",
19 | "N (Total no. of rows)
\n",
20 | "
\n",
21 | "Output format :
\n",
22 | "Pattern in N lines
\n",
23 | "
\n",
24 | "Constraints
\n",
25 | "0 <= N <= 26
\n",
26 | "
\n",
27 | "Sample Input 1:
\n",
28 | "8
\n",
29 | "Sample Output 1:
\n",
30 | "H
\n",
31 | "GH
\n",
32 | "FGH
\n",
33 | "EFGH
\n",
34 | "DEFGH
\n",
35 | "CDEFGH
\n",
36 | "BCDEFGH
\n",
37 | "ABCDEFGH
\n",
38 | "Sample Input 2:
\n",
39 | "7
\n",
40 | "Sample Output 2:
\n",
41 | "G
\n",
42 | "FG
\n",
43 | "EFG
\n",
44 | "DEFG
\n",
45 | "CDEFG
\n",
46 | "BCDEFG
\n",
47 | "ABCDEFG"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "n=int(input())\n",
57 | "currRow=1\n",
58 | "while currRow<=n:\n",
59 | " currCol=1\n",
60 | " char=ord('A')+n-currRow\n",
61 | " while currCol<=currRow:\n",
62 | " print(chr(char+currCol-1),end=\"\")\n",
63 | " currCol+=1\n",
64 | " print()\n",
65 | " currRow+=1"
66 | ]
67 | }
68 | ],
69 | "metadata": {
70 | "kernelspec": {
71 | "display_name": "Python 3",
72 | "language": "python",
73 | "name": "python3"
74 | },
75 | "language_info": {
76 | "codemirror_mode": {
77 | "name": "ipython",
78 | "version": 3
79 | },
80 | "file_extension": ".py",
81 | "mimetype": "text/x-python",
82 | "name": "python",
83 | "nbconvert_exporter": "python",
84 | "pygments_lexer": "ipython3",
85 | "version": "3.8.6"
86 | }
87 | },
88 | "nbformat": 4,
89 | "nbformat_minor": 4
90 | }
91 |
--------------------------------------------------------------------------------
/3. Pattern 1/Number Pattern 1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Number Pattern 1\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1
\n",
12 | "11
\n",
13 | "111
\n",
14 | "1111
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Sample Input :
\n",
23 | "5
\n",
24 | "Sample Output :
\n",
25 | "1
\n",
26 | "11
\n",
27 | "111
\n",
28 | "1111
\n",
29 | "11111"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "i=1\n",
40 | "while(i<=n):\n",
41 | " j=1\n",
42 | " while(j<=i):\n",
43 | " print(1,end=\"\")\n",
44 | " j+=1\n",
45 | " i+=1\n",
46 | " print()"
47 | ]
48 | }
49 | ],
50 | "metadata": {
51 | "kernelspec": {
52 | "display_name": "Python 3",
53 | "language": "python",
54 | "name": "python3"
55 | },
56 | "language_info": {
57 | "codemirror_mode": {
58 | "name": "ipython",
59 | "version": 3
60 | },
61 | "file_extension": ".py",
62 | "mimetype": "text/x-python",
63 | "name": "python",
64 | "nbconvert_exporter": "python",
65 | "pygments_lexer": "ipython3",
66 | "version": "3.8.6"
67 | }
68 | },
69 | "nbformat": 4,
70 | "nbformat_minor": 4
71 | }
72 |
--------------------------------------------------------------------------------
/3. Pattern 1/Number Pattern 2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Number Pattern 2\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1
\n",
12 | "11
\n",
13 | "202
\n",
14 | "3003
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Sample Input :
\n",
23 | "5
\n",
24 | "Sample Output :
\n",
25 | "1
\n",
26 | "11
\n",
27 | "202
\n",
28 | "3003
\n",
29 | "40004"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "i=1\n",
40 | "print(1)\n",
41 | "while i<=n-1:\n",
42 | " print(i,end=\"\")\n",
43 | " num_2=2\n",
44 | " while num_2<=i: #no of Colums\n",
45 | " print(\"0\",end=\"\")\n",
46 | " num_2+=1\n",
47 | " print(i)\n",
48 | " i=i+1"
49 | ]
50 | }
51 | ],
52 | "metadata": {
53 | "kernelspec": {
54 | "display_name": "Python 3",
55 | "language": "python",
56 | "name": "python3"
57 | },
58 | "language_info": {
59 | "codemirror_mode": {
60 | "name": "ipython",
61 | "version": 3
62 | },
63 | "file_extension": ".py",
64 | "mimetype": "text/x-python",
65 | "name": "python",
66 | "nbconvert_exporter": "python",
67 | "pygments_lexer": "ipython3",
68 | "version": "3.8.6"
69 | }
70 | },
71 | "nbformat": 4,
72 | "nbformat_minor": 4
73 | }
74 |
--------------------------------------------------------------------------------
/3. Pattern 1/Number Pattern 3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Number Pattern 3\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1
\n",
12 | "11
\n",
13 | "121
\n",
14 | "1221
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Sample Input :
\n",
23 | "5
\n",
24 | "Sample Output :
\n",
25 | "1
\n",
26 | "11
\n",
27 | "121
\n",
28 | "1221
\n",
29 | "12221"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "i=1\n",
40 | "print(1)\n",
41 | "while i<=n-1:\n",
42 | " print(\"1\",end=\"\")\n",
43 | " num_2=2\n",
44 | " while num_2<=i: #no of Colums\n",
45 | " print(\"2\",end=\"\")\n",
46 | " num_2+=1\n",
47 | " print(\"1\")\n",
48 | " i=i+1"
49 | ]
50 | }
51 | ],
52 | "metadata": {
53 | "kernelspec": {
54 | "display_name": "Python 3",
55 | "language": "python",
56 | "name": "python3"
57 | },
58 | "language_info": {
59 | "codemirror_mode": {
60 | "name": "ipython",
61 | "version": 3
62 | },
63 | "file_extension": ".py",
64 | "mimetype": "text/x-python",
65 | "name": "python",
66 | "nbconvert_exporter": "python",
67 | "pygments_lexer": "ipython3",
68 | "version": "3.8.6"
69 | }
70 | },
71 | "nbformat": 4,
72 | "nbformat_minor": 4
73 | }
74 |
--------------------------------------------------------------------------------
/3. Pattern 1/Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Number Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1234
\n",
12 | "123
\n",
13 | "12
\n",
14 | "1
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Sample Input :
\n",
23 | "5
\n",
24 | "Sample Output :
\n",
25 | "12345
\n",
26 | "1234
\n",
27 | "123
\n",
28 | "12
\n",
29 | "1"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "i=0\n",
40 | "while(iAssume N is always odd.\n",
10 | "
Note : There is space after every star.\n",
11 | "
Pattern for N = 7
\n",
12 | "\n",
13 | "[](https://imgpress.xyz/image/iegGX)
\n",
14 | "Note-Dote(.) represent the spaces
\n",
15 | "Input format :
\n",
16 | "Integer N (Total no. of rows)
\n",
17 | "
\n",
18 | "Output format :
\n",
19 | "Pattern in N lines
\n",
20 | "
\n",
21 | "Sample Input :
\n",
22 | "11
\n",
23 | "Sample Output :
\n",
24 | "[](https://imgpress.xyz/image/iehrw)"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "n = int(input())\n",
34 | "i = 1\n",
35 | "n1 = (n - 1) / 2\n",
36 | "n = (n + 1) / 2\n",
37 | "while i <= n:\n",
38 | " spaces = 2\n",
39 | " while spaces <= i:\n",
40 | " print(\" \", end=\"\")\n",
41 | " spaces += 1\n",
42 | " star = 1\n",
43 | " while star <= i:\n",
44 | " print(\"* \", end=\"\")\n",
45 | " star += 1\n",
46 | " print(\"\")\n",
47 | " i = i + 1\n",
48 | "\n",
49 | "i = 1\n",
50 | "g = 0\n",
51 | "while i <= n1:\n",
52 | " num_1 = n1 - 1\n",
53 | " while num_1 >= i:\n",
54 | " print(\" \", end=\"\")\n",
55 | " num_1 -= 1\n",
56 | "\n",
57 | " num_1 = n1 - 1\n",
58 | " while num_1 >= g:\n",
59 | " print(\"* \", end=\"\")\n",
60 | " num_1 -= 1\n",
61 | " print()\n",
62 | " i = i + 1\n",
63 | " g = g + 1\n"
64 | ]
65 | }
66 | ],
67 | "metadata": {
68 | "kernelspec": {
69 | "display_name": "Python 3",
70 | "language": "python",
71 | "name": "python3"
72 | },
73 | "language_info": {
74 | "codemirror_mode": {
75 | "name": "ipython",
76 | "version": 3
77 | },
78 | "file_extension": ".py",
79 | "mimetype": "text/x-python",
80 | "name": "python",
81 | "nbconvert_exporter": "python",
82 | "pygments_lexer": "ipython3",
83 | "version": "3.8.6"
84 | }
85 | },
86 | "nbformat": 4,
87 | "nbformat_minor": 4
88 | }
89 |
--------------------------------------------------------------------------------
/4.Pattern 2/Code Diamond of stars.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Diamond of stars\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "Note: N is always odd.
\n",
10 | "Pattern for N = 5
\n",
11 | "
\n",
12 | "The dots represent spaces.
\n",
13 | "Input format :
\n",
14 | "N (Total no. of rows and can only be odd)
\n",
15 | "
\n",
16 | "Output format :
\n",
17 | "Pattern in N lines
\n",
18 | "
\n",
19 | "Constraints :
\n",
20 | "1 <= N <= 49
\n",
21 | "
\n",
22 | "Sample Input 1:
\n",
23 | "3
\n",
24 | "Sample Output 1:
\n",
25 | "
\n",
26 | "Sample Input 2:
\n",
27 | "5
\n",
28 | "Sample Output 2:
\n",
29 | ""
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "firstHalf=(n+1)//2\n",
40 | "secHalf=(n)//2\n",
41 | "\n",
42 | "#First Half\n",
43 | "currRow=1\n",
44 | "while currRow<=firstHalf:\n",
45 | " spaces=1\n",
46 | " while spaces<=(firstHalf-currRow):\n",
47 | " print(' ',end=\"\")\n",
48 | " spaces+=1\n",
49 | " currcol=1\n",
50 | " while currcol<=(2*currRow)-1:\n",
51 | " print(\"*\",end=\"\")\n",
52 | " currcol+=1\n",
53 | " print()\n",
54 | " currRow+=1\n",
55 | "\n",
56 | "#Secound Half\n",
57 | "currRow=secHalf\n",
58 | "while currRow>=1:\n",
59 | " spaces=1\n",
60 | " while spaces<=(secHalf-currRow+1):\n",
61 | " print(\" \",end=\"\")\n",
62 | " spaces+=1\n",
63 | " currcol=1\n",
64 | " while currcol<=(2*currRow)-1:\n",
65 | " print(\"*\",end=\"\")\n",
66 | " currcol+=1\n",
67 | " print()\n",
68 | " currRow-=1"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 1,
74 | "metadata": {},
75 | "outputs": [
76 | {
77 | "name": "stdout",
78 | "output_type": "stream",
79 | "text": [
80 | "9\n",
81 | " *\n",
82 | " ***\n",
83 | " *****\n",
84 | " *******\n",
85 | "*********\n",
86 | " *******\n",
87 | " *****\n",
88 | " ***\n",
89 | " *\n"
90 | ]
91 | }
92 | ],
93 | "source": [
94 | "#Another Solution\n",
95 | "n=int(input())\n",
96 | "first=n//2+1\n",
97 | "sec=n//2\n",
98 | "#first loop\n",
99 | "for i in range(1,first+1):\n",
100 | " print(\" \"*(first-i),end=\"\")\n",
101 | " print((2*i-1)*\"*\",end=\"\")\n",
102 | " print()\n",
103 | "#Second Loop\n",
104 | "for p in range(1,sec+1):\n",
105 | " print(\" \"*p,end=\"\")\n",
106 | " print((2*(sec-p+1)-1)*\"*\",end=\"\")\n",
107 | " print()"
108 | ]
109 | }
110 | ],
111 | "metadata": {
112 | "kernelspec": {
113 | "display_name": "Python 3",
114 | "language": "python",
115 | "name": "python3"
116 | },
117 | "language_info": {
118 | "codemirror_mode": {
119 | "name": "ipython",
120 | "version": 3
121 | },
122 | "file_extension": ".py",
123 | "mimetype": "text/x-python",
124 | "name": "python",
125 | "nbconvert_exporter": "python",
126 | "pygments_lexer": "ipython3",
127 | "version": "3.8.6"
128 | }
129 | },
130 | "nbformat": 4,
131 | "nbformat_minor": 4
132 | }
133 |
--------------------------------------------------------------------------------
/4.Pattern 2/Code Mirror Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Mirror Number Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "\n",
10 | "Pattern for N = 4
\n",
11 | "
\n",
12 | "The dots represent spaces.
\n",
13 | "Input format :
\n",
14 | "Integer N (Total no. of rows)
\n",
15 | "
\n",
16 | "Output format :
\n",
17 | "Pattern in N lines
\n",
18 | "
\n",
19 | "Constraints
\n",
20 | "0 <= N <= 50
\n",
21 | "
\n",
22 | "Sample Input 1:
\n",
23 | "3
\n",
24 | "Sample Output 1:
\n",
25 | "
\n",
26 | "Sample Input 2:
\n",
27 | "4
\n",
28 | "Sample Output 2:
\n",
29 | "
"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 2,
35 | "metadata": {},
36 | "outputs": [
37 | {
38 | "name": "stdout",
39 | "output_type": "stream",
40 | "text": [
41 | "4\n",
42 | " 1\n",
43 | " 12\n",
44 | " 123\n",
45 | "1234\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "n=int(input())\n",
51 | "currRow=1\n",
52 | "while currRow<=n:\n",
53 | " currCol=1\n",
54 | " spaces=1\n",
55 | " while spaces<=n-currRow:\n",
56 | " print(\" \",end=\"\")\n",
57 | " spaces+=1\n",
58 | " while currCol<=currRow:\n",
59 | " print(currCol,end=\"\")\n",
60 | " currCol+=1\n",
61 | " print()\n",
62 | " currRow+=1"
63 | ]
64 | }
65 | ],
66 | "metadata": {
67 | "kernelspec": {
68 | "display_name": "Python 3",
69 | "language": "python",
70 | "name": "python3"
71 | },
72 | "language_info": {
73 | "codemirror_mode": {
74 | "name": "ipython",
75 | "version": 3
76 | },
77 | "file_extension": ".py",
78 | "mimetype": "text/x-python",
79 | "name": "python",
80 | "nbconvert_exporter": "python",
81 | "pygments_lexer": "ipython3",
82 | "version": "3.8.6"
83 | }
84 | },
85 | "nbformat": 4,
86 | "nbformat_minor": 4
87 | }
88 |
--------------------------------------------------------------------------------
/4.Pattern 2/Code Star Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Star Pattern\n",
8 | "Print the following pattern
\n",
9 | "Pattern for N = 4
\n",
10 | "
\n",
11 | "The dots represent spaces.
\n",
12 | "Input Format :
\n",
13 | "N (Total no. of rows)
\n",
14 | "
\n",
15 | "Output Format :
\n",
16 | "Pattern in N lines
\n",
17 | "
\n",
18 | "Constraints :
\n",
19 | "0 <= N <= 50
\n",
20 | "
\n",
21 | "Sample Input 1 :
\n",
22 | "3
\n",
23 | "Sample Output 1 :
\n",
24 | "
\n",
25 | "Note .The dots represent spaces.
\n",
26 | "Sample Input 2 :
\n",
27 | "4
\n",
28 | "Sample Output 2 :
\n",
29 | "
\n",
30 | "Note .The dots represent spaces."
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": null,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "n=int(input())\n",
40 | "currRow=1\n",
41 | "while currRow<=n:\n",
42 | " spaces=1\n",
43 | " while spaces<=n-currRow:\n",
44 | " print(\" \",end=\"\")\n",
45 | " spaces+=1\n",
46 | " currCol=1\n",
47 | " while currCol<=(2*currRow)-1:\n",
48 | " print(\"*\",end=\"\")\n",
49 | " currCol+=1\n",
50 | " print()\n",
51 | " currRow+=1"
52 | ]
53 | }
54 | ],
55 | "metadata": {
56 | "kernelspec": {
57 | "display_name": "Python 3",
58 | "language": "python",
59 | "name": "python3"
60 | },
61 | "language_info": {
62 | "codemirror_mode": {
63 | "name": "ipython",
64 | "version": 3
65 | },
66 | "file_extension": ".py",
67 | "mimetype": "text/x-python",
68 | "name": "python",
69 | "nbconvert_exporter": "python",
70 | "pygments_lexer": "ipython3",
71 | "version": "3.8.6"
72 | }
73 | },
74 | "nbformat": 4,
75 | "nbformat_minor": 4
76 | }
77 |
--------------------------------------------------------------------------------
/4.Pattern 2/Code Triangle of Numbers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Triangle of Numbers\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "Pattern for N = 4
\n",
10 | "
\n",
11 | "The dots represent spaces.
\n",
12 | "Input format :
\n",
13 | "Integer N (Total no. of rows)
\n",
14 | "
\n",
15 | "Output format :
\n",
16 | "Pattern in N lines
\n",
17 | "
\n",
18 | "Constraints :
\n",
19 | "0 <= N <= 50
\n",
20 | "
\n",
21 | "Sample Input 1:
\n",
22 | "5
\n",
23 | "Sample Output 1:
\n",
24 | "
\n",
25 | "Sample Input 2:
\n",
26 | "4
\n",
27 | "Sample Output 2:
\n",
28 | ""
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "n=int(input())\n",
38 | "currRow=1\n",
39 | "while currRow<=n:\n",
40 | " spaces=1\n",
41 | " while spaces<=n-currRow:\n",
42 | " print(\" \",end=\"\")\n",
43 | " spaces+=1\n",
44 | " currCol=1\n",
45 | " valToPrint=currRow\n",
46 | " while currCol<=currRow:\n",
47 | " print(valToPrint,end=\"\")\n",
48 | " valToPrint+=1\n",
49 | " currCol+=1\n",
50 | " currCol=1\n",
51 | " valToPrint=2*currRow-2\n",
52 | " while currCol<=currRow-1:\n",
53 | " print(valToPrint,end=\"\")\n",
54 | " valToPrint-=1\n",
55 | " currCol+=1\n",
56 | " print()\n",
57 | " currRow+=1"
58 | ]
59 | }
60 | ],
61 | "metadata": {
62 | "kernelspec": {
63 | "display_name": "Python 3",
64 | "language": "python",
65 | "name": "python3"
66 | },
67 | "language_info": {
68 | "codemirror_mode": {
69 | "name": "ipython",
70 | "version": 3
71 | },
72 | "file_extension": ".py",
73 | "mimetype": "text/x-python",
74 | "name": "python",
75 | "nbconvert_exporter": "python",
76 | "pygments_lexer": "ipython3",
77 | "version": "3.8.6"
78 | }
79 | },
80 | "nbformat": 4,
81 | "nbformat_minor": 4
82 | }
83 |
--------------------------------------------------------------------------------
/4.Pattern 2/Code Inverted Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code : Inverted Number Pattern\n",
8 | "Print the following pattern for the given N number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "4444
\n",
12 | "333
\n",
13 | "22
\n",
14 | "1
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "Integer N (Total no. of rows)
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "Pattern in N lines
\n",
21 | "
\n",
22 | "Constraints :
\n",
23 | "0 <= N <= 50
\n",
24 | "
\n",
25 | "Sample Input 1:
\n",
26 | "5
\n",
27 | "Sample Output 1:
\n",
28 | "55555
\n",
29 | "4444
\n",
30 | "333
\n",
31 | "22
\n",
32 | "1
\n",
33 | "Sample Input 2:
\n",
34 | "6
\n",
35 | "Sample Output 2:
\n",
36 | "666666
\n",
37 | "55555
\n",
38 | "4444
\n",
39 | "333
\n",
40 | "22
\n",
41 | "1"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": null,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "n=int(input())\n",
51 | "currRow=1\n",
52 | "while currRow<=n:\n",
53 | " currCol=1\n",
54 | " while currCol<=(n-currRow+1):\n",
55 | " print(n-currRow+1,end=\"\")\n",
56 | " currCol+=1\n",
57 | " print()\n",
58 | " currRow+=1"
59 | ]
60 | }
61 | ],
62 | "metadata": {
63 | "kernelspec": {
64 | "display_name": "Python 3",
65 | "language": "python",
66 | "name": "python3"
67 | },
68 | "language_info": {
69 | "codemirror_mode": {
70 | "name": "ipython",
71 | "version": 3
72 | },
73 | "file_extension": ".py",
74 | "mimetype": "text/x-python",
75 | "name": "python",
76 | "nbconvert_exporter": "python",
77 | "pygments_lexer": "ipython3",
78 | "version": "3.8.6"
79 | }
80 | },
81 | "nbformat": 4,
82 | "nbformat_minor": 4
83 | }
84 |
--------------------------------------------------------------------------------
/4.Pattern 2/Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Number Pattern\n",
8 | "Print the following pattern for n number of rows.
\n",
9 | "For eg. N = 5
\n",
10 | "
\n",
11 | "Sample Input 1 :
\n",
12 | "4
\n",
13 | "Sample Output 1 :
\n",
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": null,
20 | "metadata": {},
21 | "outputs": [],
22 | "source": [
23 | "n=int(input())\n",
24 | "totalspace=n*2-2\n",
25 | "row=1\n",
26 | "while(row<=n):\n",
27 | " column=1\n",
28 | " while column<=row:\n",
29 | " print(column,end='')\n",
30 | " column=column+1\n",
31 | " space=1\n",
32 | " while space<=totalspace:\n",
33 | " print(' ',end=\"\")\n",
34 | " space+=1\n",
35 | " totalspace-=2\n",
36 | " column=row\n",
37 | " while column>0:\n",
38 | " print(column,end='')\n",
39 | " column-=1\n",
40 | " row+=1\n",
41 | " print()"
42 | ]
43 | }
44 | ],
45 | "metadata": {
46 | "kernelspec": {
47 | "display_name": "Python 3",
48 | "language": "python",
49 | "name": "python3"
50 | },
51 | "language_info": {
52 | "codemirror_mode": {
53 | "name": "ipython",
54 | "version": 3
55 | },
56 | "file_extension": ".py",
57 | "mimetype": "text/x-python",
58 | "name": "python",
59 | "nbconvert_exporter": "python",
60 | "pygments_lexer": "ipython3",
61 | "version": "3.8.6"
62 | }
63 | },
64 | "nbformat": 4,
65 | "nbformat_minor": 4
66 | }
67 |
--------------------------------------------------------------------------------
/4.Pattern 2/Pyramid Number Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Pyramid Number Pattern\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "
\n",
12 | "Input format : N (Total no. of rows)
\n",
13 | "
\n",
14 | "Output format : Pattern in N lines
\n",
15 | "
\n",
16 | "Sample Input :
\n",
17 | "5
\n",
18 | "Sample Output :
\n",
19 | "
\n",
20 | "Note.The dots represent spaces."
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "n = int(input())\n",
30 | "num = 1\n",
31 | "gap = n - 1\n",
32 | "for j in range(1, n + 1):\n",
33 | " num = j\n",
34 | " for i in range(1, gap + 1):\n",
35 | " print(\" \", end=\"\")\n",
36 | " gap = gap - 1\n",
37 | "\n",
38 | " for i in range(1, j + 1):\n",
39 | " print(num, end=\"\")\n",
40 | " num-=1\n",
41 | " num=2\n",
42 | " for i in range(1, j):\n",
43 | " print(num, end=\"\")\n",
44 | " num+=1\n",
45 | "\n",
46 | " print()"
47 | ]
48 | }
49 | ],
50 | "metadata": {
51 | "kernelspec": {
52 | "display_name": "Python 3",
53 | "language": "python",
54 | "name": "python3"
55 | },
56 | "language_info": {
57 | "codemirror_mode": {
58 | "name": "ipython",
59 | "version": 3
60 | },
61 | "file_extension": ".py",
62 | "mimetype": "text/x-python",
63 | "name": "python",
64 | "nbconvert_exporter": "python",
65 | "pygments_lexer": "ipython3",
66 | "version": "3.8.6"
67 | }
68 | },
69 | "nbformat": 4,
70 | "nbformat_minor": 4
71 | }
72 |
--------------------------------------------------------------------------------
/4.Pattern 2/Zeros and Stars Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Zeros and Stars Pattern\n",
8 | "Print the following pattern
\n",
9 | "Pattern for N = 4
\n",
10 | "
\n",
11 | "Input Format : N (Total no. of rows)
\n",
12 | "
\n",
13 | "Output Format : Pattern in N lines
\n",
14 | "
\n",
15 | "Sample Input 1 :
\n",
16 | "3
\n",
17 | "Sample Output 1 :
\n",
18 | "
\n",
19 | "Sample Input 2 :
\n",
20 | "5
\n",
21 | "Sample Output 2 :
\n",
22 | ""
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": [
31 | "n=int(input())\n",
32 | "start=1\n",
33 | "end=2*n+1\n",
34 | "mid=n+1\n",
35 | "row=1\n",
36 | "while(row<=n):\n",
37 | " column=1\n",
38 | " while column<=(2*n+1):\n",
39 | " if column==start or column==end or column==mid:\n",
40 | " print(\"*\",end='')\n",
41 | " else:\n",
42 | " print(\"0\",end=\"\")\n",
43 | " column=column+1\n",
44 | " start=start+1\n",
45 | " end=end-1\n",
46 | " row=row+1\n",
47 | " print()"
48 | ]
49 | }
50 | ],
51 | "metadata": {
52 | "kernelspec": {
53 | "display_name": "Python 3",
54 | "language": "python",
55 | "name": "python3"
56 | },
57 | "language_info": {
58 | "codemirror_mode": {
59 | "name": "ipython",
60 | "version": 3
61 | },
62 | "file_extension": ".py",
63 | "mimetype": "text/x-python",
64 | "name": "python",
65 | "nbconvert_exporter": "python",
66 | "pygments_lexer": "ipython3",
67 | "version": "3.8.6"
68 | }
69 | },
70 | "nbformat": 4,
71 | "nbformat_minor": 4
72 | }
73 |
--------------------------------------------------------------------------------
/5.More on Loops/Binary Pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Binary Pattern\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "1111
\n",
12 | "000
\n",
13 | "11
\n",
14 | "0
\n",
15 | "Input format : N (Total no. of rows)
\n",
16 | "
\n",
17 | "Output format : Pattern in N lines
\n",
18 | "
\n",
19 | "Sample Input :
\n",
20 | "5
\n",
21 | "Sample Output :
\n",
22 | "11111
\n",
23 | "0000
\n",
24 | "111
\n",
25 | "00
\n",
26 | "1"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "n=int(input())\n",
36 | "for i in range(1,n+1):\n",
37 | " for l in range(n+2-2*i,0,-1):\n",
38 | " print(1,end=\"\")\n",
39 | " print()\n",
40 | " for j in range (n+1-2*i,0,-1):\n",
41 | " print(0,end=\"\")\n",
42 | " print()"
43 | ]
44 | }
45 | ],
46 | "metadata": {
47 | "kernelspec": {
48 | "display_name": "Python 3",
49 | "language": "python",
50 | "name": "python3"
51 | },
52 | "language_info": {
53 | "codemirror_mode": {
54 | "name": "ipython",
55 | "version": 3
56 | },
57 | "file_extension": ".py",
58 | "mimetype": "text/x-python",
59 | "name": "python",
60 | "nbconvert_exporter": "python",
61 | "pygments_lexer": "ipython3",
62 | "version": "3.8.6"
63 | }
64 | },
65 | "nbformat": 4,
66 | "nbformat_minor": 4
67 | }
68 |
--------------------------------------------------------------------------------
/5.More on Loops/Diamond of Stars.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Diamond of Stars\n",
8 | "Print the following pattern for the given number of rows.\n",
9 | "
Note: N is always odd.\n",
10 | "
Pattern for N = 5
\n",
11 | "
\n",
12 | "The dots represent spaces.
\n",
13 | "Input format :
\n",
14 | "N (Total no. of rows and can only be odd)
\n",
15 | "
\n",
16 | "Output format :
\n",
17 | "Pattern in N lines
\n",
18 | "
\n",
19 | "Constraints :
\n",
20 | "1 <= N <= 49
\n",
21 | "
"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "n=int(input())\n",
31 | "for i in range(1,n+1):\n",
32 | " for j in range(1,i):\n",
33 | " print(\" \",end=\"\")\n",
34 | " for l in range(i,n+1):\n",
35 | " print(l,end=\"\")\n",
36 | " print()\n",
37 | "for a in range(1,n):\n",
38 | " for s in range(n-1-a,0,-1):\n",
39 | " print(\" \",end=\"\")\n",
40 | " for k in range(n-a,n+1):\n",
41 | " print(k,end=\"\")\n",
42 | " print()"
43 | ]
44 | }
45 | ],
46 | "metadata": {
47 | "kernelspec": {
48 | "display_name": "Python 3",
49 | "language": "python",
50 | "name": "python3"
51 | },
52 | "language_info": {
53 | "codemirror_mode": {
54 | "name": "ipython",
55 | "version": 3
56 | },
57 | "file_extension": ".py",
58 | "mimetype": "text/x-python",
59 | "name": "python",
60 | "nbconvert_exporter": "python",
61 | "pygments_lexer": "ipython3",
62 | "version": "3.8.6"
63 | }
64 | },
65 | "nbformat": 4,
66 | "nbformat_minor": 4
67 | }
68 |
--------------------------------------------------------------------------------
/5.More on Loops/Print Number Pyramid.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Print Number Pyramid\n",
8 | "Print the following pattern for a given n.
\n",
9 | "
\n",
10 | "**Note - Dot Represent Spaces**
\n",
11 | "For eg. N = 6
\n",
12 | "\n",
13 | "
\n",
14 | "Sample Input 1 :
\n",
15 | "4
\n",
16 | "Sample Output 1 :
\n",
17 | ""
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "n=int(input())\n",
27 | "for i in range(1,n+1):\n",
28 | " for j in range(1,i):\n",
29 | " print(\" \",end=\"\")\n",
30 | " for l in range(i,n+1):\n",
31 | " print(l,end=\"\")\n",
32 | " print()\n",
33 | "for a in range(1,n):\n",
34 | " for s in range(n-1-a,0,-1):\n",
35 | " print(\" \",end=\"\")\n",
36 | " for k in range(n-a,n+1):\n",
37 | " print(k,end=\"\")\n",
38 | " print()"
39 | ]
40 | }
41 | ],
42 | "metadata": {
43 | "kernelspec": {
44 | "display_name": "Python 3",
45 | "language": "python",
46 | "name": "python3"
47 | },
48 | "language_info": {
49 | "codemirror_mode": {
50 | "name": "ipython",
51 | "version": 3
52 | },
53 | "file_extension": ".py",
54 | "mimetype": "text/x-python",
55 | "name": "python",
56 | "nbconvert_exporter": "python",
57 | "pygments_lexer": "ipython3",
58 | "version": "3.8.6"
59 | }
60 | },
61 | "nbformat": 4,
62 | "nbformat_minor": 4
63 | }
64 |
--------------------------------------------------------------------------------
/5.More on Loops/Print the pattern.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Print the pattern\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "Pattern for N = 5
\n",
10 | "
\n",
11 | "Input format : N (Total no. of rows)
\n",
12 | "
\n",
13 | "Output format : Pattern in N lines
\n",
14 | "
\n",
15 | "Sample Input :
\n",
16 | "4
\n",
17 | "Sample Output :
\n",
18 | "1 2 3 4
\n",
19 | "9 10 11 12
\n",
20 | "13 14 15 16
\n",
21 | "5 6 7 8"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "n=int(input())\n",
31 | "startvalue=1\n",
32 | "for i in range(1,n+1):\n",
33 | " for j in range(startvalue,startvalue+n):\n",
34 | " print(j,end=\" \")\n",
35 | " print()\n",
36 | " if i==(n+1)//2:\n",
37 | " if n%2!=0:\n",
38 | " startvalue=n*(n-2)+1\n",
39 | " else:\n",
40 | " startvalue=n*(n-1)+1\n",
41 | " elif i>(n+1)//2:\n",
42 | " startvalue=startvalue-2*n\n",
43 | " else:\n",
44 | " startvalue=startvalue+2*n"
45 | ]
46 | }
47 | ],
48 | "metadata": {
49 | "kernelspec": {
50 | "display_name": "Python 3",
51 | "language": "python",
52 | "name": "python3"
53 | },
54 | "language_info": {
55 | "codemirror_mode": {
56 | "name": "ipython",
57 | "version": 3
58 | },
59 | "file_extension": ".py",
60 | "mimetype": "text/x-python",
61 | "name": "python",
62 | "nbconvert_exporter": "python",
63 | "pygments_lexer": "ipython3",
64 | "version": "3.8.6"
65 | }
66 | },
67 | "nbformat": 4,
68 | "nbformat_minor": 4
69 | }
70 |
--------------------------------------------------------------------------------
/5.More on Loops/Rectangular numbers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Rectangular numbers\n",
8 | "Print the following pattern for the given number of rows.
\n",
9 | "
\n",
10 | "Pattern for N = 4
\n",
11 | "4444444
\n",
12 | "4333334
\n",
13 | "4322234
\n",
14 | "4321234
\n",
15 | "4322234
\n",
16 | "4333334
\n",
17 | "4444444
\n",
18 | "
\n",
19 | "Input format : N (Total no. of rows)
\n",
20 | "Output format : Pattern in N lines
\n",
21 | "
\n",
22 | "Sample Input :
\n",
23 | "3
\n",
24 | "Sample Output :
\n",
25 | "33333
\n",
26 | "32223
\n",
27 | "32123
\n",
28 | "32223
\n",
29 | "33333"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "pattern_1=n\n",
40 | "\n",
41 | "for i in range (1,n+1):\n",
42 | " pattern_3 = n\n",
43 | " for k in range(1,i):\n",
44 | " print(pattern_3,end=\"\")\n",
45 | " pattern_3-=1\n",
46 | " for j in range (1,2*n+2-2*i):\n",
47 | " print(pattern_1,end=\"\")\n",
48 | " pattern_1 -= 1\n",
49 | "\n",
50 | " for l in range (1,i):\n",
51 | " pattern_3+=1\n",
52 | " print(pattern_3,end=\"\")\n",
53 | " print(\"\")\n",
54 | "\n",
55 | "\n",
56 | "pattern_1=2\n",
57 | "\n",
58 | "for i in range (n,1,-1):\n",
59 | " pattern_3 = n\n",
60 | " for k in range(1,i):\n",
61 | " print(pattern_3,end=\"\")\n",
62 | " pattern_3-=1\n",
63 | " for j in range (1,2*n+2-2*i):\n",
64 | " print(pattern_1,end=\"\")\n",
65 | " pattern_1+= 1\n",
66 | "\n",
67 | " for l in range (1,i):\n",
68 | " pattern_3+=1\n",
69 | " print(pattern_3,end=\"\")\n",
70 | " print(\"\")"
71 | ]
72 | }
73 | ],
74 | "metadata": {
75 | "kernelspec": {
76 | "display_name": "Python 3",
77 | "language": "python",
78 | "name": "python3"
79 | },
80 | "language_info": {
81 | "codemirror_mode": {
82 | "name": "ipython",
83 | "version": 3
84 | },
85 | "file_extension": ".py",
86 | "mimetype": "text/x-python",
87 | "name": "python",
88 | "nbconvert_exporter": "python",
89 | "pygments_lexer": "ipython3",
90 | "version": "3.8.6"
91 | }
92 | },
93 | "nbformat": 4,
94 | "nbformat_minor": 4
95 | }
96 |
--------------------------------------------------------------------------------
/6.Functions/Check Armstrong.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Check Armstrong\n",
8 | "Write a Program to determine if the given number is Armstrong number or not. Print true if number is armstrong, otherwise print false.
\n",
9 | "
\n",
10 | "An Armstrong number is a number (with digits n) such that the sum of its digits raised to nth power is equal to the number itself.
\n",
11 | "
\n",
12 | "For example,
\n",
13 | "371, as 3^3 + 7^3 + 1^3 = 371
\n",
14 | "1634, as 1^4 + 6^4 + 3^4 + 4^4 = 1634
\n",
15 | "
\n",
16 | "Input Format :
\n",
17 | "Integer n
\n",
18 | "
\n",
19 | "Output Format :
\n",
20 | "true or false
\n",
21 | "
\n",
22 | "Sample Input 1 :
\n",
23 | "1
\n",
24 | "Sample Output 1 :
\n",
25 | "true
\n",
26 | "Sample Input 2 :
\n",
27 | "103
\n",
28 | "Sample Output 2 :
\n",
29 | "false"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "n=int(input())\n",
39 | "no_of_digits=0\n",
40 | "org_n=n\n",
41 | "orginal_no=n\n",
42 | "Armstrong=0\n",
43 | "while n>0:\n",
44 | " no_of_digits+=1\n",
45 | " n=n//10\n",
46 | "while org_n>0:\n",
47 | " Last_Digit = org_n % 10\n",
48 | " Armstrong = Armstrong + ((Last_Digit)**(no_of_digits))\n",
49 | " org_n = (org_n //10)\n",
50 | "if orginal_no==Armstrong:\n",
51 | " print(\"true\")\n",
52 | "else:\n",
53 | " print(\"false\")"
54 | ]
55 | }
56 | ],
57 | "metadata": {
58 | "kernelspec": {
59 | "display_name": "Python 3",
60 | "language": "python",
61 | "name": "python3"
62 | },
63 | "language_info": {
64 | "codemirror_mode": {
65 | "name": "ipython",
66 | "version": 3
67 | },
68 | "file_extension": ".py",
69 | "mimetype": "text/x-python",
70 | "name": "python",
71 | "nbconvert_exporter": "python",
72 | "pygments_lexer": "ipython3",
73 | "version": "3.8.6"
74 | }
75 | },
76 | "nbformat": 4,
77 | "nbformat_minor": 4
78 | }
79 |
--------------------------------------------------------------------------------
/6.Functions/Fahrenheit to Celsius Function.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Fahrenheit to Celsius Function\n",
8 | "Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), you need to convert all Fahrenheit values from Start to End at the gap of W, into their corresponding Celsius values and print the table.
\n",
9 | "
\n",
10 | "Input Format :
\n",
11 | "3 integers - S, E and W respectively
\n",
12 | "
\n",
13 | "Output Format :
\n",
14 | "Fahrenheit to Celsius conversion table. One line for every Fahrenheit and Celsius Fahrenheit value. Fahrenheit value and its corresponding Celsius value should be separate by tab ("\\t")
\n",
15 | "
\n",
16 | "Constraints :
\n",
17 | "0 <= S <= 1000
\n",
18 | "0 <= E <= 1000
\n",
19 | "0 <= W <= 1000
\n",
20 | "
\n",
21 | "Sample Input 1:
\n",
22 | "0
\n",
23 | "100
\n",
24 | "20
\n",
25 | "Sample Output 1:
\n",
26 | "0 -17
\n",
27 | "20 -6
\n",
28 | "40 4
\n",
29 | "60 15
\n",
30 | "80 26
\n",
31 | "100 37
\n",
32 | "Sample Input 2:
\n",
33 | "120
\n",
34 | "200
\n",
35 | "40
\n",
36 | "Sample Output 2:
\n",
37 | "120 48
\n",
38 | "160 71
\n",
39 | "200 93
\n",
40 | "
\n",
41 | "Explanation for Sample Output 2 :
\n",
42 | "Start value is 120, end value is 200 and step size is 40. Therefore, the values we need to convert are 120, 120 + 40 = 160, and 160 + 40 = 200.
\n",
43 | "The formula for converting Fahrenheit to Celsius is:
\n",
44 | "Celsius Value = (5/9)*(Fahrenheit Value - 32)
\n",
45 | "Plugging 120 into the formula, the celsius value will be (5 / 9)*(120 - 32) => (5 / 9) * 88 => (5 * 88) / 9 => 440 / 9 => 48.88
\n",
46 | "But we'll only print 48 because we are only interested in the integral part of the value."
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "def printTable(start,end,step):\n",
56 | " while start <= end:\n",
57 | " Fahrenheit_to_Celsius = (((start - 32) * 5) / 9)\n",
58 | " print(start, int(Fahrenheit_to_Celsius))\n",
59 | " start = start + step\n",
60 | " \n",
61 | "s = int(input())\n",
62 | "e = int(input())\n",
63 | "step = int(input())\n",
64 | "printTable(s,e,step)"
65 | ]
66 | }
67 | ],
68 | "metadata": {
69 | "kernelspec": {
70 | "display_name": "Python 3",
71 | "language": "python",
72 | "name": "python3"
73 | },
74 | "language_info": {
75 | "codemirror_mode": {
76 | "name": "ipython",
77 | "version": 3
78 | },
79 | "file_extension": ".py",
80 | "mimetype": "text/x-python",
81 | "name": "python",
82 | "nbconvert_exporter": "python",
83 | "pygments_lexer": "ipython3",
84 | "version": "3.8.6"
85 | }
86 | },
87 | "nbformat": 4,
88 | "nbformat_minor": 4
89 | }
90 |
--------------------------------------------------------------------------------
/6.Functions/Fibonacci Member.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Fibonacci Member\n",
8 | "Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false.
\n",
9 | "
\n",
10 | "Fibonacci Series is defined by the recurrence
\n",
11 | " F(n) = F(n-1) + F(n-2)
\n",
12 | "where F(0) = 0 and F(1) = 1
\n",
13 | "
\n",
14 | "
\n",
15 | "Input Format :
\n",
16 | "Integer N
\n",
17 | "
\n",
18 | "Output Format :
\n",
19 | "true or false
\n",
20 | "
\n",
21 | "Constraints :
\n",
22 | "0 <= n <= 10^4
\n",
23 | "
\n",
24 | "Sample Input 1 :
\n",
25 | "5
\n",
26 | "Sample Output 1 :
\n",
27 | "true
\n",
28 | "Sample Input 2 :
\n",
29 | "14
\n",
30 | "Sample Output 2 :
\n",
31 | "false"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": null,
37 | "metadata": {},
38 | "outputs": [],
39 | "source": [
40 | "#Python Code\n",
41 | "import math \n",
42 | "def isPerfectSquare(x): \n",
43 | " s = int(math.sqrt(x)) \n",
44 | " return s*s == x \n",
45 | "def isFibonacci(n): \n",
46 | " return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)\n",
47 | "\n",
48 | "n=int(input())\n",
49 | "if (isFibonacci(n)==True):\n",
50 | " print(\"true\")\n",
51 | "else:\n",
52 | " print(\"false\")"
53 | ]
54 | }
55 | ],
56 | "metadata": {
57 | "kernelspec": {
58 | "display_name": "Python 3",
59 | "language": "python",
60 | "name": "python3"
61 | },
62 | "language_info": {
63 | "codemirror_mode": {
64 | "name": "ipython",
65 | "version": 3
66 | },
67 | "file_extension": ".py",
68 | "mimetype": "text/x-python",
69 | "name": "python",
70 | "nbconvert_exporter": "python",
71 | "pygments_lexer": "ipython3",
72 | "version": "3.8.6"
73 | }
74 | },
75 | "nbformat": 4,
76 | "nbformat_minor": 4
77 | }
78 |
--------------------------------------------------------------------------------
/6.Functions/Palindrome number.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Palindrome number\n",
8 | "Write a program to determine if given number is palindrome or not. Print true if it is palindrome, false otherwise.
\n",
9 | "
\n",
10 | "Palindrome are the numbers for which reverse is exactly same as the original one. For eg. 121
\n",
11 | "
\n",
12 | "Sample Input 1 :
\n",
13 | "121
\n",
14 | "Sample Output 1 :
\n",
15 | "true
\n",
16 | "Sample Input 2 :
\n",
17 | "1032
\n",
18 | "Sample Output 2 :
\n",
19 | "false"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "def checkPalindrome(n):\n",
29 | " Revese = 0\n",
30 | " while n > 0:\n",
31 | " Remainder = n % 10\n",
32 | " n = int(n / 10)\n",
33 | " Revese = Revese * 10 + Remainder\n",
34 | " return Revese\n",
35 | "\n",
36 | "\n",
37 | "num = int(input())\n",
38 | "Rev=checkPalindrome(num)\n",
39 | "if (num==Rev):\n",
40 | " print('true')\n",
41 | "else:\n",
42 | " print('false')"
43 | ]
44 | }
45 | ],
46 | "metadata": {
47 | "kernelspec": {
48 | "display_name": "Python 3",
49 | "language": "python",
50 | "name": "python3"
51 | },
52 | "language_info": {
53 | "codemirror_mode": {
54 | "name": "ipython",
55 | "version": 3
56 | },
57 | "file_extension": ".py",
58 | "mimetype": "text/x-python",
59 | "name": "python",
60 | "nbconvert_exporter": "python",
61 | "pygments_lexer": "ipython3",
62 | "version": "3.8.6"
63 | }
64 | },
65 | "nbformat": 4,
66 | "nbformat_minor": 4
67 | }
68 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Array Intersection.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Array Intersection\n",
8 | "You have been given two integer arrays/list(ARR1 and ARR2) of size M and N, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.
\n",
9 | "
\n",
10 | "Note :
\n",
11 | "Input arrays/lists can contain duplicate elements.
\n",
12 | "
\n",
13 | "The intersection elements printed would be in the order they appear in the first array/list(ARR1)
\n",
14 | "
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
18 | "
\n",
19 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.
\n",
20 | "
\n",
21 | "Second line contains 'N' single space separated integers representing the elements of the first the array/list.
\n",
22 | "
\n",
23 | "Third line contains an integer 'M' representing the size of the second array/list.
\n",
24 | "
\n",
25 | "Fourth line contains 'M' single space separated integers representing the elements of the second array/list.
\n",
26 | "
\n",
27 | "Output format :
\n",
28 | "For each test case, print the intersection elements in a row, separated by a single space.
\n",
29 | "
\n",
30 | "Output for every test case will be printed in a separate line.
\n",
31 | "
\n",
32 | "Constraints :
\n",
33 | "1 <= t <= 10^2
\n",
34 | "0 <= N <= 10^5
\n",
35 | "0 <= M <= 10^5
\n",
36 | "Time Limit: 1 sec
\n",
37 | "
\n",
38 | "Sample Input 1 :
\n",
39 | "2
\n",
40 | "6
\n",
41 | "2 6 8 5 4 3
\n",
42 | "4
\n",
43 | "2 3 4 7
\n",
44 | "2
\n",
45 | "10 10
\n",
46 | "1
\n",
47 | "10
\n",
48 | "Sample Output 1 :
\n",
49 | "2 4 3
\n",
50 | "10
\n",
51 | "Sample Input 2 :
\n",
52 | "1
\n",
53 | "4
\n",
54 | "2 6 1 2
\n",
55 | "5
\n",
56 | "1 2 3 4 2
\n",
57 | "
\n",
58 | "Sample Output 2 :
\n",
59 | "2 1 2
\n",
60 | "Explanation for Sample Output 2 :
\n",
61 | "Since, both input arrays have two '2's, the intersection of the arrays also have two '2's. The first '2' of first array matches with the first '2' of the second array. Similarly, the second '2' of the first array matches with the second '2' if the second array.
\n",
62 | "Note there is some Problem in running this code in jupyter notebook Run in Pycharm it will fine\n",
63 | "
or you copy paste directly in CodingNinjas"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {},
70 | "outputs": [],
71 | "source": [
72 | "import sys\n",
73 | "\n",
74 | "def intersections(arr1, n, arr2, m):\n",
75 | " for i in range(n):\n",
76 | " for j in range(m):\n",
77 | " if arr1[i] == arr2[j]:\n",
78 | " print(arr1[i],end =\" \")\n",
79 | " arr2[j] = sys.maxsize\n",
80 | " break\n",
81 | " \n",
82 | "\n",
83 | "def takeInput() :\n",
84 | " n = int(sys.stdin.readline().strip())\n",
85 | "\n",
86 | " if n == 0 :\n",
87 | " return list(), 0\n",
88 | "\n",
89 | " arr = list(map(int, sys.stdin.readline().strip().split(\" \")))\n",
90 | " return arr, n\n",
91 | "\n",
92 | "\n",
93 | "\n",
94 | "#main\n",
95 | "t = int(sys.stdin.readline().strip())\n",
96 | "\n",
97 | "while t > 0 :\n",
98 | " arr1 , n = takeInput()\n",
99 | " arr2 , m = takeInput()\n",
100 | " intersections(arr1, n, arr2, m)\n",
101 | " print()\n",
102 | " \n",
103 | " t -= 1"
104 | ]
105 | }
106 | ],
107 | "metadata": {
108 | "kernelspec": {
109 | "display_name": "Python 3",
110 | "language": "python",
111 | "name": "python3"
112 | },
113 | "language_info": {
114 | "codemirror_mode": {
115 | "name": "ipython",
116 | "version": 3
117 | },
118 | "file_extension": ".py",
119 | "mimetype": "text/x-python",
120 | "name": "python",
121 | "nbconvert_exporter": "python",
122 | "pygments_lexer": "ipython3",
123 | "version": "3.8.6"
124 | }
125 | },
126 | "nbformat": 4,
127 | "nbformat_minor": 4
128 | }
129 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Array Sum.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Array Sum\n",
8 | "Given an array of length N, you need to find and print the sum of all elements of the array.
\n",
9 | "
\n",
10 | "Input Format :
\n",
11 | "Line 1 : An Integer N i.e. size of array
\n",
12 | "Line 2 : N integers which are elements of the array, separated by spaces
\n",
13 | "
\n",
14 | "Output Format :
\n",
15 | "Sum
\n",
16 | "
\n",
17 | "Constraints :
\n",
18 | "1 <= N <= 10^6
\n",
19 | "
\n",
20 | "Sample Input :
\n",
21 | "3
\n",
22 | "9 8 9
\n",
23 | "Sample Output :
\n",
24 | "26"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "p=input()\n",
34 | "arr = input().split()\n",
35 | "sum = 0\n",
36 | "for x in range(len(arr)):\n",
37 | " p=int(arr[x])\n",
38 | " sum=sum+p\n",
39 | "print(sum)"
40 | ]
41 | }
42 | ],
43 | "metadata": {
44 | "kernelspec": {
45 | "display_name": "Python 3",
46 | "language": "python",
47 | "name": "python3"
48 | },
49 | "language_info": {
50 | "codemirror_mode": {
51 | "name": "ipython",
52 | "version": 3
53 | },
54 | "file_extension": ".py",
55 | "mimetype": "text/x-python",
56 | "name": "python",
57 | "nbconvert_exporter": "python",
58 | "pygments_lexer": "ipython3",
59 | "version": "3.8.6"
60 | }
61 | },
62 | "nbformat": 4,
63 | "nbformat_minor": 4
64 | }
65 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Find Duplicate.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Find Duplicate\n",
8 | "You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array.
\n",
9 | "
\n",
10 | "Note :
\n",
11 | "Duplicate number is always present in the given array/list.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
17 | "
\n",
18 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
19 | "Output Format :
\n",
20 | "For each test case, print the duplicate element in the array/list.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a separate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^3
\n",
27 | "Time Limit: 1 sec
\n",
28 | "
\n",
29 | "Sample Input 1:
\n",
30 | "1
\n",
31 | "9
\n",
32 | "0 7 2 5 4 7 1 3 6
\n",
33 | "Sample Output 1:
\n",
34 | "7
\n",
35 | "Sample Input 2:
\n",
36 | "2
\n",
37 | "5
\n",
38 | "0 2 1 3 1
\n",
39 | "7
\n",
40 | "0 3 1 5 4 3 2
\n",
41 | "Sample Output 2:
\n",
42 | "1
\n",
43 | "3"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "N = int(input())\n",
53 | "k=0\n",
54 | "while k\n",
9 | "
\n",
10 | "Note:
\n",
11 | "Given array/list can contain duplicate elements.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.
\n",
17 | "
\n",
18 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
19 | "
\n",
20 | "Third line contains an integer 'X'.
\n",
21 | "
\n",
22 | "Output format :
\n",
23 | "For each test case, print the total number of pairs present in the array/list.
\n",
24 | "
\n",
25 | "Output for every test case will be printed in a separate line.
\n",
26 | "
\n",
27 | "Constraints :
\n",
28 | "1 <= t <= 10^2
\n",
29 | "0 <= N <= 10^3
\n",
30 | "0 <= X <= 10^9
\n",
31 | "Time Limit: 1 sec
\n",
32 | "
\n",
33 | "Sample Input 1:
\n",
34 | "1
\n",
35 | "9
\n",
36 | "1 3 6 2 5 4 3 2 4
\n",
37 | "7
\n",
38 | "Sample Output 1:
\n",
39 | "7
\n",
40 | "Sample Input 2:
\n",
41 | "2
\n",
42 | "9
\n",
43 | "1 3 6 2 5 4 3 2 4
\n",
44 | "12
\n",
45 | "6
\n",
46 | "2 8 10 5 -2 5
\n",
47 | "10
\n",
48 | "Sample Output 2:
\n",
49 | "0
\n",
50 | "2
\n",
51 | "Explanation for Input 2:
\n",
52 | "Since there doesn't exist any pair with sum equal to 12 for the first query, we print 0.
\n",
53 | "For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5)."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {},
60 | "outputs": [],
61 | "source": [
62 | "def duplicate (list1,sum):\n",
63 | " sum_time=0\n",
64 | " size=len(list1)\n",
65 | " for i in range(0,size):\n",
66 | " for j in range(i+1,size):\n",
67 | " if list1[i] + list1[j]==sum:\n",
68 | " sum_time+=1\n",
69 | " else:\n",
70 | " continue\n",
71 | " print(sum_time)\n",
72 | "\n",
73 | "n=int(input()) #First Input\n",
74 | "for i in range(n):\n",
75 | " faltu1=int(input()) #Secound input #no of ele in list\n",
76 | " if faltu1!=0:\n",
77 | " list = [int(x) for x in input().split()]\n",
78 | " X=int(input()) #3rd Input\n",
79 | " if faltu1!=0 :\n",
80 | " duplicate(list,X)\n",
81 | " else:\n",
82 | " print(0)"
83 | ]
84 | }
85 | ],
86 | "metadata": {
87 | "kernelspec": {
88 | "display_name": "Python 3",
89 | "language": "python",
90 | "name": "python3"
91 | },
92 | "language_info": {
93 | "codemirror_mode": {
94 | "name": "ipython",
95 | "version": 3
96 | },
97 | "file_extension": ".py",
98 | "mimetype": "text/x-python",
99 | "name": "python",
100 | "nbconvert_exporter": "python",
101 | "pygments_lexer": "ipython3",
102 | "version": "3.8.6"
103 | }
104 | },
105 | "nbformat": 4,
106 | "nbformat_minor": 4
107 | }
108 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Sort 0 1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sort 0 1\n",
8 | "You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list.
\n",
9 | "
\n",
10 | "Note:
\n",
11 | "You need to change in the given array/list itself. Hence, no need to return or print anything.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
17 | "
\n",
18 | "Second line contains 'N' single space separated integers(all 0s and 1s) representing the elements in the array/list.
\n",
19 | "
\n",
20 | "Output format :
\n",
21 | "For each test case, print the sorted array/list elements in a row separated by a single space.
\n",
22 | "
\n",
23 | "Output for every test case will be printed in a separate line.
\n",
24 | "
\n",
25 | "Constraints :
\n",
26 | "1 <= t <= 10^2
\n",
27 | "0 <= N <= 10^5
\n",
28 | "Time Limit: 1 sec
\n",
29 | "Sample Input 1:
\n",
30 | "1
\n",
31 | "7
\n",
32 | "0 1 1 0 1 0 1
\n",
33 | "Sample Output 1:
\n",
34 | "0 0 0 1 1 1 1
\n",
35 | "Sample Input 2:
\n",
36 | "2
\n",
37 | "8
\n",
38 | "1 0 1 1 0 1 0 1
\n",
39 | "5
\n",
40 | "0 1 0 1 0
\n",
41 | "Sample Output 2:
\n",
42 | "0 0 0 1 1 1 1 1
\n",
43 | "0 0 0 1 1"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "n=int(input())\n",
53 | "for i in range(n):\n",
54 | " faltu=(input())\n",
55 | " list1=[int(x) for x in input().split()]\n",
56 | " sort_list1=sorted(list1)\n",
57 | " for j in range(len(sort_list1)):\n",
58 | " print(sort_list1[j],end=\" \")\n",
59 | " print()"
60 | ]
61 | }
62 | ],
63 | "metadata": {
64 | "kernelspec": {
65 | "display_name": "Python 3",
66 | "language": "python",
67 | "name": "python3"
68 | },
69 | "language_info": {
70 | "codemirror_mode": {
71 | "name": "ipython",
72 | "version": 3
73 | },
74 | "file_extension": ".py",
75 | "mimetype": "text/x-python",
76 | "name": "python",
77 | "nbconvert_exporter": "python",
78 | "pygments_lexer": "ipython3",
79 | "version": "3.8.6"
80 | }
81 | },
82 | "nbformat": 4,
83 | "nbformat_minor": 4
84 | }
85 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Swap Alternate.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Swap Alternate\n",
8 | "You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
\n",
9 | "
\n",
10 | "You don't need to print or return anything, just change in the input array itself.
\n",
11 | "
\n",
12 | "Input Format :
\n",
13 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
14 | "
\n",
15 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
16 | "
\n",
17 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
18 | "
\n",
19 | "Output Format :
\n",
20 | "For each test case, print the elements of the resulting array in a single row separated by a single space.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a separate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^5
\n",
27 | "Time Limit: 1sec
\n",
28 | "
\n",
29 | "Sample Input 1:
\n",
30 | "1
\n",
31 | "6
\n",
32 | "9 3 6 12 4 32
\n",
33 | "Sample Output 1 :
\n",
34 | "3 9 12 6 32 4
\n",
35 | "Sample Input 2:
\n",
36 | "2
\n",
37 | "9
\n",
38 | "9 3 6 12 4 32 5 11 19
\n",
39 | "4
\n",
40 | "1 2 3 4
\n",
41 | "Sample Output 2 :
\n",
42 | "3 9 12 6 32 4 11 5 19
\n",
43 | "2 1 4 3"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "def swapAlternate (li):\n",
53 | " length =len(li)\n",
54 | " i = 0\n",
55 | " for k in range(length//2):\n",
56 | " li[i],li[i+1]=li[i+1],li[i]\n",
57 | " i+=2\n",
58 | " print(*li,end=\" \")\n",
59 | " print()\n",
60 | "\n",
61 | "loop_cycle=int(input())\n",
62 | "for i in range(0, loop_cycle):\n",
63 | " faltu = input()\n",
64 | " lip = [int(x) for x in input().split()]\n",
65 | " swapAlternate(lip)"
66 | ]
67 | }
68 | ],
69 | "metadata": {
70 | "kernelspec": {
71 | "display_name": "Python 3",
72 | "language": "python",
73 | "name": "python3"
74 | },
75 | "language_info": {
76 | "codemirror_mode": {
77 | "name": "ipython",
78 | "version": 3
79 | },
80 | "file_extension": ".py",
81 | "mimetype": "text/x-python",
82 | "name": "python",
83 | "nbconvert_exporter": "python",
84 | "pygments_lexer": "ipython3",
85 | "version": "3.8.6"
86 | }
87 | },
88 | "nbformat": 4,
89 | "nbformat_minor": 4
90 | }
91 |
--------------------------------------------------------------------------------
/7.Arrays & Lists/Triplet Sum.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Triplet Sum\n",
8 | "You have been given a random integer array/list(ARR) and a number X. Find and return the triplet(s) in the array/list which sum to X.
\n",
9 | "
\n",
10 | "Note :
\n",
11 | "Given array/list can contain duplicate elements.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.
\n",
17 | "
\n",
18 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
19 | "
\n",
20 | "Third line contains an integer 'X'.
\n",
21 | "
\n",
22 | "Output format :
\n",
23 | "For each test case, print the total number of triplets present in the array/list.
\n",
24 | "
\n",
25 | "Output for every test case will be printed in a separate line.
\n",
26 | "
\n",
27 | "Constraints :
\n",
28 | "1 <= t <= 50
\n",
29 | "0 <= N <= 10^2
\n",
30 | "0 <= X <= 10^9
\n",
31 | "Time Limit: 1 sec
\n",
32 | "
\n",
33 | "Sample Input 1:
\n",
34 | "1
\n",
35 | "7
\n",
36 | "1 2 3 4 5 6 7
\n",
37 | "12
\n",
38 | "Sample Output 1:
\n",
39 | "5
\n",
40 | "Sample Input 2:
\n",
41 | "2
\n",
42 | "7
\n",
43 | "1 2 3 4 5 6 7
\n",
44 | "19
\n",
45 | "9
\n",
46 | "2 -5 8 -6 0 5 10 11 -3
\n",
47 | "10
\n",
48 | "Sample Output 2:
\n",
49 | "0
\n",
50 | "5
\n",
51 | "Explanation for Input 2:
\n",
52 | "Since there doesn't exist any triplet with sum equal to 19 for the first query, we print 0.
\n",
53 | "
\n",
54 | "For the second query, we have 5 triplets in total that sum up to 10. They are, (2, 8, 0), (2, 11, -3), (-5, 5, 10), (8, 5, -3) and (-6, 5, 11)"
55 | ]
56 | },
57 | {
58 | "cell_type": "code",
59 | "execution_count": null,
60 | "metadata": {},
61 | "outputs": [],
62 | "source": [
63 | "def triplesum (list1,sum):\n",
64 | " sum_time=0\n",
65 | " n=len(list1)\n",
66 | " for i in range(0,n):\n",
67 | " for j in range(i+1,n):\n",
68 | " for k in range(j+1,n):\n",
69 | " if list1[i] + list1[j]+ list1[k]==sum:\n",
70 | " sum_time+=1\n",
71 | " else:\n",
72 | " continue\n",
73 | " print(sum_time)\n",
74 | "\n",
75 | "n=int(input())\n",
76 | "for i in range(n):\n",
77 | " faltu1=int(input())\n",
78 | " if faltu1!=0:\n",
79 | " list = [int(x) for x in input().split()]\n",
80 | " X=int(input())\n",
81 | " if faltu1!=0 :\n",
82 | " triplesum(list,X)\n",
83 | " else:\n",
84 | " print(0)"
85 | ]
86 | }
87 | ],
88 | "metadata": {
89 | "kernelspec": {
90 | "display_name": "Python 3",
91 | "language": "python",
92 | "name": "python3"
93 | },
94 | "language_info": {
95 | "codemirror_mode": {
96 | "name": "ipython",
97 | "version": 3
98 | },
99 | "file_extension": ".py",
100 | "mimetype": "text/x-python",
101 | "name": "python",
102 | "nbconvert_exporter": "python",
103 | "pygments_lexer": "ipython3",
104 | "version": "3.8.6"
105 | }
106 | },
107 | "nbformat": 4,
108 | "nbformat_minor": 4
109 | }
110 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Check Array Rotation.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Check Array Rotation\n",
8 | "You have been given an integer array/list(ARR) of size N. It has been sorted(in increasing order) and then rotated by some number 'K' in the clockwise direction.
\n",
9 | "Your task is to write a function that returns the value of 'K', that means, the index from which the array/list has been rotated.
\n",
10 | "
\n",
11 | "Input format :
\n",
12 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
13 | "
\n",
14 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
15 | "
\n",
16 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
17 | "
\n",
18 | "Output Format :
\n",
19 | "For each test case, print the value of 'K' or the index from which which the array/list has been rotated.
\n",
20 | "
\n",
21 | "Output for every test case will be printed in a separate line.
\n",
22 | "
\n",
23 | "Constraints :
\n",
24 | "1 <= t <= 10^2
\n",
25 | "0 <= N <= 10^5
\n",
26 | "Time Limit: 1 sec
\n",
27 | "Sample Input 1:
\n",
28 | "1
\n",
29 | "6
\n",
30 | "5 6 1 2 3 4
\n",
31 | "Sample Output 1:
\n",
32 | "2
\n",
33 | "Sample Input 2:
\n",
34 | "2
\n",
35 | "5
\n",
36 | "3 6 8 9 10
\n",
37 | "4
\n",
38 | "10 20 30 1
\n",
39 | "Sample Output 2:
\n",
40 | "0
\n",
41 | "3"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": null,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "loop=int(input())\n",
51 | "list1=[]\n",
52 | "for i in range(loop):\n",
53 | " ele=int(input())\n",
54 | " if ele!=0:\n",
55 | " list1=[int(x) for x in input().split()]\n",
56 | " else:\n",
57 | " print(0)\n",
58 | " break\n",
59 | " list2=list1.copy()\n",
60 | " list2.sort()\n",
61 | " f1=list2[0]\n",
62 | " print(list1.index(f1))"
63 | ]
64 | }
65 | ],
66 | "metadata": {
67 | "kernelspec": {
68 | "display_name": "Python 3",
69 | "language": "python",
70 | "name": "python3"
71 | },
72 | "language_info": {
73 | "codemirror_mode": {
74 | "name": "ipython",
75 | "version": 3
76 | },
77 | "file_extension": ".py",
78 | "mimetype": "text/x-python",
79 | "name": "python",
80 | "nbconvert_exporter": "python",
81 | "pygments_lexer": "ipython3",
82 | "version": "3.8.6"
83 | }
84 | },
85 | "nbformat": 4,
86 | "nbformat_minor": 4
87 | }
88 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Code Binary Search.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code Binary Search\n",
8 | "You have been given a sorted(in ascending order) integer array/list(ARR) of size N and an element X. Write a function to search this element in the given input array/list using 'Binary Search'. Return the index of the element in the input array/list. In case the element is not present in the array/list, then return -1.
\n",
9 | "
\n",
10 | "Input format :
\n",
11 | "The first line contains an Integer 'N' which denotes the size of the array/list.
\n",
12 | "
\n",
13 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
14 | "
\n",
15 | "Third line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow..
\n",
16 | "
\n",
17 | "All the 't' lines henceforth, will take the value of X to be searched for in the array/list.
\n",
18 | "
\n",
19 | "Output Format :
\n",
20 | "For each test case, print the index at which X is present, -1 otherwise.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a separate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^4
\n",
26 | "0 <= N <= 10^6
\n",
27 | "0 <= X <= 10^9
\n",
28 | "Time Limit: 1 sec
\n",
29 | "Sample Input 1:
\n",
30 | "7
\n",
31 | "1 3 7 9 11 12 45
\n",
32 | "1
\n",
33 | "3
\n",
34 | "Sample Output 1:
\n",
35 | "1
\n",
36 | "Sample Input 2:
\n",
37 | "7
\n",
38 | "1 2 3 4 5 6 7
\n",
39 | "2
\n",
40 | "9
\n",
41 | "7
\n",
42 | "Sample Output 2:
\n",
43 | "-1
\n",
44 | "6"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": [
53 | "def binarysearch(arr, element):\n",
54 | " start = 0\n",
55 | " end = len(arr) - 1\n",
56 | " while start <= end:\n",
57 | " mid = (start + end) // 2\n",
58 | " if arr[mid] == element:\n",
59 | " return mid\n",
60 | " elif arr[mid] < element:\n",
61 | " start = mid + 1\n",
62 | " else:\n",
63 | " end = mid - 1\n",
64 | " return -1\n",
65 | "\n",
66 | "\n",
67 | "faltu =int(input())\n",
68 | "if faltu == 0:\n",
69 | " list = [0]\n",
70 | "else:\n",
71 | " list = [int(x) for x in input().split()]\n",
72 | "no_of_loop = int(input())\n",
73 | "for i in range(no_of_loop):\n",
74 | " search_element = int(input())\n",
75 | " print(binarysearch(list, search_element))"
76 | ]
77 | }
78 | ],
79 | "metadata": {
80 | "kernelspec": {
81 | "display_name": "Python 3",
82 | "language": "python",
83 | "name": "python3"
84 | },
85 | "language_info": {
86 | "codemirror_mode": {
87 | "name": "ipython",
88 | "version": 3
89 | },
90 | "file_extension": ".py",
91 | "mimetype": "text/x-python",
92 | "name": "python",
93 | "nbconvert_exporter": "python",
94 | "pygments_lexer": "ipython3",
95 | "version": "3.8.6"
96 | }
97 | },
98 | "nbformat": 4,
99 | "nbformat_minor": 4
100 | }
101 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Code Bubble Sort.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code Bubble Sort\n",
8 | "Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Bubble Sort'.
\n",
9 | "
\n",
10 | "Note:
\n",
11 | "Change in the input array/list itself. You don't need to return or print the elements.
\n",
12 | "Input format :
\n",
13 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
14 | "
\n",
15 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
16 | "
\n",
17 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "For each test case, print the elements of the array/list in sorted order separated by a single space.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a separate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^3
\n",
27 | "Time Limit: 1 sec
\n",
28 | "Sample Input 1:
\n",
29 | "1
\n",
30 | "7
\n",
31 | "2 13 4 1 3 6 28
\n",
32 | "Sample Output 1:
\n",
33 | "1 2 3 4 6 13 28
\n",
34 | "Sample Input 2:
\n",
35 | "2
\n",
36 | "5
\n",
37 | "9 3 6 2 0
\n",
38 | "4
\n",
39 | "4 3 2 1
\n",
40 | "Sample Output 2:
\n",
41 | "0 2 3 6 9
\n",
42 | "1 2 3 4"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "def bubblesort(list):\n",
52 | " length=len(list)\n",
53 | " for i in range(length-1):\n",
54 | " for j in range(length-i-1):\n",
55 | " if list[j]>list[j+1]:\n",
56 | " list[j],list[j+1]=list[j+1],list[j]\n",
57 | " for k in range(len(list)):\n",
58 | " print(list[k],end=\" \")\n",
59 | " print()\n",
60 | " \n",
61 | " \n",
62 | "loop=int(input())\n",
63 | "\n",
64 | "if n!=0:\n",
65 | " for p in range(loop):\n",
66 | " n=int(input())\n",
67 | " list = [int(x) for x in input().split()]\n",
68 | " bubblesort(list)"
69 | ]
70 | }
71 | ],
72 | "metadata": {
73 | "kernelspec": {
74 | "display_name": "Python 3",
75 | "language": "python",
76 | "name": "python3"
77 | },
78 | "language_info": {
79 | "codemirror_mode": {
80 | "name": "ipython",
81 | "version": 3
82 | },
83 | "file_extension": ".py",
84 | "mimetype": "text/x-python",
85 | "name": "python",
86 | "nbconvert_exporter": "python",
87 | "pygments_lexer": "ipython3",
88 | "version": "3.8.6"
89 | }
90 | },
91 | "nbformat": 4,
92 | "nbformat_minor": 4
93 | }
94 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Code Insertion Sort.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code Insertion Sort\n",
8 | "Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Insertion Sort'.
\n",
9 | " Note:
\n",
10 | "Change in the input array/list itself. You don't need to return or print the elements.
\n",
11 | "
\n",
12 | " Input format :
\n",
13 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
14 | "
\n",
15 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
16 | "
\n",
17 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
18 | "
\n",
19 | "Output Format :
\n",
20 | "For each test case, print the elements of the array/list in sorted order separated by a single space.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a separate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^3
\n",
27 | "Time Limit: 1 sec
\n",
28 | "Sample Input 1:
\n",
29 | "1
\n",
30 | "7
\n",
31 | "2 13 4 1 3 6 28
\n",
32 | "Sample Output 1:
\n",
33 | "1 2 3 4 6 13 28
\n",
34 | "Sample Input 2:
\n",
35 | "2
\n",
36 | "5
\n",
37 | "9 3 6 2 0
\n",
38 | "4
\n",
39 | "4 3 2 1
\n",
40 | "Sample Output 2:
\n",
41 | "0 2 3 6 9
\n",
42 | "1 2 3 4"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "def insertsort(arr):\n",
52 | " length=len(arr)\n",
53 | " for i in range(1,length):\n",
54 | " j=i-1\n",
55 | " temp=arr[i]\n",
56 | " while(j>=0 and arr[j]>temp):\n",
57 | " arr[j+1]=arr[j]\n",
58 | " j=j-1\n",
59 | " arr[j+1]=temp\n",
60 | "loop=int(input())\n",
61 | "arr=[]\n",
62 | "for p in range(loop):\n",
63 | " ele=int(input())\n",
64 | " if ele!=0:\n",
65 | " arr=[int(r) for r in input().split()]\n",
66 | " else:\n",
67 | " break\n",
68 | " insertsort(arr)\n",
69 | " print(*arr,\" \")"
70 | ]
71 | }
72 | ],
73 | "metadata": {
74 | "kernelspec": {
75 | "display_name": "Python 3",
76 | "language": "python",
77 | "name": "python3"
78 | },
79 | "language_info": {
80 | "codemirror_mode": {
81 | "name": "ipython",
82 | "version": 3
83 | },
84 | "file_extension": ".py",
85 | "mimetype": "text/x-python",
86 | "name": "python",
87 | "nbconvert_exporter": "python",
88 | "pygments_lexer": "ipython3",
89 | "version": "3.8.6"
90 | }
91 | },
92 | "nbformat": 4,
93 | "nbformat_minor": 4
94 | }
95 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Code Merge Two Sorted Arrays.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Code Merge Two Sorted Arrays\n",
8 | "You have been given two sorted arrays/lists(ARR1 and ARR2) of size N and M respectively, merge them into a third array/list such that the third array is also sorted.
\n",
9 | "
\n",
10 | "Input Format :
\n",
11 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
12 | "
\n",
13 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.
\n",
14 | "
\n",
15 | "Second line contains 'N' single space separated integers representing the elements of the first array/list.
\n",
16 | "
\n",
17 | "Third line contains an integer 'M' representing the size of the second array/list.
\n",
18 | "
\n",
19 | "Fourth line contains 'M' single space separated integers representing the elements of the second array/list.
\n",
20 | "
\n",
21 | "Output Format :
\n",
22 | "For each test case, print the sorted array/list(of size N + M) in a single row, separated by a single space.
\n",
23 | "
\n",
24 | "Output for every test case will be printed in a separate line.
\n",
25 | "
\n",
26 | "Constraints :
\n",
27 | "1 <= t <= 10^2
\n",
28 | "0 <= N <= 10^5
\n",
29 | "0 <= M <= 10^5
\n",
30 | "Time Limit: 1 sec
\n",
31 | "Sample Input 1 :
\n",
32 | "1
\n",
33 | "5
\n",
34 | "1 3 4 7 11
\n",
35 | "4
\n",
36 | "2 4 6 13
\n",
37 | "Sample Output 1 :
\n",
38 | "1 2 3 4 4 6 7 11 13
\n",
39 | "Sample Input 2 :
\n",
40 | "2
\n",
41 | "3
\n",
42 | "10 100 500
\n",
43 | "7
\n",
44 | "4 7 9 25 30 300 450
\n",
45 | "4
\n",
46 | "7 45 89 90
\n",
47 | "0
\n",
48 | "Sample Output 2 :
\n",
49 | "4 7 9 10 25 30 100 300 450 500
\n",
50 | "7 45 89 90"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "def mergesorted(arr1, arr2):\n",
60 | " i = 0\n",
61 | " j = 0\n",
62 | " len1 = len(arr1)\n",
63 | " len2 = len(arr2)\n",
64 | " arr = []\n",
65 | " while ((i < len1) and (j < len2)):\n",
66 | " if (arr1[i] < arr2[j]):\n",
67 | " arr.append(arr1[i])\n",
68 | " i += 1\n",
69 | " else:\n",
70 | " arr.append(arr2[j])\n",
71 | " j += 1\n",
72 | " while (i < len1):\n",
73 | " arr.append(arr1[i])\n",
74 | " i += 1\n",
75 | " while (j < len2):\n",
76 | " arr.append(arr2[j])\n",
77 | " j += 1\n",
78 | " return arr\n",
79 | "\n",
80 | "\n",
81 | "loop = int(input())\n",
82 | "for l in range(loop):\n",
83 | " arr1 = []\n",
84 | " arr2 = []\n",
85 | " ele1=int(input())\n",
86 | " if ele1!=0:\n",
87 | " arr1=[int(c) for c in input().split()]\n",
88 | " ele2=int(input())\n",
89 | " if ele2!=0:\n",
90 | " arr2=[int(v) for v in input().split()]\n",
91 | " arr=mergesorted(arr1,arr2)\n",
92 | " print(*arr)"
93 | ]
94 | }
95 | ],
96 | "metadata": {
97 | "kernelspec": {
98 | "display_name": "Python 3",
99 | "language": "python",
100 | "name": "python3"
101 | },
102 | "language_info": {
103 | "codemirror_mode": {
104 | "name": "ipython",
105 | "version": 3
106 | },
107 | "file_extension": ".py",
108 | "mimetype": "text/x-python",
109 | "name": "python",
110 | "nbconvert_exporter": "python",
111 | "pygments_lexer": "ipython3",
112 | "version": "3.8.6"
113 | }
114 | },
115 | "nbformat": 4,
116 | "nbformat_minor": 4
117 | }
118 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Push Zeros to end.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Push Zeros to end\n",
8 | "You have been given a random integer array/list(ARR) of size N. You have been required to push all the zeros that are present in the array/list to the end of it. Also, make sure to maintain the relative order of the non-zero elements.
\n",
9 | "
\n",
10 | "Note:
\n",
11 | "Change in the input array/list itself. You don't need to return or print the elements.
\n",
12 | "
\n",
13 | "You need to do this in one scan of array only. Don't use extra space.
\n",
14 | "
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
18 | "
\n",
19 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
20 | "
\n",
21 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
22 | "
\n",
23 | "Output Format :
\n",
24 | "For each test case, print the elements of the array/list in the desired order separated by a single space.
\n",
25 | "
\n",
26 | "Output for every test case will be printed in a separate line.
\n",
27 | "
\n",
28 | "Constraints :
\n",
29 | "1 <= t <= 10^2
\n",
30 | "0 <= N <= 10^5
\n",
31 | "Time Limit: 1 sec
\n",
32 | "
\n",
33 | "Sample Input 1:
\n",
34 | "1
\n",
35 | "7
\n",
36 | "2 0 0 1 3 0 0
\n",
37 | "Sample Output 1:
\n",
38 | "2 1 3 0 0 0 0
\n",
39 | " Explanation for the Sample Input 1 :
\n",
40 | "All the zeros have been pushed towards the end of the array/list. Another important fact is that the order of the non-zero elements have been maintained as they appear in the input array/list.
\n",
41 | "Sample Input 2:
\n",
42 | "2
\n",
43 | "5
\n",
44 | "0 3 0 2 0
\n",
45 | "4
\n",
46 | "9 0 0 8 2
\n",
47 | "Sample Output 2:
\n",
48 | "3 2 0 0 0
\n",
49 | "9 8 2 0 0"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "loop=int(input())\n",
59 | "for i in range(loop):\n",
60 | " no_element=input()\n",
61 | " n=input().split()\n",
62 | " n.sort(key='0'.__eq__)\n",
63 | " for k in range(len(n)):\n",
64 | " print(n[k],end=' ')\n",
65 | " print()"
66 | ]
67 | }
68 | ],
69 | "metadata": {
70 | "kernelspec": {
71 | "display_name": "Python 3",
72 | "language": "python",
73 | "name": "python3"
74 | },
75 | "language_info": {
76 | "codemirror_mode": {
77 | "name": "ipython",
78 | "version": 3
79 | },
80 | "file_extension": ".py",
81 | "mimetype": "text/x-python",
82 | "name": "python",
83 | "nbconvert_exporter": "python",
84 | "pygments_lexer": "ipython3",
85 | "version": "3.8.6"
86 | }
87 | },
88 | "nbformat": 4,
89 | "nbformat_minor": 4
90 | }
91 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Rotate array.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Rotate array\n",
8 | "You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).
\n",
9 | "
\n",
10 | "Note:
\n",
11 | "Change in the input array/list itself. You don't need to return or print the elements.
\n",
12 | "
\n",
13 | "Input format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
17 | "
\n",
18 | "Second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
19 | "
\n",
20 | "Third line contains the value of 'D' by which the array/list needs to be rotated.
\n",
21 | "
\n",
22 | "Output Format :
\n",
23 | "For each test case, print the rotated array/list in a row separated by a single space.
\n",
24 | "
\n",
25 | "Output for every test case will be printed in a separate line.
\n",
26 | "
\n",
27 | "Constraints :
\n",
28 | "1 <= t <= 10^4
\n",
29 | "0 <= N <= 10^6
\n",
30 | "0 <= D <= N
\n",
31 | "Time Limit: 1 sec
\n",
32 | "
\n",
33 | "Sample Input 1:
\n",
34 | "1
\n",
35 | "7
\n",
36 | "1 2 3 4 5 6 7
\n",
37 | "2
\n",
38 | "Sample Output 1:
\n",
39 | "3 4 5 6 7 1 2
\n",
40 | "Sample Input 2:
\n",
41 | "2
\n",
42 | "7
\n",
43 | "1 2 3 4 5 6 7
\n",
44 | "0
\n",
45 | "4
\n",
46 | "1 2 3 4
\n",
47 | "2
\n",
48 | "Sample Output 2:
\n",
49 | "1 2 3 4 5 6 7
\n",
50 | "3 4 1 2"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "loop=int(input())\n",
60 | "n=[]\n",
61 | "n1=[]\n",
62 | "for i in range(loop):\n",
63 | " no_of_ele=int(input())\n",
64 | " if no_of_ele !=0:\n",
65 | " n=[int(x) for x in input().split()]\n",
66 | " no_rotate=int(input())\n",
67 | " if no_of_ele!=no_rotate:\n",
68 | " n1=n[no_rotate:]\n",
69 | " n1.extend(n[0:no_rotate]) \n",
70 | " print(*n1)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "#Another Code\n",
80 | "loop=int(input())\n",
81 | "n=[]\n",
82 | "for i in range(loop):\n",
83 | " no_of_ele=int(input())\n",
84 | " if no_of_ele !=0:\n",
85 | " n=[int(x) for x in input().split()]\n",
86 | " no_rotate=int(input())\n",
87 | " for p in range(no_rotate):\n",
88 | " n.append(n[0])\n",
89 | " n.pop(0)\n",
90 | " print(*n)"
91 | ]
92 | }
93 | ],
94 | "metadata": {
95 | "kernelspec": {
96 | "display_name": "Python 3",
97 | "language": "python",
98 | "name": "python3"
99 | },
100 | "language_info": {
101 | "codemirror_mode": {
102 | "name": "ipython",
103 | "version": 3
104 | },
105 | "file_extension": ".py",
106 | "mimetype": "text/x-python",
107 | "name": "python",
108 | "nbconvert_exporter": "python",
109 | "pygments_lexer": "ipython3",
110 | "version": "3.8.6"
111 | }
112 | },
113 | "nbformat": 4,
114 | "nbformat_minor": 4
115 | }
116 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Second Largest in array.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Second Largest in array\n",
8 | "You have been given a random integer array/list(ARR) of size N. You are required to find and return the second largest element present in the array/list.
\n",
9 | "If N <= 1 or all the elements are same in the array/list then return -2147483648 or -2 ^ 31(It is the smallest value for the range of Integer)
\n",
10 | "
\n",
11 | "Input format :
\n",
12 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
13 | "
\n",
14 | "The first line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
15 | "
\n",
16 | "The second line contains 'N' single space separated integers representing the elements in the array/list.
\n",
17 | "
\n",
18 | "Output Format :
\n",
19 | "For each test case, print the second largest in the array/list if exists, -2147483648 otherwise.
\n",
20 | "
\n",
21 | "Output for every test case will be printed in a separate line.
\n",
22 | "
\n",
23 | "Constraints :
\n",
24 | "1 <= t <= 10^2
\n",
25 | "0 <= N <= 10^5
\n",
26 | "
\n",
27 | "Time Limit: 1 sec
\n",
28 | "Sample Input 1:
\n",
29 | "1
\n",
30 | "7
\n",
31 | "2 13 4 1 3 6 28
\n",
32 | "Sample Output 1:
\n",
33 | "13
\n",
34 | "Sample Input 2:
\n",
35 | "1
\n",
36 | "5
\n",
37 | "9 3 6 2 9
\n",
38 | "Sample Output 2:
\n",
39 | "6
\n",
40 | "Sample Input 3:
\n",
41 | "2
\n",
42 | "2
\n",
43 | "6 6
\n",
44 | "4
\n",
45 | "90 8 90 5
\n",
46 | "Sample Output 3:
\n",
47 | "-2147483648
\n",
48 | "8"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": null,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "loop=int(input())\n",
58 | "for p in range(loop):\n",
59 | " list1=[]\n",
60 | " n = int(input())\n",
61 | " if n!=0:\n",
62 | " list1=[int(x) for x in input().split()]\n",
63 | " size = len(list1)\n",
64 | " first = list1.count(list1[1])\n",
65 | "\n",
66 | " if n<=1:\n",
67 | " print(-2147483648)\n",
68 | "\n",
69 | " elif first==size:\n",
70 | " print(-2147483648)\n",
71 | " else:\n",
72 | " maxno=max(list1)\n",
73 | " maxcount=list1.count(maxno)\n",
74 | " for i in range(maxcount):\n",
75 | " list1.remove(maxno)\n",
76 | " list1.sort()\n",
77 | " print(list1[-1])"
78 | ]
79 | }
80 | ],
81 | "metadata": {
82 | "kernelspec": {
83 | "display_name": "Python 3",
84 | "language": "python",
85 | "name": "python3"
86 | },
87 | "language_info": {
88 | "codemirror_mode": {
89 | "name": "ipython",
90 | "version": 3
91 | },
92 | "file_extension": ".py",
93 | "mimetype": "text/x-python",
94 | "name": "python",
95 | "nbconvert_exporter": "python",
96 | "pygments_lexer": "ipython3",
97 | "version": "3.8.6"
98 | }
99 | },
100 | "nbformat": 4,
101 | "nbformat_minor": 4
102 | }
103 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Sort 0 1 2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sort 0 1 2\n",
8 | "\n",
9 | "You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'.
\n",
10 | "
\n",
11 | "'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once.
\n",
12 | "
\n",
13 | "Note:You need to change in the given array/list itself. Hence, no need to return or print anything.
\n",
14 | "
\n",
15 | "Input format :
\n",
16 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
17 | "
\n",
18 | "First line of each test case or query contains an integer 'N' representing the size of the array/list.
\n",
19 | "
\n",
20 | "Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list.
\n",
21 | "Output Format :
\n",
22 | "For each test case, print the sorted array/list elements in a row separated by a single space.
\n",
23 | "
\n",
24 | "Output for every test case will be printed in a separate line.
\n",
25 | "
\n",
26 | "Constraints :
\n",
27 | "1 <= t <= 10^2
\n",
28 | "0 <= N <= 10^5
\n",
29 | "Time Limit: 1 sec
\n",
30 | "
\n",
31 | "Sample Input 1:
\n",
32 | "1
\n",
33 | "7
\n",
34 | "0 1 2 0 2 0 1
\n",
35 | "Sample Output 1:
\n",
36 | "0 0 0 1 1 2 2
\n",
37 | "Sample Input 2:
\n",
38 | "2
\n",
39 | "5
\n",
40 | "2 2 0 1 1
\n",
41 | "7
\n",
42 | "0 1 2 0 1 2 0
\n",
43 | "Sample Output 2:
\n",
44 | "0 1 1 2 2
\n",
45 | "0 0 0 1 1 2 2"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 1,
51 | "metadata": {},
52 | "outputs": [
53 | {
54 | "name": "stdout",
55 | "output_type": "stream",
56 | "text": [
57 | "1\n",
58 | "11\n",
59 | "2 2 1 1 2 1 0 0 1 2 2 \n",
60 | "0 0 1 1 1 1 2 2 2 2 2\n"
61 | ]
62 | }
63 | ],
64 | "source": [
65 | "#Remove All Coding Ninjas Code, Then Paste my Code\n",
66 | "loop=int(input())\n",
67 | "list=[]\n",
68 | "for i in range(loop):\n",
69 | " no_of_ele=int(input())\n",
70 | " if no_of_ele!=0:\n",
71 | " list=[int(x) for x in input().split()]\n",
72 | " list.sort()\n",
73 | " print(*list)"
74 | ]
75 | }
76 | ],
77 | "metadata": {
78 | "kernelspec": {
79 | "display_name": "Python 3",
80 | "language": "python",
81 | "name": "python3"
82 | },
83 | "language_info": {
84 | "codemirror_mode": {
85 | "name": "ipython",
86 | "version": 3
87 | },
88 | "file_extension": ".py",
89 | "mimetype": "text/x-python",
90 | "name": "python",
91 | "nbconvert_exporter": "python",
92 | "pygments_lexer": "ipython3",
93 | "version": "3.8.6"
94 | }
95 | },
96 | "nbformat": 4,
97 | "nbformat_minor": 4
98 | }
99 |
--------------------------------------------------------------------------------
/8.Searching & Sorting/Sum of Two Arrays.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sum of Two Arrays\n",
8 | "Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M.
\n",
9 | "You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index.
\n",
10 | "Note:The sizes N and M can be different.
\n",
11 | "
\n",
12 | "Output array/list(of all 0s) has been provided as a function argument. Its size will always be one more than the size of the bigger array/list. Place 0 at the 0th index if there is no carry.
\n",
13 | "
\n",
14 | "No need to print the elements of the output array/list.
\n",
15 | "
\n",
16 | "Input format :
\n",
17 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
18 | "
\n",
19 | "First line of each test case or query contains an integer 'N' representing the size of the first array/list.
\n",
20 | "
\n",
21 | "Second line contains 'N' single space separated integers representing the elements of the first array/list.
\n",
22 | "
\n",
23 | "Third line contains an integer 'M' representing the size of the second array/list.
\n",
24 | "
\n",
25 | "Fourth line contains 'M' single space separated integers representing the elements of the second array/list.
\n",
26 | "
\n",
27 | "Output Format :
\n",
28 | "For each test case, print the required sum of the arrays/list in a row, separated by a single space.
\n",
29 | "
\n",
30 | "Output for every test case will be printed in a separate line.
\n",
31 | "
\n",
32 | "Constraints :
\n",
33 | "1 <= t <= 10^2
\n",
34 | "0 <= N <= 10^5
\n",
35 | "0 <= M <= 10^5
\n",
36 | "Time Limit: 1 sec
\n",
37 | "Sample Input 1:
\n",
38 | "1
\n",
39 | "3
\n",
40 | "6 2 4
\n",
41 | "3
\n",
42 | "7 5 6
\n",
43 | "Sample Output 1:
\n",
44 | "1 3 8 0
\n",
45 | "Sample Input 2:
\n",
46 | "2
\n",
47 | "3
\n",
48 | "8 5 2
\n",
49 | "2
\n",
50 | "1 3
\n",
51 | "4
\n",
52 | "9 7 6 1
\n",
53 | "3
\n",
54 | "4 5 9
\n",
55 | "Sample Output 2:
\n",
56 | "0 8 6 5
\n",
57 | "1 0 2 2 0"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": null,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "def sumArray(arr1,n,arr2,m,output):\n",
67 | " i=n-1\n",
68 | " j=m-1\n",
69 | " carry=0\n",
70 | " l=max(n,m)+1\n",
71 | " while i>=0 and j>=0:\n",
72 | " num=arr1[i]+arr2[j]+carry\n",
73 | " s=num%10\n",
74 | " carry=num//10\n",
75 | " output[l-1]=s\n",
76 | " l=l-1\n",
77 | " i=i-1\n",
78 | " j=j-1\n",
79 | " while i>=0:\n",
80 | " num=arr1[i]+carry\n",
81 | " s=num%10\n",
82 | " carry=num//10\n",
83 | " output[l-1]=s\n",
84 | " l=l-1\n",
85 | " i=i-1\n",
86 | " while j>=0:\n",
87 | " num=arr2[j]+carry\n",
88 | " s=num%10\n",
89 | " carry=num//10\n",
90 | " output[l-1]=s\n",
91 | " l=l-1\n",
92 | " j=j-1\n",
93 | " if carry!=0:\n",
94 | " output[0]=carry\n",
95 | " \n",
96 | " \n",
97 | " \n",
98 | "t=int(input())\n",
99 | "for i in range(t):\n",
100 | " n=int(input())\n",
101 | " if n==0:\n",
102 | " print(0)\n",
103 | " break\n",
104 | " arr1=[int(x) for x in input().split()]\n",
105 | " m=int(input())\n",
106 | " arr2=[int(x) for x in input().split()]\n",
107 | " size=max(n,m)+1\n",
108 | " output=[0 for i in range(size)]\n",
109 | " sumArray(arr1,n,arr2,m,output)\n",
110 | " print(*output)\n",
111 | " print()"
112 | ]
113 | }
114 | ],
115 | "metadata": {
116 | "kernelspec": {
117 | "display_name": "Python 3",
118 | "language": "python",
119 | "name": "python3"
120 | },
121 | "language_info": {
122 | "codemirror_mode": {
123 | "name": "ipython",
124 | "version": 3
125 | },
126 | "file_extension": ".py",
127 | "mimetype": "text/x-python",
128 | "name": "python",
129 | "nbconvert_exporter": "python",
130 | "pygments_lexer": "ipython3",
131 | "version": "3.8.6"
132 | }
133 | },
134 | "nbformat": 4,
135 | "nbformat_minor": 4
136 | }
137 |
--------------------------------------------------------------------------------
/9.Two Dimensional Lists/Largest Row or Column.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Largest Row or Column\n",
8 | "For a given two-dimensional integer array/list of size (N x M), you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns.
\n",
9 | "
\n",
10 | "Note :
\n",
11 | "If there are more than one rows/columns with maximum sum, consider the row/column that comes first. And if ith row and jth column has the same largest sum, consider the ith row as answer.
\n",
12 | "
\n",
13 | "Input Format :
\n",
14 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
15 | "
\n",
16 | "First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
\n",
17 | "
\n",
18 | "Second line onwards, the next 'N' lines or rows represent the ith row values.
\n",
19 | "
\n",
20 | "Each of the ith row constitutes 'M' column values separated by a single space.
\n",
21 | "
\n",
22 | "Output Format :
\n",
23 | "For each test case, If row sum is maximum, then print: \"row\"
\n",
24 | "OR
\n",
25 | "If column sum is maximum, then print: \"column\"
\n",
26 | "It will be printed in a single line separated by a single space between each piece of information.
\n",
27 | "
\n",
28 | "Output for every test case will be printed in a seperate line.
\n",
29 | " Consider :
\n",
30 | "If there doesn't exist a sum at all then print \"row 0 -2147483648\", where -2147483648 or -2^31 is the smallest value for the range of Integer.
\n",
31 | "
\n",
32 | "Constraints :
\n",
33 | "1 <= t <= 10^2
\n",
34 | "0 <= N <= 10^3
\n",
35 | "0 <= M <= 10^3
\n",
36 | "Time Limit: 1sec
\n",
37 | "Sample Input 1 :
\n",
38 | "1
\n",
39 | "2 2
\n",
40 | "1 1
\n",
41 | "1 1
\n",
42 | "Sample Output 1 :
\n",
43 | "row 0 2
\n",
44 | "Sample Input 2 :
\n",
45 | "2
\n",
46 | "3 3
\n",
47 | "3 6 9
\n",
48 | "1 4 7
\n",
49 | "2 8 9
\n",
50 | "4 2
\n",
51 | "1 2
\n",
52 | "90 100
\n",
53 | "3 40
\n",
54 | "-10 200
\n",
55 | "Sample Output 2 :
\n",
56 | "column 2 25
\n",
57 | "column 1 342"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": null,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "#Remove All Code From Coding Ninjas then apply\n",
67 | "from sys import stdin\n",
68 | "MIN_VALUE=-2147483648\n",
69 | "def findLargest(arr, nRows, mCols):\n",
70 | " #Your code goes here\n",
71 | " isRow =True\n",
72 | " largestSum=MIN_VALUE\n",
73 | " num=0\n",
74 | " for i in range(nRows):\n",
75 | " rowSum=0\n",
76 | " for j in range(mCols):\n",
77 | " rowSum+=arr[i][j]\n",
78 | " if rowSum>largestSum:\n",
79 | " largestSum=rowSum\n",
80 | " num=i\n",
81 | " for j in range(mCols):\n",
82 | " colSum=0\n",
83 | " for i in range(nRows):\n",
84 | " colSum+=arr[i][j]\n",
85 | " if colSum>largestSum:\n",
86 | " largestSum=colSum\n",
87 | " num=j\n",
88 | " isRow=False\n",
89 | " if isRow:\n",
90 | " print(\"row \"+str(num)+\" \"+str(largestSum))\n",
91 | " else:\n",
92 | " print(\"column \"+str(num)+\" \"+str(largestSum))\n",
93 | "#Taking Input Using Fast I/O\n",
94 | "def take2DInput() :\n",
95 | " li = stdin.readline().rstrip().split(\" \")\n",
96 | " nRows = int(li[0])\n",
97 | " mCols = int(li[1])\n",
98 | " \n",
99 | " if nRows == 0 :\n",
100 | " return list(), 0, 0\n",
101 | " \n",
102 | " mat = [list(map(int, input().strip().split(\" \"))) for row in range(nRows)]\n",
103 | " return mat, nRows, mCols\n",
104 | "\n",
105 | "\n",
106 | "#main\n",
107 | "t = int(stdin.readline().rstrip())\n",
108 | "\n",
109 | "while t > 0 :\n",
110 | "\n",
111 | " mat, nRows, mCols = take2DInput()\n",
112 | " findLargest(mat, nRows, mCols)\n",
113 | "\n",
114 | " t -= 1"
115 | ]
116 | }
117 | ],
118 | "metadata": {
119 | "kernelspec": {
120 | "display_name": "Python 3",
121 | "language": "python",
122 | "name": "python3"
123 | },
124 | "language_info": {
125 | "codemirror_mode": {
126 | "name": "ipython",
127 | "version": 3
128 | },
129 | "file_extension": ".py",
130 | "mimetype": "text/x-python",
131 | "name": "python",
132 | "nbconvert_exporter": "python",
133 | "pygments_lexer": "ipython3",
134 | "version": "3.8.6"
135 | }
136 | },
137 | "nbformat": 4,
138 | "nbformat_minor": 4
139 | }
140 |
--------------------------------------------------------------------------------
/9.Two Dimensional Lists/Row Wise Sum.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Row Wise Sum\n",
8 | "For a given two-dimensional integer array/list of size (N x M), find and print the sum of each of the row elements in a single line, separated by a single space.
\n",
9 | "
\n",
10 | "Input Format :
\n",
11 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
12 | "
\n",
13 | "First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
\n",
14 | "
\n",
15 | "Second line onwards, the next 'N' lines or rows represent the ith row values.
\n",
16 | "
\n",
17 | "Each of the ith row constitutes 'M' column values separated by a single space.
\n",
18 | "
\n",
19 | "Output Format :
\n",
20 | "For each test case, print the sum of every ith row elements in a single line separated by a single space.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a seperate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^3
\n",
27 | "0 <= M <= 10^3
\n",
28 | "Time Limit: 1sec
\n",
29 | "
\n",
30 | "Sample Input 1:
\n",
31 | "1
\n",
32 | "4 2
\n",
33 | "1 2
\n",
34 | "3 4
\n",
35 | "5 6
\n",
36 | "7 8
\n",
37 | "Sample Output 1:
\n",
38 | "3 7 11 15
\n",
39 | "Sample Input 2:
\n",
40 | "2
\n",
41 | "2 5
\n",
42 | "4 5 3 2 6
\n",
43 | "7 5 3 8 9
\n",
44 | "4 4
\n",
45 | "1 2 3 4
\n",
46 | "9 8 7 6
\n",
47 | "3 4 5 6
\n",
48 | "-1 1 -10 5
\n",
49 | "Sample Output 2:
\n",
50 | "20 32
\n",
51 | "10 30 18 -5"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": null,
57 | "metadata": {},
58 | "outputs": [],
59 | "source": [
60 | "p=int(input())\n",
61 | "for i in range(p):\n",
62 | " str=input().split()\n",
63 | " n,m=int(str[0]),int(str[1])\n",
64 | " arr=[]\n",
65 | " for i in range(n):\n",
66 | " b=input().split()\n",
67 | " arrr=[int(x) for x in b]\n",
68 | " print(sum(arrr),end=\" \")\n",
69 | " print()"
70 | ]
71 | }
72 | ],
73 | "metadata": {
74 | "kernelspec": {
75 | "display_name": "Python 3",
76 | "language": "python",
77 | "name": "python3"
78 | },
79 | "language_info": {
80 | "codemirror_mode": {
81 | "name": "ipython",
82 | "version": 3
83 | },
84 | "file_extension": ".py",
85 | "mimetype": "text/x-python",
86 | "name": "python",
87 | "nbconvert_exporter": "python",
88 | "pygments_lexer": "ipython3",
89 | "version": "3.8.6"
90 | }
91 | },
92 | "nbformat": 4,
93 | "nbformat_minor": 4
94 | }
95 |
--------------------------------------------------------------------------------
/9.Two Dimensional Lists/Spiral Print.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Spiral Print\n",
8 | "For a given two-dimensional integer array/list of size (N x M), print it in a spiral form. That is, you need to print in the order followed for every iteration:\n",
9 | "\n",
10 | "
a. First row(left to right)\n",
11 | "
b. Last column(top to bottom)\n",
12 | "
c. Last row(right to left)\n",
13 | "
d. First column(bottom to top)\n",
14 | "
Mind that every element will be printed only once.\n",
15 | "
Refer to the Image:\n",
16 | "
\n",
17 | "Input format :
\n",
18 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
19 | "
\n",
20 | "First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
\n",
21 | "
\n",
22 | "Second line onwards, the next 'N' lines or rows represent the ith row values.
\n",
23 | "
\n",
24 | "Each of the ith row constitutes 'M' column values separated by a single space.
\n",
25 | "Output format :
\n",
26 | "For each test case, print the elements of the two-dimensional array/list in the spiral form in a single line, separated by a single space.
\n",
27 | "
\n",
28 | "Output for every test case will be printed in a seperate line.
\n",
29 | "Constraints :
\n",
30 | "1 <= t <= 10^2
\n",
31 | "0 <= N <= 10^3
\n",
32 | "0 <= M <= 10^3
\n",
33 | "Time Limit: 1sec
\n",
34 | "
\n",
35 | "Sample Input 1:
\n",
36 | "1
\n",
37 | "4 4
\n",
38 | "1 2 3 4
\n",
39 | "5 6 7 8
\n",
40 | "9 10 11 12
\n",
41 | "13 14 15 16
\n",
42 | "Sample Output 1:
\n",
43 | "1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
\n",
44 | "Sample Input 2:
\n",
45 | "2
\n",
46 | "3 3
\n",
47 | "1 2 3
\n",
48 | "4 5 6
\n",
49 | "7 8 9
\n",
50 | "3 1
\n",
51 | "10
\n",
52 | "20
\n",
53 | "30
\n",
54 | "Sample Output 2:
\n",
55 | "1 2 3 6 9 8 7 4 5
\n",
56 | "10 20 30"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "from sys import stdin\n",
66 | "\n",
67 | "def spiralPrint(mat, nRows, mCols):\n",
68 | " loop=nRows\n",
69 | " if loop%2!=0:\n",
70 | " loop+=1\n",
71 | " for i in range(loop//2):\n",
72 | " for j in range(i,mCols-i):\n",
73 | " print(mat[i][j],end=\" \")\n",
74 | " for k in range(1+i,nRows-i):\n",
75 | " print(mat[k][j],end=\" \")\n",
76 | " for l in range(mCols-2-i,-1+i,-1):\n",
77 | " print(mat[k][l],end=\" \")\n",
78 | " for p in range(nRows-2-i,0+i,-1):\n",
79 | " print(mat[p][l],end=\" \")\n",
80 | "\n",
81 | "#Taking Input Using Fast I/O\n",
82 | "def take2DInput() :\n",
83 | " li = stdin.readline().rstrip().split(\" \")\n",
84 | " nRows = int(li[0])\n",
85 | " mCols = int(li[1])\n",
86 | " \n",
87 | " if nRows == 0 :\n",
88 | " return list(), 0, 0\n",
89 | " \n",
90 | " mat = [list(map(int, input().strip().split(\" \"))) for row in range(nRows)]\n",
91 | " return mat, nRows, mCols\n",
92 | "\n",
93 | "\n",
94 | "#main\n",
95 | "t = int(stdin.readline().rstrip())\n",
96 | "\n",
97 | "while t > 0 :\n",
98 | "\n",
99 | " mat, nRows, mCols = take2DInput()\n",
100 | " spiralPrint(mat, nRows, mCols)\n",
101 | " print()\n",
102 | "\n",
103 | " t -= 1"
104 | ]
105 | }
106 | ],
107 | "metadata": {
108 | "kernelspec": {
109 | "display_name": "Python 3",
110 | "language": "python",
111 | "name": "python3"
112 | },
113 | "language_info": {
114 | "codemirror_mode": {
115 | "name": "ipython",
116 | "version": 3
117 | },
118 | "file_extension": ".py",
119 | "mimetype": "text/x-python",
120 | "name": "python",
121 | "nbconvert_exporter": "python",
122 | "pygments_lexer": "ipython3",
123 | "version": "3.8.6"
124 | }
125 | },
126 | "nbformat": 4,
127 | "nbformat_minor": 4
128 | }
129 |
--------------------------------------------------------------------------------
/9.Two Dimensional Lists/Wave Print.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Wave Print\n",
8 | "For a given two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, i.e, print the first column top to bottom, next column bottom to top and so on.
\n",
9 | "
\n",
10 | "Input format :
\n",
11 | "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
\n",
12 | "
\n",
13 | "First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
\n",
14 | "
\n",
15 | "Second line onwards, the next 'N' lines or rows represent the ith row values.
\n",
16 | "
\n",
17 | "Each of the ith row constitutes 'M' column values separated by a single space.
\n",
18 | "
\n",
19 | "Output format :
\n",
20 | "For each test case, print the elements of the two-dimensional array/list in the sine wave order in a single line, separated by a single space.
\n",
21 | "
\n",
22 | "Output for every test case will be printed in a seperate line.
\n",
23 | "
\n",
24 | "Constraints :
\n",
25 | "1 <= t <= 10^2
\n",
26 | "0 <= N <= 10^3
\n",
27 | "0 <= M <= 10^3
\n",
28 | "Time Limit: 1sec
\n",
29 | "
\n",
30 | "Sample Input 1:
\n",
31 | "1
\n",
32 | "3 4
\n",
33 | "1 2 3 4
\n",
34 | "5 6 7 8
\n",
35 | "9 10 11 12
\n",
36 | "Sample Output 1:
\n",
37 | "1 5 9 10 6 2 3 7 11 12 8 4
\n",
38 | "Sample Input 2:
\n",
39 | "2
\n",
40 | "5 3
\n",
41 | "1 2 3
\n",
42 | "4 5 6
\n",
43 | "7 8 9
\n",
44 | "10 11 12
\n",
45 | "13 14 15
\n",
46 | "3 3
\n",
47 | "10 20 30
\n",
48 | "40 50 60
\n",
49 | "70 80 90
\n",
50 | "Sample Output 2:
\n",
51 | "1 4 7 10 13 14 11 8 5 2 3 6 9 12 15
\n",
52 | "10 40 70 80 50 20 30 60 90"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "Solve Using While Loop
"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {},
66 | "outputs": [],
67 | "source": [
68 | "from sys import stdin\n",
69 | "\n",
70 | "\n",
71 | "def wavePrint(mat, nRows, mCols):\n",
72 | " i = 0\n",
73 | " while i 0:\n",
103 | " mat, nRows, mCols = take2DInput()\n",
104 | " wavePrint(mat, nRows, mCols)\n",
105 | " print()\n",
106 | "\n",
107 | " t -= 1"
108 | ]
109 | },
110 | {
111 | "cell_type": "markdown",
112 | "metadata": {},
113 | "source": [
114 | "Solve Using For Loop
"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {},
121 | "outputs": [],
122 | "source": [
123 | "from sys import stdin\n",
124 | "\n",
125 | "\n",
126 | "\n",
127 | "def wavePrint(mat, nRows, mCols):\n",
128 | " if nRows==0:\n",
129 | " return\n",
130 | " for i in range(mCols):\n",
131 | " if i%2==0:\n",
132 | " for j in range(nRows):\n",
133 | " print(mat[j][i],end=\" \")\n",
134 | " else:\n",
135 | " for j in range(nRows-1,-1,-1):\n",
136 | " print(mat[j][i],end=\" \")\n",
137 | "\n",
138 | "\n",
139 | "\n",
140 | "# Taking Iput Using Fast I/O\n",
141 | "def take2DInput():\n",
142 | " li = stdin.readline().rstrip().split(\" \")\n",
143 | " nRows = int(li[0])\n",
144 | " mCols = int(li[1])\n",
145 | "\n",
146 | " if nRows == 0:\n",
147 | " return list(), 0, 0\n",
148 | "\n",
149 | " mat = [list(map(int, input().strip().split(\" \"))) for row in range(nRows)]\n",
150 | " return mat, nRows, mCols\n",
151 | "\n",
152 | "\n",
153 | "# main\n",
154 | "t = int(stdin.readline().rstrip())\n",
155 | "\n",
156 | "while t > 0:\n",
157 | " mat, nRows, mCols = take2DInput()\n",
158 | " wavePrint(mat, nRows, mCols)\n",
159 | " print()\n",
160 | "\n",
161 | " t -= 1"
162 | ]
163 | }
164 | ],
165 | "metadata": {
166 | "kernelspec": {
167 | "display_name": "Python 3",
168 | "language": "python",
169 | "name": "python3"
170 | },
171 | "language_info": {
172 | "codemirror_mode": {
173 | "name": "ipython",
174 | "version": 3
175 | },
176 | "file_extension": ".py",
177 | "mimetype": "text/x-python",
178 | "name": "python",
179 | "nbconvert_exporter": "python",
180 | "pygments_lexer": "ipython3",
181 | "version": "3.8.6"
182 | }
183 | },
184 | "nbformat": 4,
185 | "nbformat_minor": 4
186 | }
187 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Coding Ninjas Problem Solving Using Python
2 | 
3 | Problem Solving Using Python Programming taught by Coding Ninjas.
4 | This repository includes all the practice problems and assignments.
5 | I made this repository for your help.
6 | If you're unable to view the patterns correctly in the Jupyter notebook, then double-click the markdown cell to enlarge.
7 |
8 | If there is any code in coding ninjas first remove that than use my code
9 |
10 | Topics discussed are:-
11 | 1) [Introduction to Python](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/1.Introduction%20to%20Python)
12 | 2) [Conditionals and Loops](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/2.Conditionals%20and%20Loops)
13 | 3) [Patterns 1](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/3.%20Pattern%201)
14 | 4) [Patterns 2](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/4.Pattern%202)
15 | 5) [More on Loops](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/5.More%20on%20Loops)
16 | 6) [Functions](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/6.Functions)
17 | 7) [Arrays and Lists](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/7.Arrays%20%26%20Lists)
18 | 8) [Searching And Sorting](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/8.Searching%20%26%20Sorting)
19 | 9) [Two Dimensional Lists](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/9.Two%20Dimensional%20Lists)
20 | 10) [Strings](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/tree/main/10.Strings)
21 | 11) [2048 Game](https://github.com/JainMaster/Coding-Ninjas-Problem-Solving-Using-Python/blob/main/2048%20Game.ipynb)
22 |
23 |
24 | # Support Me By Giving Me Star
25 |
26 |
27 |
--------------------------------------------------------------------------------