Posts

Showing posts with the label Basic Python Problems

Transpose of a matrix

  Write a program to find the transpose of a given n*m matrix. Input Format : First line take an Integer from stdin  which is number of rows  n. Second line take an Integer from stdin which is number of columns  m. Third line take an Integers which are matrix elements Output Format: Transpose of a matrix Example Input: 1   2   3 4   5   6 7   8   9 Output: 1   4   7 2   5   8 3   6   9 Solution: rows=int(input())  #transpose of a matrix cols=int(input()) a=list(map(int,input().split())) k=0 arr= [[0 for i in range(cols)] for j in range(rows)] for i in range(rows):     for j in range(cols):         arr[j][i]=a[k]         k=k+1 for i in range(rows):     for j in range(cols):         print(arr[i][j],end=" ")     print()

Printing Diagonal of 2D Array

  Write a Program to take 2d Integer Array then you have to print the Right diagonal elements in that Array. Input Format: The first line take Integer inputs of row and colomn from stdin. In Second line take the values of 2d array according to its row and colomn value. Output Format: Print the Integers Which is right diagonal elements of 2d Array by space separated. Example Input: 3 3 1 2 3 4 5 6 7 8 9 Output: 1 5 9 Example Input: 3 4 1 1 1 1 2 2 2 2 3 3 3 3 Output: 1 2 3 Solution: rows=int(input()) cols=int(input()) a=list(map(int,input().split())) k=0 arr=[[0]*cols]*rows for i in range(rows):     for j in range(cols):         arr[i][j]=a[k]         k=k+1         if(i==j):             print(arr[i][j],end=" ")

Largest Palindrome in the Array

  Write a program to find the largest palindrome in the String Array Input Format: In First line take an Integer which is size of the array In Second line take a list of Strings Output Format: print the Largest palindrome String Example Input: 4 aba abab abaab abaaba Output: abaaba Note: If there are multiple largest palindromic strings with same length, print the lexicpraphically smallest one. Solution: n=int(input()) #largest palindrome in a array l=[] x=list(map(str,input().split())) for i in range(len(x)):     k=x[i]     if(x[i]==k[::-1]):   #strng rev is compared to original one         l.append(x[i]) maxlen=0 if(len(l)!=0):          for i in range(len(l)):         if(maxlen<len(l[i])):             maxlen=len(l[i])     m=[]     for j in l:         if(len(j)==maxlen):             m.a...

Merge 2 unsorted arrays into a sorted array

  Given 2 unsorted arrays, combine both of them such that the resultant array is sorted. Print the resultant array. Input Format First line contains size of first array Followed by elements of first array Following line contains size of 2nd array Followed by elements of the 2nd array Output Format Print the elements of the merged array on a single line Example Input: 5 2 8 9 0 1 3 3 2 1 Output: 0 1 1 2 2 3 8 9 Solution: n1=int(input()) a1=list(map(int,input().split())) n2=int(input()) a2=list(map(int,input().split())) a2=a1+a2  #merge two arrays a2.sort() for i in range(len(a2)):     print(a2[i],end=" ")

Sum Of an Array

  Write a program to take Array of Elements and print the sum of all numbers in Array . Input format: First line take an Integer input from stdin which is array length  n. Second line take n Integers which is inputs of array . Output format: Print Integer which is sum of all elements in an array. Example Input: 5 1 2 3 4 5 Output: 15 Example Input: 6 1 2 1 2 1 2 Output: 9 Solution: n=int(input()) e=list(map(int,input().split()))  # making input list changed to int type s=0 if(n==len(e)):          for i in range(n):         s=s+e[i]     print(s)

Max Number In Array

  Write a Program to take Array of values,and print the Maximum number in an Array. Input Format: First line take an Integer input from stdin which is array length  n. Second line take n Integers which is inputs of array . Output Format: Print the Integer which is maximum value in an array. Example Input: 5 2 3 1 5 4 Output: 5 Example Input: 6 1 3 7 6 2 4 Output: 7 Solution: n=int(input()) e=input().split() if(n==len(e)):     for i in range(n):         e[i]=int(e[i])       e.sort()     print(e[-1])   #last element of list   

Left Arrow Pattern

  Write a program to print the Left Arrow Pattern.You need to do that by  using two for loops only. Input Format: Take integer input from stdin. Output Format: Print the desired Pattern. Example Input: 5 Output: * * * * * * * * * *  * * * * * * * *  * * * * * *  * * * *  * *  * *  * * * *  * * * * * *  * * * * * * * *  * * * * * * * * * *  Solution: n=int(input()) for i in range(n):     for j in range(2*(n-i)):    #for upper half pattern         print("*",end=" ")     print()     #new line  for i in range(1,n+1):    # for bottom half pattern     for j in range(2*i):         print("*",end=" ")     print()

Reverse Parts Of A String

  Write a program to take input a line from stdin. You have to reverse every word of this line, except for the starting and ending line of the words. For example, for the following input: we love coding in this world the output is: we lveo cnidog in tihs wlrod every word has been reversed except it's first and last alphabet. Input format: first line contains the number of lines,  n this is followed by  n  lines Output: print each line such that the words are reversed as per the criteria above Example Input: 5 hello all this is a good example to show you how to reverse a string Output: hlleo all  tihs is a good  elpmaxe  to sohw you how to rsrevee  a snirtg  Solution: t=int(input()) for i in range(t):     s=input()     str=s.split()     for i in str:         q=[h for h in i]  # for each word to be read         k=1  #for 2nd character of word      ...

Hollow Diamond

  Write a program to take number   n  and print a Hollow Diamond shape . Input Format: Take an Integer from stdin. Output: Print the desired Pattern Example Input: 5 Output:     *    * *   *   *  *     * *       *  *     *   *   *    * *     * Solution: n=int(input()) a=(n*2)-1   #no of lines  for i in range(a):     for j in range(a):         if(i+j==int(a/2) or j-i==int(a/2) or i-j==int(a/2) or i+j==((a-1)+int(a/2))):             print("*",end="")         else:             print(" ",end="")     print("")

Inverted Full Pyramid

  Given an integer   n   as input, print an inverted full pyramid of   n   rows. Input Format: Take input an integer from stdin Output Format: Print the pattern Example Input: 10 Output: * * * * * * * * * *   * * * * * * * * *     * * * * * * *      * * * * * *       * * * * *        * * * *         * * *          * *           *    Solution: n=int(input()) for i in range(n):     for j in range(i):   # j for starting  spaces          print(" ",end="")     for k in range(n-i): #k for stars in a line         print("*",end=" ")     print("")

Right Arrow Pattern

Write a program to print the Right Arrow Pattern. Input Format: Take an input Integer which is from stdin. Output Format: print Right Arrow pattern Example Input: 5 Output: ***** **** *** ** * ** *** **** ***** SOLUTION: n=int(input()) for i in range(n):     for j in range(2*i):      #for spaces as each i th row has 2*i spaces         print(" ",end="")     for k in range(n-i):     #for stars as each i th row has n-i stars         print("*",end="")          print("")                     # half pattern is completed here for i in range(2,n+1):     #range started from 2 as the single star at middle is already                                         ...