Posts

Showing posts with the label python exercise

Remove duplicates from sorted array, palindrome number - Leetcode solution in python

Solution  for leetcode problems -  Remove duplicates from sorted array and palindrome number  in python 1.  Remove duplicates from sorted array Given an integer array, nums sorted in non-decreasing order, remove the duplicates in place such that each unique element appears only once. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements. Return k after placing the final result in the first k slots of nums. Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory. Custom Judge: The judge will test your solution with the following code: int[] nums = [...]; /

Count the number of vowels in a file using Python

Image
Count the number of vowels in a file using Python Problem description : The program reads a text file entered by the user and counts the number of vowels in the text file. Prerequisite :  Files concepts Procedure : Start. Read the file. Store the file in a string. initialize a count variable. Iterate through the string. check if the character is a vowel. increment the count variable. End. Input file : demofile.txt Techieebytes is a tech blog where you can find lot of programming exercises and solutions. The programming exercise can be solved in any of the programming language. In this program we are going to  count the number of vowels in a text file. Code : #Method 1 (using or operator) #open the text file in read mode f = open("demofile.txt", "r") #read the file and the contents will be stored as string in s. s = f.read() #initialize count variable count = 0 for i in s:          #if the letter is a vowel if(i=="a" or i=="e" or i=="i"

Allocation in python - Kickstart solution

Solution for Kickstart problem Allocation in python Problem There are  N  houses for sale. The i-th house costs  A i  dollars to buy. You have a budget of  B  dollars to spend. What is the maximum number of houses you can buy? Input The first line of the input gives the number of test cases,  T .  T  test cases follow. Each test case begins with a single line containing the two integers  N  and  B . The second line contains  N  integers. The i-th integer is  A i , the cost of the i-th house. Output For each test case, output one line containing  Case #x: y , where  x  is the test case number (starting from 1) and  y  is the maximum number of houses you can buy. Limits Time limit: 15 seconds per test set. Memory limit: 1GB. 1 ≤  T  ≤ 100. 1 ≤  B  ≤ 10 5 . 1 ≤  A i  ≤ 1000, for all i. Test set 1 1 ≤  N  ≤ 100. Test set 2 1 ≤  N  ≤ 10 5 . Sample Input  3 4 100 20 90 40 90 4 50 30 30 10 10 3 300 999 999 999 Output  Case #1: 2 Case #2: 3 Case #3: 0 In Sample Case #1, you have a budget of 10

Python3 hackerRank exercise with solutions (Set 4)

1. Python If-Else  Given an integer, n, perform the following conditional actions: If   is odd, print  Weird If   is even and in the inclusive range of   to  , print  Not Weird If   is even and in the inclusive range of   to, print  Weird If   is even and greater than  , print  Not Weird Input format : A single line containing an integer N. (i.e.N>0) Output format : Print weird if the number is weird. Else print Not weird Sample input : 3 Sample output : Weird Code : n=int(input()) if((n>20)&(n%2==0)): print("Not weird") else: if(n%2==0): print("Not weird") if(n>2)&(n<5): print("Weird") elif(n<20)&(n>6): 2. Arithmetic Operators The code reads two integers a and b from user and print three lines where, the first line contains the sum of two integers, a+b the second line contains the difference of two integers, a-b the third line contains the product of two integers, a*b Input for

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

Image
  1.  Rearrangement Given a list A elements of length n , ranging from 1 to n. All elements may not be present in the array. If the element is not present then there will be -1 present in the array. Rearrange the array such that A[i]=i and if i is not present display -1 at that place. Input format : The first line contains a n numbers with each number separated by space. Output format : Print the elements of the list after modification. Sample input : -1 -1 6 1 9 3 2 -1 4 -1 Sample Output : -1 1 2 3 4 -1 6 -1 -1 9 Explanation : The modified list contains elements such that A[i]=i . Code : Output :   2.  Formula Write a program that calculates and prints the value according to the given formula , Q=square root of [(2*C*D)/H]. Following are the fixed values of C and H , C=50 ,H=30. D is the variable whose values should be input to your program in a comma separated sequence. Input format : A sequence of values for D with each value separated by comma. Output format : Print the sequence of