Professional Documents
Culture Documents
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;
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;
int arr[size];
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;
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>
int main() {
// Aspect 1: Function without arguments and without return value
checkPrimeWithoutArguments();
if (checkPrimeWithArguments(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
// Display result
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
}
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 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."
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="")