Posts

Showing posts with the label python programs

ginortS in Python - HackerRank solution

Solution for hackerRank problem ginortS in Python Problem : You are given a string S.  S contains alphanumeric characters only.  'SORTING' Your task is to sort the string S in the following manner: All sorted lowercase letters are ahead of uppercase letters. All sorted uppercase letters are ahead of digits. All sorted odd digits are ahead of sorted even digits. Input Format A single line of input contains the string S. Constraints 0 < len(S) < 1000 Output Format Output the sorted string S. Sample Input Sorting1234 Sample Output ginortS1324 Code : s = str(input()) Upper = [] lower = [] odd_no = [] even_no = [] for i in s:     if(i.isupper()):         Upper += [i]     if(i.islower()):         lower += [i]     if(i.isnumeric()):         if(int(i)%2 == 0):             even_no += [i]         else :             odd_no += [i] Upper.sort() lower.sort() odd_no.sort() even_no.sort() Upper = "".join(Upper) lower = "".join(lower) odd = "".join(odd_no) ev

Arrays - DS in Python - HackerRank solution

Image
Solution for hackerRank problem Arrays-DS in Python Problem : An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A , of size N, each memory location has some unique index,  i(where 0 <= i < N), that can be referenced as A[i] or Ai. Reverse an array of integers. Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this. Example A = [1, 2, 3] Return [3, 2, 1]. Function Description Complete the function reverseArray in the editor below. reverseArray has the following parameter(s): int A[n]: the array to reverse Returns int[n]: the reversed array Input Format The first line contains an integer, N, the number of integers in A. The second line contains N space-separated integers that make up A. Constraints 1 <= N <= 10^3 1 <= A[i] <= 10^4, where A[i] is the ith integer in A Sample Input 1 Sample Output 1 2 3 4 1 Code : #!/bin/python3 import math impor

Time Delta in Python - HackerRank solution

Solution for hackerRank problem Time Delta in Python Problem : When users post an update on social media,such as a URL, image, status update etc., other users in their network are able to view this new post on their news feed. Users can also see exactly when the post was published, i.e, how many hours, minutes or seconds ago. Since sometimes posts are published and viewed in different time zones, this can be confusing. You are given two timestamps of one such post that a user can see on his newsfeed in the following format: Day dd Mon yyyy hh:mm:ss +xxxx Here +xxxx represents the time zone. Your task is to print the absolute difference (in seconds) between them. Input Format The first line contains T, the number of testcases. Each testcase contains  2 lines, representing time t1 and time t2. Constraints Input contains only valid timestamps year <= 3000 Output Format Print the absolute difference (t1-t2)  in seconds. Sample Input 0 2 Sun 10 May 2015 13:54:36 -0700 Sun 10 May 2015 13:

Validating Credit Card Numbers in Python - HackerRank solution

Solution for hackerRank problem Validating Credit Card Numbers in Python Problem : You and Fredrick are good friends. Yesterday, Fredrick received  N credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help! A valid credit card from ABCD Bank has the following characteristics: ► It must start with a  4, 5 or 6. ► It must contain exactly  16 digits. ► It must only consist of digits (0-9). ► It may have digits in groups of 4, separated by one hyphen "-". ► It must NOT use any other separator like ' ' , '_', etc. ► It must NOT have  4 or more consecutive repeated digits. Examples: Valid Credit Card Numbers 4253625879615786 4424424424442444 5122-2368-7954-3214 InValid Credit Card Numbers 42536258796157867       #17 digits in card number → Invalid  4424444424442444        #Consecutive digits are repeating 4 or more times → Invalid 5122-2368-7954 - 3214   #Separato

Array Manipulation in Python - HackerRank Solution

Image
Solution for hackerRank problem Array Manipulation in Python Problem : Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example, n = 10 queries = [ [1, 5, 3], [4, 8, 7], [6, 9, 1] ] Queries are interpreted as follows: Add the values of k between the indices a and b inclusive: The largest value is 10 after all operations are performed. Function Description Complete the function arrayManipulation in the editor below. arrayManipulation has the following parameters: int n - the number of elements in the array int queries[q][3] - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k. Returns int - the maximum value in the resultant array Input Format The first line contains two space-separated integers  n and  m, the size of the array and the number of operation

Left Rotation in Python - HackerRank solution

Solution for hackerRank problem Left Rotation in Python Problem : A  left rotation  operation on an array of size n  shifts each of the array's elements 1  unit to the left. Given an integer,d , rotate the array that many steps left and return the result. Example d = 2 arr = [1,2,3,4,5] After 2 rotations, arr' = [3,4,5,1,2]. Function Description Complete the  rotateLeft  function in the editor below. rotateLeft  has the following parameters: int d:  the amount to rotate by int arr[n]:  the array to rotate Returns int[n]:  the rotated array Input Format The first line contains two space-separated integers that denote n , the number of integers, and d , the number of left rotations to perform. The second line contains  n space-separated integers that describe arr[] . Constraints 1<=n<= 10 5 1<=d<=n 1<=a[i]<=10 6 Sample Input 5 4 1 2 3 4 5 Sample Output 5 1 2 3 4 Explanation To perform d=4  left rotations, the array undergoes the following sequence of changes: [1

Python program to count the number of digits in a number

Image
Count the number of digits in a number Problem description : The program gets a number as input from the user and returns the number of digits in the number. For example, number = 845 Here for the above number the number of digits is equal to 3. Sample input : n = 1324 Sample output : Number of digits = 4 Method 1 : In this method,  We are using a while loop until the number becomes zero.   "//" operator is used , it d ivides the number on its left by the number on its right, rounds down the answer, and returns a whole number. For example,  45 // 10 = 4 762 // 10 = 76 After performing the division operation the count variable is increased by one. Procedure : 1. Start. 2. Get the number as input from user and store it in a variable. 3. Initialize digit count variable as 0. 4. Loop (until n = 0)     => increment digit count     => num = num // 10 5. Print digit count. 6. End.  Code : #Get the number as input from the user and store it in a variable  n = int(input("En

Matrix input in python

Image
Get matrix input from user in Python A matrix is a rectangular arrangement of numbers arranged in rows and columns. Eg : 3x3 matrix          1  2  3 M =   4  5  6          7  8  9 In python a matrix is nothing but a list of lists (sub list). The above matrix can be written as [ [1,2,3], [4,5,6], [7,8,9] ]. Prerequisite :   List in python Method 1 : In this method, we get the elements of matrix and append it to a list and append the entire list to a main list (matrix). Code : #function to print matrix def printMatrix(matrix,r,c): for i in range(r): for j in range(c): print(matrix[i][j],  end=" ") print() #Get the number of rows from user r  = int(input("Enter the number of rows : ")) #Get the number of columns from the user c  = int(input("Enter the number of columns : ")) #initialize a matrix(list) matrix = [] for row in range(r): l = [] for col in range(c):                 #Get elements one by one num = int(input("Enter  element :