Practice Coding Problems (Batch DS301222)

1.Find the Greatest Common Divisor (GCD) of Two Numbers:

Write a program to find the greatest common divisor (GCD) of two given numbers. The GCD is the largest positive integer that divides each of the numbers without leaving a remainder. For example, the GCD of 24 and 36 is 12.

2.Check Whether a Number is Prime:

Write a program to check whether a given number is prime or not. A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29 are the first 10 prime numbers.

3.Find the Sum of an Array:

Write a program to find the sum of all the elements in an array. For example, if the array is [1, 2, 3, 4, 5], the output should be 15.

4.Count the Number of Words in a String:

Write a program to count the number of words in a given string. For example, if the input string is “The quick brown fox jumps over the lazy dog”, the output should be 9.

5.Sort an Array:

Write a program to sort an array of integers in ascending order. For example, if the array is [3, 1, 4, 1, 5, 9, 2, 6, 5], the output should be [1, 1, 2, 3, 4, 5, 5, 6, 9].

3 Likes

--------------------------->>>>> Answers <<< ------------------------------

Question :1

image

Question 2 :

image

Question 3 :

image

Question 4 :

image

Question 5 :

image

3 Likes

@sriv.ansh
Nice :+1:
Keep practicing

# GCD

def gcd(num1,num2):

    if num1< num2:
        min,max = num1,num2
    else:
        min,max = num2,num1
    
    isEvenMin = True if min%2==0 else False
    isEvenMax = True if max%2==0 else False

    if max%min ==0:
        return min
    else:
        if isEvenMin:
            if isEvenMax:
                for i in range(min//2,1,-1):
                    if max % i ==0:
                        return i
                return 1
            else:
                return 1
        elif not isEvenMin:
            for i in range(min,1,-1):
                if max%i==0 and min%i ==0:
                    return i
            return 1

print(gcd(3,5))
print(gcd(3,9))
print(gcd(8,24))
print(gcd(24,36))
print(gcd(18,7))
print(gcd(4,25))
# Prime Number
import math
def isPrime(num):
    if num ==1:
        return 'is not prime'
    elif num ==2 or num == 3:
        return 'is prime'
    else:
        for i in range(int(math.sqrt(num)),1,-1):
            if num %i ==0:
                return 'is not prime'
        return 'is prime'

print(isPrime(2))
print(isPrime(17))
print(isPrime(25))
print(isPrime(937))
# Array sum
def sumArray(arr):
    sum = 0
    for i in arr:
        sum+=i
    return sum

def arrSum(arr):
    return sum(arr)
print(sumArray([1,2,3,4,5,6]))
print(arrSum([23,25,2,7,12,3]))
# Word Count in String
import re

def wordCount(s):
    count =0
    for i in s:
        if i == ' ':
            count +=1
    return count

def wordCount1(s):
    return len(s.split(' '))

def wordCount2(s):
    return len(re.split('\s',s))

print(wordCount('This is first'))
print(wordCount1('Now again this one'))
print(wordCount2('while at it this one too'))
# sort
def sortArr(arr):
    return sorted(arr)

print(sortArr([3,2,1,5,4,8,9,6,7]))
2 Likes

@kharshavardhan31
Well done :+1:

Keep Growing @sriv.ansh Keep Practicing

All are correct @kharshavardhan31 Keep Practicing