Posts

Showing posts with the label hackerank challenges

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 &

Find a string in python - HackerRank solution

Solution for hackerRank problem Find a string in python Problem : In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left. NOTE:  String letters are case-sensitive. Input Format The first line of input contains the original string. The next line contains the substring. Constraints 1 <= len(string) <= 200 Each character in the string is an  ascii  character. Output Format Output the integer number indicating the total number of occurrences of the substring in the original string. Sample Input ABCDCDC CDC Sample Output 2 Concept Some string processing examples,   such as these , might be useful. There are a couple of new concepts: In Python, the length of a string is found by the function  len(s) , where   is the string. To traverse through the length of a string, use a  for  loop: for i in range(0, len(s)):     print

Athlete sort in python - HackerRank solution

Image
Solution for hackerRank problem Athlete sort in python Problem : You are given a spreadsheet that contains a list of N  athletes and their details (such as age, height, weight, and so on). You are required to sort the data based on Kth the  attribute and print the final resulting table. Follow the example given below for a better understanding. Note that is indexed from 0 to M-1, where M is the number of attributes. Note : If two attributes are the same for different rows, for example, if two athletes are of the same age, print the row that appeared first in the input. Input Format The first line contains N and M separated by a space. The next N lines each contain M elements. The last line contains K  . Constraints 1<=N, M<=1000 0<=K<M Each element <= 1000 Output Format Print the lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity. Sample Input 0: 5 3 10 2 5 7 1 0 9 9 9 1 23 12 6 5 9 1 Sample Output 0 7 1 0