Hot Topics

Itron Solution
Technical Round
#include <stdio.h>
int main() {
int i, n, pos, ele, arr[100];
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to be inserted: ");
scanf("%d", &ele);
printf("Enter the position where the element should be inserted: ");
scanf("%d", &pos);
for (i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = ele;
printf("The updated array is: ");
for (i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/*
OUTPUT
Enter the number of elements in the array: 5
Enter the elements of the array: 1
2
3
4
6
Enter the element to be inserted: 8
Enter the position where the element should be inserted: 2
The updated array is: 1 8 2 3 4 6 */
def is_palindrome(s):
return s == s[::-1]
# test
s = "racecar"
print(is_palindrome(s)) # True
#OUTPUT - True
class Shape:
def __init__(self, type):
self._type = type
def draw(self):
pass
class Rectangle(Shape):
def draw(self):
print("Inside Rectangle::draw() method.")
class Square(Shape):
def draw(self):
print("Inside Square::draw() method.")
class Circle(Shape):
def draw(self):
print("Inside Circle::draw() method.")
class ShapeFactory:
def __init__(self):
self._creators = {}
def register_creator(self, type, creator):
self._creators[type] = creator
def create_shape(self, type):
if type not in self._creators:
raise ValueError("Invalid shape type")
return self._creators[type]()
factory = ShapeFactory()
factory.register_creator("circle", Circle)
factory.register_creator("rectangle", Rectangle)
factory.register_creator("square", Square)
shape = factory.create_shape("circle")
shape.draw()
In the above example, we register the different shape classes with the factory using the register_creator
method. When we want to create an instance of a specific shape, we call the create_shape
method and pass in the type of shape we want. The factory then looks up the corresponding class in the dictionary and returns an instance of that class. This way the factory is not dependent on any if-else if or switch-case construct.
class MyClass
{
static int s_count;
public:
MyClass()
{
++s_count;
}
static int getCount()
{
return s_count;
}
};
int MyClass::s_count = 0;
int main()
{
MyClass a, b, c;
cout << MyClass::getCount() << endl;
return 0;
}
/*
OUTPUT -
3
*/
In this example, the class MyClass
has a static integer member s_count
, which is shared by all objects of the class and is incremented each time an object is created. The getCount
function returns the current value of s_count
.
def find_unique(A, B):
result = []
for a in A:
found = False
for b in B:
if a == b:
found = True
break
if not found:
result.append(a)
return result
# test
A = [1, 2, 3, 4, 5]
B = [3, 4, 5, 6, 7]
print(find_unique(A, B))
# OUTPUT - [1,2]
In this example, the function find_unique
takes two arrays A
and B
as inputs and returns a list of elements in A
that are not in B
. The function uses a nested loop to compare each element in A
with each element in B
, and if a match is not found, the element is added to the result list.
import heapq
# Create a priority queue
priority_queue = []
# Add elements to the priority queue with different priorities
heapq.heappush(priority_queue, (3, 'low_priority_task'))
heapq.heappush(priority_queue, (1, 'high_priority_task'))
heapq.heappush(priority_queue, (2, 'normal_priority_task'))
# Remove elements from the priority queue
print(heapq.heappop(priority_queue)) # will print (1, 'high_priority_task')
print(heapq.heappop(priority_queue)) # will print (2, 'normal_priority_task')
print(heapq.heappop(priority_queue)) # will print (3, 'low_priority_task')
# OUTPUT
# (1, 'high_priority_task')
# (2, 'normal_priority_task')
# (3, 'low_priority_task')
Here the priority queue is implemented using the heapq module, we push the elements in the priority queue, giving them a priority value, in this case, the integers are the priority value of the elements, and the string is the element. And when we pop the elements from the priority queue, it returns the elements according to their priority value.




Popular Category
Hot Topics
Go through our study material. Your Job is awaiting.