You are on page 1of 9

SRM Institute of Science and Technology

College of Engineering and Technology SET6


School of Computing
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamil Nadu
Academic Year: 2023-24 (ODD)
21CSS101J & Programming for Problem Solving

SET 6-Answer Key for CT2

1. You are given an integer array nums. A number x is said to be ‘lonely’ when it appears only once, and no adjacent
numbers (i.e. x + 1 and x - 1) appear in the array. Return all lonely numbers in nums. You may return the answer in
any order using the C program.
Input:nums = [10,6,5,8]
Output: [10,8]
Mandatory Keywords: int nums[10];, for, if

Solution:
#include <stdio.h>

int main() {
int nums[100]; // Assuming a maximum of 100 elements in the array
int numsSize;

// Prompt the user to enter the number of elements in the array


printf("Enter the number of elements in the array: ");
scanf("%d", &numsSize);

// Prompt the user to enter each element


printf("Enter the elements of the array:\n");
for (inti = 0; i<numsSize; i++)
{
scanf("%d", &nums[i]);
}
int result[100]; // Assuming a maximum of 100 elements in the result array
intreturnSize = 0;

for (inti = 0; i<numsSize; i++)


{
intisLonely = 1;
for (int j = 0; j <numsSize; j++)
{
if (i != j && (nums[i] == nums[j] || nums[i] + 1 == nums[j] || nums[i] - 1 == nums[j]))
{
isLonely = 0;
break;
}
}
if (isLonely) {
result[returnSize++] = nums[i];
}
}

printf("Lonely Numbers: [");


for (inti = 0; i<returnSize; i++) {
printf("%d", result[i]);
if (i<returnSize - 1) {
printf(", ");
}
}
printf("]\n");
return 0;
}

Input:
Enter the number of elements in the array: 4
Enter the elements of the array:
10
6
5
8
Output:
Lonely Numbers: [10, 8]

2. You have taken on the task of developing a C program designed to illustrate the practical application of pointer
arithmetic. In this program, you define an array of integers and showcase how pointers can be employed to execute
various operations such as advancing and reversing the pointer, accessing specific elements, and calculating the sum
of the elements.
Input Format:
The program prompts the user to enter the size of an array, and then requests the input of array elements.
Output Format:
The program displays the original array , advances the pointer , reverses the pointer , and calculates the sum of the
array elements.
Mandatory Keywords: int *ptr = arr;, for, if

Solution:
#include <stdio.h>

int main() {
int size;

// Prompt the user to enter the size of the array


printf("Enter the size of the array: ");
scanf("%d", &size);

// Check for an empty array


if (size <= 0) {
printf("The array is empty.\n");
return 0; }

int arr[size];

// Input array elements


printf("Enter %d elements for the array:\n", size);
for (inti = 0; i< size; i++) {
scanf("%d", &arr[i]);
}

// Pointer to the beginning of the array


int *ptr = arr;

// Demonstrate pointer arithmetic


printf("Original Array: ");
for (inti = 0; i< size; i++) {
printf("%d ", *(ptr + i)); // Accessing elements using pointer arithmetic
}
printf("\n");

// Advancing the pointer


ptr++; // Move the pointer to the next element
printf("After advancing the pointer: ");
printf("%d\n", *ptr); // This should print the second element

// Reversing the pointer


ptr--; // Move the pointer back to the previous element
printf("After reversing the pointer: ");
printf("%d\n", *ptr); // This should print the first element

// Calculating the sum of the elements using pointer arithmetic


int sum = 0;
int *end = arr + size; // Pointer to one past the last element
while (ptr < end) {
sum += *ptr;
ptr++; // Move the pointer to the next element
}
printf("Sum of elements: %d\n", sum);

return 0;
}

Input:
Enter the size of array: 8
Enter the array Elements:
1
2
3
4
5
6
7
8
Output:
Original Array: 1 2 3 4 5 6 7 8
After advancing the pointer: 2
After reversing the pointer: 1
Sum of elements: 36

