Posts

Showing posts with the label code

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

Sparse Arrays in C - HackerRank Solution

Image
Solution for hackerRank problem Sparse Arrays in C Problem :           There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example, stringList = [ 'ab', 'ab' ,  'abc' ] queries = [ 'ab' , 'abc' , 'bc' ] There are 2 instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results = [2,1,0]. Function Description Complete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in stringList. matchingStrings has the following parameters: string stringList[n] - an array of strings to search string queries[q] - an array of query strings Returns int[q]: an array of results for each query Input Format The first line contains and integer n, the

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

String validators in python - HackerRank solution

Solution for hackerRank problem String validators in python Problem : Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc. str.isalnum() This method checks if all the characters of a string are alphanumeric (a-z, A-Z, and 0-9). >>> print 'ab123'.isalnum() True >>> print 'ab123#'.isalnum() False str.isalpha() This method checks if all the characters of a string are alphabetical (a-z and A-Z). >>> print 'abcD'.isalpha() True >>> print 'abcd1'.isalpha() False str.isdigit() This method checks if all the characters of a string are digits (0-9). >>> print '1234'.isdigit() True >>> print '123edsd'.isdigit() False str.islower() This method checks if all the characters of a string are lowercase characters (a-z). >>> print 'abcd123#'.islower() True >>> print &