For All Experiences
sentence = "Hello world this is ramu"
words = sentence.split()
reversedwords = words[::-1]
reversedsentence = ' '.join(reversedwords)
print(reversedsentence)
from collections import Counter
s = "swiss"
# Count the frequency of each character
charcount = Counter(s)
# Find the first character with count 1
for char in s:
if charcount[char] == 1:
print(char)
break
sentence = "Python interview questions from skillupgearup"
# Split sentence into words
words = sentence.split()
# Find the longest word
longestword = max(words, key=len)
print(longestword)
str1 = "skillup"
str2 = "gearup"
# Convert strings to sets and find intersection
commonchars = set(str1) & set(str2)
print(commonchars)
s = "education"
vowels = "aeiouAEIOU"
result = ''.join(['*' if ch in vowels else ch for ch in s ])
def isprime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
primes = []
num = 2
while len(primes) < 10:
if isprime(num):
primes.append(num)
num += 1
print(primes)
#An Armstrong number (also called a narcissistic number) is a number that is equal to the sum of its digits raised to the power of the number of digits.
num = 153 # Example number
#Convert number to string to iterate over digits
digits = str(num)
power = len(digits)
Calculate sum of digits raised to the power
sumofpowers = sum(int(digit) ** power for digit in digits)
#Check if it equals the original number
if num == sumofpowers:
print(f”{num} is an Armstrong number”)
else:
print(f”{num} is not an Armstrong number”)
import math
a = 12
b = 18
# GCD
gcd = math.gcd(a, b)
print("GCD:", gcd)
# LCM (a * b) // GCD
lcm = (a * b) // gcd
print("LCM:", lcm)
s = "abc"
substrings = []
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
substrings.append(s[i:j])
print(substrings)
numbers = [10, 4, 7, 9, 2, 10]
# Remove duplicates and sort
uniquenumbers = list(set(numbers))
uniquenumbers.sort()
# Second largest is the second last element
secondlargest = uniquenumbers[-2]
print(secondlargest)
s = "hello world"
chartoremove = "l"
# Remove the character
result = s.replace(chartoremove, "")
print(result)
numbers = [1, 2, 4, 5, 6] # 3 is missing
n = max(numbers) # assuming list starts from 1
# Sum of first n numbers
totalsum = n * (n + 1) // 2
# Subtract sum of given numbers
missingnumber = totalsum - sum(numbers)
print(missingnumber)
Contains every letter of the alphabet at least once
import string
def ispangram(sentence):
# Convert sentence to lowercase and make a set of letters
letters = set(sentence.lower())
# Check if all alphabets are in the set
return set(string.asciilowercase).issubset(letters)
sentence = "The quick brown fox jumps over the lazy dog"
print(ispangram(sentence)) # Output: True
from itertools import permutations
s = "ABC"
# Generate all permutations
perm = permutations(s)
# Convert each permutation from tuple to string
permlist = [''.join(p) for p in perm]
print(permlist)
def romantoint(s):
romandict = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000}
total = 0
prevvalue = 0
for char in reversed(s): # Process from right to left
value = romandict[char]
if value < prevvalue:
total -= value
else:
total += value
prevvalue = value
return total
# Example
romannum = "MCMXCIV"
print(romantoint(romannum))
def inttoroman(num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4, 1
]
syms = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I"
]
roman = ""
for i in range(len(val)):
while num >= val[i]:
num -= val[i]
roman += syms[i]
return roman
# Example
number = 1994
print(inttoroman(number))
A Harshad number (or Niven number) is an integer that is divisible by the sum of its digits.
def isharshad(num):
# Sum of digits
digitsum = sum(int(digit) for digit in str(num))
# Check divisibility
return num % digitsum == 0
#example
number = 156
if isharshad(number):
print(f”{number} is a Harshad number”)
else:
print(f”{number} is not a Harshad number”)
n = 10
sumn = n * (n + 1) // 2 # Use // for integer division
print(sumn)
num = 234
product = 1
for digit in str(num):
product *= int(digit)
print(product)
s = "Hello World 123"
upper = lower = digits = 0
for char in s:
if char.isupper():
upper += 1
elif char.islower():
lower += 1
elif char.isdigit():
digits += 1
print("Uppercase letters:", upper)
print("Lowercase letters:", lower)
print("Digits:", digits)
#One Liner
s = "Hello World 123"
upper = sum(1 for c in s if c.isupper())
lower = sum(1 for c in s if c.islower())
digits = sum(1 for c in s if c.isdigit())
Leave a comment