Posts

Showing posts with the label Python program

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 Angle MBC in python - HackerRank solution

Image
Solution for hackerRank problem Nested lists in python Problem : ABC is a right triangle, 90 o at B. Therefore <ABC = 90 o  . Point M is the midpoint of hypotenuse AC. You are given the lengths AB and BC. Your task is to find <MBC (angle  0 o   as shown in the figure) in degrees. Input Format The first line contains the length of side AB . The second line contains the length of side BC. Constraints 0 < AB <= 100 0 < BC <= 100 Lengths AB and BC are natural numbers. Output Format Output <MBC in degrees. Note:  Round the angle to the nearest integer. Examples : If angle is 56.5000001°, then output  57° . If angle is 56.5000000°, then output  57° . If angle is 56.4999999°, then output  56° . 0 o  <  θ o  < 90 o . Sample Input 10 10 Sample Output 45° Procedure 1. Start. 2. Import math module. 3. Get the length of the sides. 4. Find the AB/BC. 5. AB/BC = tan  θ. 6.  θ = t an -1 (AB/BC). 7. Round the angle to integer. 8. Print the angle in degrees. 9. End. Code

Word Order in Python - HackerRank solution

Solution for hackerRank problem Word order in python Problem : You are given n words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification. Note: Each input line ends with a "\n" character. Constraints: 1 <= n <=  10 5 . The sum of the lengths of all the words do not exceed  10 6 . All the words are composed of lowercase English letters only. Input Format The first line contains the integer, n . The next n lines each contain a word. Output Format Output  2 lines. On the first line, output the number of distinct words from the input. On the second line, output the number of occurrences for each distinct word according to their appearance in the input. Sample Input 4 bcdef abcdefg bcde bcdef Sample Output 3 2 1 1 Explanation There are 3 distinct words. Here,  "bcdef"  appears twice in the input at the first and last

Text wrap in python - HackerRank solution

Solution for hackerRank problem Text wrap in python Problem : You are given a string S  and width w. Your task is to wrap the string into a paragraph of width w. Function Description Complete the  wrap  function in the editor below. wrap  has the following parameters: string string: a long string int max_width: the width to wrap to Returns string: a single string with newline characters ('\n') where the breaks should be Input Format The first line contains a string, string. The second line contains the width, max width . Constraints 0 < len(string) < 1000 0 < maxwidth < len(string) Sample Input 0 ABCDEFGHIJKLIMNOQRSTUVWXYZ 4 Sample output 0 ABCD EFGH IJKL IMNO QRST UVWX YZ Procedure 1. Start. 2. Initialize a list. 3. loop from 0 to length of string => If i is not equal to 0 and i % width =0     => add new line character to list => else     => append s[i] to the list. 4. Covert the list into string. 5. Print the converted string 6. End C ode : import text