How to Reverse a String in Python
Python string library doesn’t support the in-built “reverse()” as done by other python containers like list, hence knowing other methods to reverse string can prove to be useful
1) Using loop
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "OkGuru"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using loops) is : ",end="")
print (reverse(s))
Output:
The original string is : OkGuru
The reversed string(using loops) is : uruGkO
2) Using recursion
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
s = "OkGuru"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using recursion) is : ",end="")
print (reverse(s))
Output
The original string is : OkGuru
The reversed string(using recursion) is : uruGkO
3) Using extended slice syntax
# Function to reverse a string
def reverse(string):
string = string[::-1]
s = "OkGuru"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using extended slice syntax) is : ",end="")
print (reverse(s))
Output:
The original string is : OkGuru
The reversed string(using extended slice syntax) is :uruGkO
The original string is : OkGuru
The reversed string(using extended slice syntax) is :uruGkO
4) Using reversed
# Function to reverse a string
def reverse(string):
string = "".join(reversed(string))
return string
s = "OkGuru"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using reversed) is : ",end="")
print (reverse(s))
Output
The original string is : OkGuru
The reversed string(using reversed) is : uruGkO
Return maximum occurring character in an input string
Method 1
# Python program to return the maximum occurring character in the input string
ASCII_SIZE = 256
def getMaxOccuringChar(str):
# Create array to keep the count of individual characters
# Initialize the count array to zero
count = [0] * ASCII_SIZE
# Utility variables
max = -1
c = ''
# Traversing through the string and maintaining the count of
# each character
for i in str:
count[ord(i)]+=1;
for i in str:
if max < count[ord(i)]:
max = count[ord(i)]
c = i
return c
# Driver program to test the above function
str = "sample string"
print "Max occurring character is " + getMaxOccuringChar(str)
Method2
Program to check if characters of a given string can be rearranged to form a palindrome
Algorithm:
We can do it in O(n) time using a count array. Following are detailed steps.
- Create a count array of alphabet size which is typically 256. Initialize all values of count array as 0.
- Traverse the given string and increment count of every character.
- Traverse the count array and if the count array has more than one odd values, return false. Otherwise, return true.
Write a python function to send email notification.
Input: Receiver email address, subject line , message body
The idea is to reset the count as 1 as soon as we find a character not matching with previous.
Write a function to save python dictionary to file which takes dict and file as input
Also write another function to load the saved dictionary into python and load function must take file to the saved dictionary as the input and return the dictionary object
How to search for a pattern in log file
Find maximum consecutive repeating character in string
How to search for a pattern in log file
Find maximum consecutive repeating character in string
Input : str = "seeekk"
Output : e
Input : str = "aaaabbcbbb"
Output : a
The idea is to reset the count as 1 as soon as we find a character not matching with previous.
Suppose you were scanning for a 3-way handshake between an FTP server and a client and you were successful in doing so too. But as you guys might already know, passwords are never really stored in plain text. They are always hashed before being stored in a database and normally the hash itself is compared for verification purposes. Let us create a small Python program that can be used to crack a password using the dictionary attack method.
import hashlib
flag = 0
pass_hash = input("md5 hash: ")
wordlist = input("File name: ")
try:
pass_file = open(wordlist,"r")
except:
print("No file found :(")
quit()
for word in pass_file:
enc_wrd =word.encode('utf-8')
digest =hashlib.md5(enc_wrd.strip()).hexdigest()
# print(word)
# print(digest)
# print(pass_hash)
if digest.strip() == pass_hash.strip():
print("password found")
print("Password is " + word)
flag = 1
break
if flag == 0:
print("password not in list")
Write a program to find most frequent element in an array
Examples:
Input : arr[] = {1, 3, 2, 1, 4, 1}
Output : 1
1 appears three times in array which
is maximum frequency.
Input : arr[] = {10, 20, 10, 20, 30, 20, 20}
Output : 20
What happened to Python Interview question section ?
ReplyDeletehttps://okayguru.blogspot.com/2020/06/python-interview-question-question-bank.html
This post is removed ?
Post is back
Delete