3. Aryan is very upset that nowadays, many people mix uppercase and lowercase letters in their words. So, he decided
to developan extension for his favorite browser that would change the letters' in every word so that it either consists
of lowercase letters or uppercase letters. The decision for changing is based on the maximum number of uppercase or
lowercase letters in the word. Write a C program to implement the required logic.
Mandatory Keywords: char word[10];, for, if Input: ViP
Input and Output Output: VIP
Input: maTRIx
Input: HoUse Output: matrix
Output: house

Solution:
#include <stdio.h>
#include <string.h>

int main() {
char word[101]; // The maximum length of the word is 100 characters

scanf("%s", word);

intlen = strlen(word);
intuppercaseCount = 0;
intlowercaseCount = 0;

// Count the number of uppercase and lowercase letters


for (inti = 0; i<len; i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
lowercaseCount++;
} else {
uppercaseCount++;
}
}

// Convert the word to lowercase or uppercase


if (lowercaseCount>= uppercaseCount) {
for (inti = 0; i<len; i++) {
if (word[i] >= 'A' && word[i] <= 'Z') {
word[i] += 'a' - 'A'; // Convert uppercase to lowercase
}
}
} else {
for (inti = 0; i<len; i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
word[i] -= 'a' - 'A'; // Convert lowercase to uppercase
}
}
}

printf("%s\n", word);

return 0;
}

Input:
HoUse
Output:
House
4. Sam and Kiran, are friends, have attended the topic on function in programming class. However, they are facing
difficulties in understanding how to write a program using different aspects of function calling. Can you help them to
write a program to find a given number is prime or not using the various aspects.
Write a C program with two different aspects of function calls.
• function without arguments and without return value.
• function with arguments and with return value

Solution:
#include <stdio.h>

// Function without arguments and without return value


void checkPrimeWithoutArguments();

// Function with arguments and with return value


int checkPrimeWithArguments(int num);

int main() {
// Aspect 1: Function without arguments and without return value
checkPrimeWithoutArguments();

// Aspect 2: Function with arguments and with return value


int number;
printf("Enter a number to check if it's prime: ");
scanf("%d", &number);

if (checkPrimeWithArguments(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

return 0;
}

// Function without arguments and without return value


void checkPrimeWithoutArguments() {
int num, i, flag = 0;

printf("Enter a number to check if it's prime: ");


scanf("%d", &num);

// Check for prime


for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}

// Display result
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
}

// Function with arguments and with return value


int checkPrimeWithArguments(int num) {
int i;

// Check for prime


for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
return 0; // Not a prime number
}
}

return 1; // Prime number


}

Input and Output:


Enter a number to check if it's prime: 3
3 is a prime number.
Enter a number to check if it's prime: 4
4 is not a prime number.

5. You are given a string S. You have to determine whether it is possible to build the string S out of
strings aa, aaa, bb, bbb by concatenating them. You can use the strings aa, aaa, bb, bbb any number of times and in
any order. Write a C program to implement the required logic. Print “YES” if the given string is built from aa, aaa,
bb, bbb. Else print “NO”
For example:
Input: Output:
● aaaabbb can be built as aa ++ aa ++ bbb; bbaaaaabbb YES
● bbaaaaabbb can be built as bb ++ aaa ++ aa ++ bbb;
aaaaaa YES
● aaaaaa can be built as aa ++ aa ++ aa;
abab NO
● abab cannot be built from aa, aaa, bb, bbb.
[Hint: ++ denotes concatenate] a NO

Solution:
#include <stdio.h>
#include <string.h>

int canBuildString(char s[]) {


int len = strlen(s);
int i = 0;

while (i < len) {


// Check if s[i] is 'a' and if we can match "aaa" or "aa"
if (s[i] == 'a') {
if (i + 2 < len && s[i + 1] == 'a' && s[i + 2] == 'a') {
i += 3;
} else if (i + 1 < len && s[i + 1] == 'a') {
i += 2;
} else {
return 0; // Cannot match "aa" or "aaa"
}
}
// Check if s[i] is 'b' and if we can match "bbb" or "bb"
else if (s[i] == 'b') {
if (i + 2 < len && s[i + 1] == 'b' && s[i + 2] == 'b') {
i += 3;
} else if (i + 1 < len && s[i + 1] == 'b') {
i += 2;
} else {
return 0; // Cannot match "bb" or "bbb"
}
} else {
return 0; // Neither 'a' nor 'b' in the input string
}
}

return 1; // Successfully matched the entire input string


}

