Ai-Lab1 Cy
Ai-Lab1 Cy
Lab-1
Artificial Intelligence
Cyber Security
Instructors:
Example:
def reverse_string(s):
return s[::-1]
# Input
print(reverse_string("hello"))
Input: "hello"
Output: "olleh"
Example:
seen = {}
for i, num in enumerate(nums):
if complement in seen:
return [seen[complement], i]
seen[num] = i
# Input
3. FizzBuzz (Easy)
Write a function that prints numbers from 1 to n but for multiples of 3, print "Fizz" instead of the
number, and for multiples of 5, print "Buzz". For numbers that are multiples of both 3 and 5, print
"FizzBuzz".
Example:
def fizz_buzz(n):
result = []
if i % 3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(i)
return result
Input: n = 15
Output: [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11,
"Fizz", 13, 14, "FizzBuzz"]
Example:
def is_valid_parentheses(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping:
top_element = stack.pop() if stack else '#'
if mapping[char] != top_element:
return False
else:
stack.append(char)
return not stack
# Input
print(is_valid_parentheses("()[]{}"))
Input: "()[]{}"
Output: True
5. Merge Two Sorted Arrays (Easy)
Merge two sorted arrays into one sorted array.
Example:
# Input
# Output
# [1, 2, 3, 4, 5, 6]
Example:
def is_palindrome(s):
return s == s[::-1]
# Input
print(is_palindrome("racecar"))
# Output
# True
Input: "racecar"
Output: True
Example:
def max_subarray_sum(nums):
return max_sum
# Input
# Output
#6
Example:
def majority_element(nums):
count = {}
count[num] = count.get(num, 0) + 1
return num
# Input
print(majority_element([3, 2, 3]))
# Output
#3
Example:
# Input
print(are_anagrams("listen", "silent"))
# Output
# True
Example:
def find_duplicates(nums):
seen = set()
duplicates = set()
if num in seen:
duplicates.add(num)
else:
seen.add(num)
return list(duplicates)
# Input
print(find_duplicates([1, 2, 3, 1]))
# Output
# [1]
Tasks:
1. An attacker encoded malicious commands in reverse to evade detection. Write a
function to reverse the encoded string and reveal the original command.
2. You suspect two IPs are responsible for a combined unusual amount of traffic. Given a
list of traffic volumes, find the two IPs that sum to the suspicious traffic.
3. You are monitoring server logs. If a log appears every 3 seconds, tag it as "Ping"; every
5 seconds, tag it as "Pong"; and if it overlaps (15 seconds), tag it as "PingPong"
4. A security system uses nested encryption keys represented by parentheses, brackets,
and braces. Write a function to verify if the key structure is valid.
5. You need to merge sorted lists of blocked IPs from two different servers into one
comprehensive list.
6. An encrypted password is suspected to be a palindrome for simplicity. Verify if the
password meets this condition.
7. During a DDoS attack analysis, find the longest burst of consecutive traffic spikes
(subarray with the maximum sum).
8. An analysis of failed login attempts shows one IP address dominates. Identify the
majority IP from the log.
9. You suspect a phishing domain is using a scrambled version of your official domain.
Check if two domains are anagrams.
10.A log file contains duplicate entries of the same IPs. Find all duplicate IPs.