Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Programming_Solutions_Python

The document provides solutions to various programming, Selenium, and API questions using Python. It covers basic data structures, programming tasks such as finding the largest and smallest numbers in an array, checking for palindromes, and automating login functionality with Selenium. Additionally, it includes examples of reversing a string, implementing FizzBuzz, and making API calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Programming_Solutions_Python

The document provides solutions to various programming, Selenium, and API questions using Python. It covers basic data structures, programming tasks such as finding the largest and smallest numbers in an array, checking for palindromes, and automating login functionality with Selenium. Additionally, it includes examples of reversing a string, implementing FizzBuzz, and making API calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Solutions to Programming, Selenium, and API Questions (Python)

### 1. Basic Data Structures

#### a) Difference between Array and Linked List:


- **Array**: Fixed size, contiguous memory, direct access by index.
- **Linked List**: Dynamic size, non-contiguous memory, sequential
access.

#### b) Binary Search Tree (BST) Implementation (Python):


```python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

class BST:
def __init__(self):
self.root = None

def insert(self, value):


if not self.root:
self.root = Node(value)
else:
self._insert_rec(self.root, value)
def _insert_rec(self, root, value):
if value < root.value:
if not root.left:
root.left = Node(value)
else:
self._insert_rec(root.left, value)
elif value > root.value:
if not root.right:
root.right = Node(value)
else:
self._insert_rec(root.right, value)

def in_order(self):
result = []
self._in_order_rec(self.root, result)
return result

def _in_order_rec(self, root, result):


if root:
self._in_order_rec(root.left, result)
result.append(root.value)
self._in_order_rec(root.right, result)

# Usage:
# tree = BST()
# tree.insert(10); tree.insert(20); tree.insert(5)
# print(tree.in_order()) # Output: [5, 10, 20]
```

### 2. Programming Questions

#### a) Largest and Smallest in Array (Python):


```python
arr = [10, 5, 20, 8]
max_val = max(arr)
min_val = min(arr)
print("Max:", max_val, "Min:", min_val)
```

#### b) Check if String is Palindrome (Python):


```python
def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam")) # Output: True


```

### 3. Selenium Questions

#### a) Automate Login Functionality (Python):


```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com/login")
driver.find_element("id", "username").send_keys("invalid_user")
driver.find_element("id", "password").send_keys("wrong_password")
driver.find_element("id", "loginButton").click()
error_msg = driver.find_element("id", "error").text
print("Error:", error_msg)
driver.quit()
```

### 4. Python Solutions

#### Strings - Reverse a String:


```python
str = "hello"
reversed_str = str[::-1]
print(reversed_str) # Output: "olleh"
```

#### Control Structures - FizzBuzz:


```python
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
```

### 5. API Knowledge

#### API Call in Python:


```python
import requests

response = requests.get("https://api.example.com/data")
if response.status_code == 200:
print("Response:", response.json())
else:
print("Error:", response.status_code)
```

You might also like