int main() {
int t;
scanf("%d", &t);

while (t--) {
char s[55]; // The maximum length is 50, plus 1 for null character
scanf("%s", s);

if (canBuildString(s)) {
printf("YES\n");
} else {
printf("NO\n");
}
}

return 0;
}

Input:
2
aaaaabbb
bbaaaaabbb

Output:
YES
YES

6. You are developing a password strength checker for a website. The password strength is assessed based on the
following criteria:
 The password must be at least 8 characters long.
 It must contain both uppercase and lowercase letters.
 It should have at least one digit.
 It should include at least one special character (e.g., @, #, $).
Write a Python program that takes a user's password as input and checks its strength based on the specified criteria.
Provide appropriate feedback to the user indicating whether the password is strong or needs improvement.
Certainly! In general, for the given password strength checker program:

Input Format:
The user is prompted to input their password. The program expects the user to provide a string representing their
chosen password.
Output Format:
The program provides feedback to the user based on the strength of the entered password. The output could fall into
one of the following categories:
- "Strong: Password meets all criteria. Good job!"
- "Weak: Password should be at least 8 characters long."
- "Weak: Password should contain both uppercase and lowercase letters."
- "Weak: Password should include at least one digit."
- "Weak: Password should include at least one special character (e.g., @, #, $)."

Solution:
defcheck_password_strength(password):
# Criteria 1: Password must be at least 8 characters long
if len(password) < 8:
return "Weak: Password should be at least 8 characters long."

# Criteria 2: Password must contain both uppercase and lowercase letters


if not any(c.isupper() for c in password) or not any(c.islower() for c in password):
return "Weak: Password should contain both uppercase and lowercase letters."

# Criteria 3: Password must have at least one digit


if not any(c.isdigit() for c in password):
return "Weak: Password should include at least one digit."

# Criteria 4: Password must include at least one special character


special_characters = set('@#$%^&+=')
if not any(char in special_characters for char in password):
return "Weak: Password should include at least one special character (@, #, $, etc.)."

# If all criteria are met, the password is strong


return "Strong: Password meets all criteria. Good job!"

# Test the function with a password


user_password = input("Enter your password: ")
result = check_password_strength(user_password)
print(result)

Input: "Pass123"
Expected Output: "Weak: Password should be at least 8 characters long."

Input: "SecureP@ss123"
Expected Output: "Strong: Password meets all criteria. Good job!"

7.a . Given two dictionaries d1 and d2, write a function mergeDic that accepts two dictionaries
d1 and d2 and return a new dictionary by merging d1 and d2.
Note: Contents of d1 should be appear before contents of d2 in the new dictionary and in same order. In case of
duplicate value retain the value present in d1.
Input Format:
The function mergeDic takes two dictionaries, d1 and d2, as input.
Output Format:
The function returns a new dictionary obtained by merging d1 and d2.
Solution:
def mergeDic(d1, d2):
for key in d2.keys():
if key not in d1:
d1[key] = d2[key]
return d1

# Example usage:
d1 = {1: 1, 2: 2}
d2 = {3: 3, 4: 4}
merged_dict = mergeDic(d1, d2)
print(merged_dict)

Input:
{1: 1, 2: 2}
{3: 3, 4: 4}
Output:
{1: 1, 2: 2, 3: 3, 4: 4}

7.bWrite a program that take list L as an input and shifts all zeroes in list L towards the right by maintaining the order
of the list. Also print the new list.
Input Format:
The program takes a list of integers, L , as input.
Output Format:
The program prints a new list after moving all the zeros to the right while keeping the order of other elements.

Solution:
L = list(map(int, input().split()))

zeroes = L.count(0)
i=0
while i<len(L) - zeroes:
if L[i] == 0:
L.pop(i)
L.append(0)
i -= 1
i += 1
print(L, end="")

Input : list1 = [1, 2, 0, 4, 3, 0, 5, 0]


Output : list1 = [1, 2, 4, 3, 5, 0, 0, 0]

You might also like