Appearance
question:Construct a Python program that creates the following string: 999,888,777,666,555.
answer:# This program creates the string "999,888,777,666,555". # create a list of numbers list = [999, 888, 777, 666, 555] # join the values in the list output = ",".join([str(item) for item in list]) # print the output print(output) # Outputs: 999,888,777,666,555
question:Create a Python module that implements a simple Stack. The module should have functions for push, pop, peek, size, and empty operations. Not applicable
answer:class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def empty(self): return self.size() == 0
question:Create a language detection system using Python and NLTK. Not applicable
answer:import nltk def detect_language(text): tokens = nltk.word_tokenize(text) tagged = nltk.pos_tag(tokens) language_probabilities = {} for i in range(0, len(tagged)): if tagged[i][1] in language_probabilities: language_probabilities[tagged[i][1]] += 1 else: language_probabilities[tagged[i][1]] = 1 # Find most probable language language = max(language_probabilities, key=lambda key: language_probabilities[key]) return language text = 'This is a sentence in English' language = detect_language(text) print(language) # Outputs 'English'
question:Design a python program to calculate the sum of digits of an integer.
answer:def sumDigits(num): # convert number into string # and convert each digit into number # using ord() and store it in list # convert number into string # and convert each digit into number # using ord() and store it in list strNum = str(num) digits = [int(x) for x in strNum] # return sum using sum() function return sum(digits) # Driver program num = 12345 print("Sum of digits in num is:", sumDigits(num))