Write a python program for basic HTML parser
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print(tag)
for attr in attrs:
print("->", attr[0], ">", attr[1])
parser = MyHTMLParser()
for i in range(int(input())):
parser.feed(input())
Write a python function for Named Entity Recognizer using NLTK
def ner_checker(texts):
all_set = set()
def nltk_ner_check(texts):
for i, text in texts:
for entity in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(text))):
if isinstance(entity, nltk.tree.Tree):
etext = " ".join([word for word, tag in entity.leaves()])
# label = entity.label()
all_set.add(etext)
nltk_ner_check(texts=texts)
return all_set
Write a function to compress a given string. Suppose a character 'c' occurs consecutively X times in the string. Replace these consecutive occurrences of the character 'c' with (X, c) in the string.
def compress(text):
from itertools import groupby
for k, g in groupby(text):
print("({}, {})".format(len(list(g)), k), end=" ")
Write a python function to count 'a's in the repetition of a given string 'n' times.
def repeated_string(s, n):
return s.count('a') * (n // len(s)) + s[:n % len(s)].count('a')
Write a python function to find all the substrings of given string that contains 2 or more vowels. Also, these substrings must lie in between 2 consonants and should contain vowels only.
def find_substr():
import re
v = "aeiou"
c = "qwrtypsdfghjklzxcvbnm"
m = re.findall(r"(?<=[%s])([%s]{2,})[%s]" % (c, v, c), input(), flags=re.I)
print('\n'.join(m or ['-1']))
Write a python function that given five positive integers and find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
def min_max():
nums = [int(x) for x in input().strip().split(' ')]
print(sum(nums) - max(nums), sum(nums) - min(nums))
Write a python function to find the number of (i, j) pairs where i<j and ar[i]+ar[j] is divisible by k in a data list
def divisible_sum_pairs(arr, k):
count = 0
n = len(arr)
for i in range(n - 1):
j = i + 1
while j < n:
if ((arr[i] + arr[j]) % k) == 0:
count += 1
j += 1
return count
import math
Write a python Class to calculate area of a circle and print the vale for a radius
class CircleArea:
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius * self.radius
r = 2
obj = CircleArea(r)
print("Area of circle:", obj.area())
Write a python function to count the number of Words in a Text File
def check_words():
fname = input("file name: ")
num_words = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
print("Number of words = ", num_words)
Write a python function to Count the Number of Lines in a Text File
def check_lines():
fname = input("file name: ")
num_lines = 0
with open(fname, 'r') as f:
for line in f:
num_lines += 1
print("Number of lines = ", num_lines)
Write a python function that Counts the Number of Blank Spaces in a Text File
def count_blank_space():
fname = input("file name:")
count = 0
with open(fname, 'r') as f:
for line in f:
count += line.count(' ')
return count
Write a python function to check if 2 strings are anagrams or not
def anagram(s1, s2):
if sorted(s1) == sorted(s2):
return True
else:
return False
Write a python function to remove the duplicate items from a List and return the modified data list
def remove_duplicates(data):
c = Counter(data)
s = set(data)
for item in s:
count = c.get(item)
while count > 1:
data.pop(item)
count -= 1
return data