0% found this document useful (0 votes)
13 views10 pages

Ai-Lab1 Cy

The document outlines a lab session for Artificial Intelligence and Cyber Security, detailing a pool of programming questions and tasks. Each question includes a description, example code, and expected input/output, covering topics like string manipulation, array operations, and validation of data structures. Additionally, there are specific tasks related to security scenarios, such as reversing encoded strings and identifying suspicious traffic patterns.

Uploaded by

kelvin03zine
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)
13 views10 pages

Ai-Lab1 Cy

The document outlines a lab session for Artificial Intelligence and Cyber Security, detailing a pool of programming questions and tasks. Each question includes a description, example code, and expected input/output, covering topics like string manipulation, array operations, and validation of data structures. Additionally, there are specific tasks related to security scenarios, such as reversing encoded strings and identifying suspicious traffic patterns.

Uploaded by

kelvin03zine
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/ 10

Lab-1

Artificial Intelligence
Cyber Security

Instructors:

Mr. Ahsan Shakeel Malik & Ms.Nabeelah Maryam​


Questions Pool For Lab-1

1. Reverse a String (Easy)


Write a function to reverse a string.

Example:

def reverse_string(s):

return s[::-1]

# Input

print(reverse_string("hello"))

Input: "hello"
Output: "olleh"

2. Two Sum (Easy)


Find two numbers in a list that add up to a given sum.

Example:

def two_sum(nums, target):

seen = {}
for i, num in enumerate(nums):

complement = target - num

if complement in seen:

return [seen[complement], i]

seen[num] = i

# Input

print(two_sum([2, 7, 11, 15], 9))

Input: nums = [2, 7, 11, 15], target = 9


Output: [0, 1] # indices of the numbers

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 = []

for i in range(1, n + 1):

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"]

4. Valid Parentheses (Easy/Medium)


Check if a string has a balanced set of parentheses.

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:

def merge_sorted_arrays(nums1, nums2):

return sorted(nums1 + nums2)

# Input

print(merge_sorted_arrays([1, 3, 5], [2, 4, 6]))

# Output

# [1, 2, 3, 4, 5, 6]

Input: nums1 = [1, 3, 5], nums2 = [2, 4, 6]


Output: [1, 2, 3, 4, 5, 6]

6. Palindrome Check (Easy)


Check if a string is a palindrome.

Example:

def is_palindrome(s):

return s == s[::-1]
# Input

print(is_palindrome("racecar"))

# Output

# True

Input: "racecar"
Output: True

7. Maximum Subarray Sum (Medium)


Find the contiguous subarray with the maximum sum.



Example:

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

# Input

print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]))

# Output
#6

Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]


Output: 6 # Subarray is [4, -1, 2, 1]

8. Find the Majority Element (Medium)


Find the element that appears more than n/2 times in an array.

Example:

def majority_element(nums):

count = {}

for num in nums:

count[num] = count.get(num, 0) + 1

if count[num] > len(nums) // 2:

return num

# Input

print(majority_element([3, 2, 3]))

# Output

#3

Input: nums = [3, 2, 3]


Output: 3
9. Anagram Check (Easy)
Check if two strings are anagrams.

Example:

def are_anagrams(s1, s2):

return sorted(s1) == sorted(s2)

# Input

print(are_anagrams("listen", "silent"))

# Output

# True

Input: s1 = "listen", s2 = "silent"


Output: True

10. Find Duplicates in Array (Easy/Medium)


Find duplicate elements in an array.

Example:

def find_duplicates(nums):

seen = set()

duplicates = set()

for num in nums:

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.

You might also like