Hi Coder Buddies
Sharing some quick solutions on 8 beginner practice problems I have come across while learning Python.
For downloading the notebook, please visit -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Given a number, find the sum of its digits. Take the number as an input from the user. | |
num1 = input('enter the number 1') | |
num2 = input('enter the number 2') | |
print(int(num1)+int(num2)) | |
sum = 0 | |
sum = int(input('enter the number 1')) | |
sum += int(input('enter the number 2')) | |
print(sum) | |
# 2. Given a number, check whether the given number is an Armstrong number or not. A positive integer is called an Armstrong number of order n if: | |
abcd... = an + bn + cn + dn + ... | |
Example: 153 = 1*1*1 + 5*5*5 + 3*3*3 153 is an Armstrong number of order 3. | |
Inputs from the user will be number and order n. | |
num = input('enter the number:') | |
arr = [] | |
arr[:] = num | |
print(arr) | |
sum = 0 | |
for char in num: | |
print('char='+ char) | |
i = int(char) | |
sum += i*i*i | |
print('cubeSum=' + str(sum)) | |
if(int(num) == sum): | |
print('Armstrong') | |
else: | |
print('Not Armstrong') | |
num = input('enter the number:') | |
order = input('enter the order:') | |
print(arr) | |
sum = 0 | |
for char in num: | |
print('char='+ char) | |
sum += int(char)**int(order) | |
print('sum=' + str(sum)) | |
if(int(num) == sum): | |
print('Armstrong') | |
else: | |
print('Not Armstrong') | |
# 3. Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “malayalam” is a palindrome, but “music” is not a palindrome. | |
str = input('enter the string:') | |
#print(len(str)) | |
length = len(str) | |
i = 0 | |
flag = bool(1) | |
for char in str: | |
i += 1 | |
#print('char = ' + char) | |
#print('str[length-i]' + str[length-i]) | |
if (length-i >= length/2) & (char != str[length-i]): | |
flag = bool(0) | |
print(flag) | |
break | |
if (flag): | |
print('palindrome') | |
else: | |
print('not a palindrome') | |
# txt = "Hello World" [::-1] | |
str = input('enter the string:') | |
revStr = str[::-1] | |
if (str == revStr): | |
print('palindrome') | |
else: | |
print('not a palindrome') | |
# 4. Given an array which may contain duplicates, print all elements and their frequencies. | |
str = input('enter the string:') | |
dict = {} | |
for char in str: | |
if char in dict: | |
dict.__setitem__(char,dict.get(char) + 1) | |
else: | |
dict.__setitem__(char,1) | |
print(dict) | |
# 5. Given a number n, write a function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3”. | |
Suppose n is not a prime number (greater than 1). So there are numbers a and b such that | |
n = ab (1 < a <= b < n) | |
By multiplying the relation a<=b by a and b we get: | |
a^2 <= ab | |
ab <= b^2 | |
Therefore: (note that n=ab) | |
a^2 <= n <= b^2 | |
Hence: (Note that a and b are positive) | |
a <= sqrt(n) <= b | |
So if a number (greater than 1) is not prime and we test divisibility up to square root of the number, we will find one of the factors. | |
import math | |
def findPrimeFactors(n): | |
while n % 2 == 0: | |
print(2) | |
n = n / 2 | |
for i in range(3,int(math.sqrt(n))+1,2): | |
while n % i== 0: | |
print(i) | |
n = n / i | |
if n > 2: | |
print(n) | |
findPrimeFactors(100) | |
# 6. Given two numbers n and r, find the value of nCr (binomial coefficient: nCr = (n!) / (r! * (n-r)!)) | |
def fact(num): | |
n = int(num) | |
if n <= 1: | |
return 1 | |
else: | |
return n*fact(n-1) | |
#print(fact(int(5))) | |
n = 4 | |
r = 2 | |
coeff = fact(n) / (fact(r) * fact(n-r)) | |
print(coeff) | |
# 7. Searching: Given a sorted array arr[] of n elements, write a function to search a given element x in arr[]. Do it using linear and binary search techniques. | |
arr = [1,2,3,4,5] | |
n = 323 | |
def func(arr, n): | |
flag = bool(0) | |
for i in arr: | |
if(i == n): | |
flag = bool(1) | |
break | |
if (flag): | |
print('Found!!') | |
else: | |
print('Not Found!') | |
def funcBin(arr, mn, mx, n): | |
mid = (mn + mx)/2 | |
# print('mid - ') | |
# print(mid) | |
# print('mn - ') | |
# print(mn) | |
# print('mx - ') | |
# print(mx) | |
# print('============') | |
if (arr[int(mid)] == n): | |
print('Found!!') | |
elif (n > arr[int(mid)] & mn <= mx & mx < len(arr)): | |
mn = int(mid + 1) | |
# print('min - ') | |
# print(mn) | |
funcBin(arr, mn, mx, n) | |
elif (n < arr[int(mid)] & mn <= mx & mx < len(arr)): | |
mx = int(mid - 1) | |
# print('max - ') | |
# print(mx) | |
funcBin(arr, mn, mx, n) | |
func(arr, n) | |
funcBin(arr, 0, 4, n) | |
# 8. Input a text file (containing 1 or more paragraphs of English text) from the user, parse this file to display the frequency of occurrence of each word in this text file. Find the 3 most frequent words as well. | |
with open('/Users/namitsha/Downloads/testData.txt', 'r') as file: | |
lines = file.readlines() | |
dict = {} | |
for line in lines: | |
#s = line | |
#print(s) | |
words = line.split(' ') | |
for word in words: | |
#print(word) | |
if word.strip(): | |
if word.strip() in dict: | |
#print('Updating ' + word.strip()) | |
dict.__setitem__(word.strip(),dict.get(word.strip()) + 1) | |
else: | |
#print('Adding ' + word.strip()) | |
dict.__setitem__(word.strip(),1) | |
print(dict) | |