0% found this document useful (0 votes)
0 views3 pages

Python Coding Questions Analyst

The document contains a series of Python coding questions along with their solutions. It covers topics such as string manipulation, data structures (stack and queue), finding duplicates, checking for palindromes, and merging sorted lists. Each function is accompanied by an example demonstrating its usage.

Uploaded by

romjan miraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Python Coding Questions Analyst

The document contains a series of Python coding questions along with their solutions. It covers topics such as string manipulation, data structures (stack and queue), finding duplicates, checking for palindromes, and merging sorted lists. Each function is accompanied by an example demonstrating its usage.

Uploaded by

romjan miraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Coding Questions and Answers

1. Reverse a String
def reverse_string(s):
return s[::-1]

# Example
print(reverse_string("analyst")) # Output: "tsylana"

2. Find Duplicates in a List


def find_duplicates(lst):
return set([x for x in lst if lst.count(x) > 1])

# Example
print(find_duplicates([1, 2, 3, 2, 4, 5, 1])) # Output: {1, 2}

3. Implement a Stack
class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)

def pop(self):
return self.stack.pop() if self.stack else None

def peek(self):
return self.stack[-1] if self.stack else None

def is_empty(self):
return len(self.stack) == 0

# Example
s = Stack()
s.push(10)
s.push(20)
print(s.pop()) # Output: 20

4. Check Palindrome
def is_palindrome(s):
cleaned = ''.join(filter(str.isalnum, s)).lower()
return cleaned == cleaned[::-1]

# Example
print(is_palindrome("Madam")) # Output: True
5. Find Missing Number
def find_missing_number(lst):
n = len(lst) + 1
expected_sum = n * (n + 1) // 2
actual_sum = sum(lst)
return expected_sum - actual_sum

# Example
print(find_missing_number([1, 2, 4, 5])) # Output: 3

6. Count Vowels and Consonants


def count_vowels_consonants(s):
vowels = "aeiou"
s = s.lower()
v_count = sum(1 for ch in s if ch in vowels and ch.isalpha())
c_count = sum(1 for ch in s if ch not in vowels and ch.isalpha())
return v_count, c_count

# Example
print(count_vowels_consonants("Hello Analyst")) # Output: (4, 7)

7. Merge Sorted Lists


def merge_sorted_lists(a, b):
result = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
result.extend(a[i:])
result.extend(b[j:])
return result

# Example
print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4, 5, 6]

8. Implement a Queue
from collections import deque

class Queue:
def __init__(self):
self.queue = deque()

def enqueue(self, item):


self.queue.append(item)

def dequeue(self):
return self.queue.popleft() if self.queue else None

def is_empty(self):
return len(self.queue) == 0

# Example
q = Queue()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1

9. Find Maximum Subarray Sum


def max_subarray_sum(nums):
max_sum = current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum

# Example
print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # Output: 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy