Week 11 App
Week 11 App
1) def find_largest(arr):
largest = arr[0]
second_largest = arr[1]
for i in range(2, len(arr)):
if arr[i] > largest:
second_largest = largest
largest = arr[i]
elif arr[i] > second_largest:
second_largest = arr[i]
return largest, second_largest
# Example usage
arr = [10, 20, 4, 45, 99]
largest, second_largest = find_largest(arr)
print("First largest number:", largest)
print("Second largest number:", second_largest)
2) def sum_even_odd(arr):
even_sum = 0
odd_sum = 0
for num in arr:
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
return even_sum, odd_sum
# Example usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_sum, odd_sum = sum_even_odd(arr)
print("Sum of even numbers:", even_sum)
print("Sum of odd numbers:", odd_sum)
4) def is_palindrome(word):
return word == word[::-1]
def find_palindromes(sentence):
palindromes = []
words = sentence.split()
for word in words:
if is_palindrome(word):
palindromes.append(word)
return palindromes
# Example usage
sentence = "A man a plan a canal Panama"
palindromes = find_palindromes(sentence)
print("Palindromic words in the sentence:", palindromes)
5) def remove_duplicates(arr):
seen = set()
result = []
for num in arr:
if num not in seen:
seen.add(num)
result.append(num)
return result
# Example usage
arr = [1, 2, 3, 2, 1, 4, 5, 4, 6, 7, 6, 8, 9]
unique_arr = remove_duplicates(arr)
print("List with duplicates removed:", unique_arr)
# Example usage
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
C = matrix_multiplication(A, B)
if C is not None:
print("Result of matrix multiplication:")
for row in C:
print(row)
8) import random
attempts = 0
while True:
try:
user_guess = int(input("Guess the number (between 1 and 100): "))
attempts += 1
9) import re
def is_strong_password(password):
# Define evaluation criteria
length_criteria = len(password) >= 8
lowercase_criteria = any(char.islower() for char in password)
uppercase_criteria = any(char.isupper() for char in password)
digit_criteria = any(char.isdigit() for char in password)
special_character_criteria = re.search(r'[!@#$%^&*()_+{}\[\]:;<>,.?~\\-]', password) is not
None
return all(criteria)
if not length_criteria:
print("- Password should be at least 8 characters long")
if not lowercase_criteria:
print("- Password should contain at least one lowercase letter")
if not uppercase_criteria:
print("- Password should contain at least one uppercase letter")
if not digit_criteria:
print("- Password should contain at least one digit")
if not special_character_criteria:
print("- Password should contain at least one special character (!@#$%^&*()_+{}[]:;<>,.?
~\\-)")
if n_terms <= 0:
print("Please enter a positive number of terms.")
else:
fibonacci_sequence = generate_fibonacci(n_terms)
print(f"Fibonacci sequence with {n_terms} terms: {fibonacci_sequence}")
Hackerrank questions:
1) # Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations
N = int(input())
L = input().split()
K = int(input())
C = list(combinations(L, K))
F = filter(lambda c: 'a' in c, C)
print("{0:.3}".format(len(list(F))/len(C)))
2) from itertools import groupby
for key, group in groupby(input()):
print('({}, {})'.format(len(list(group)), key), end=" ")