0% found this document useful (0 votes)
5 views8 pages

Accenture

Uploaded by

akashkumar4280
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)
5 views8 pages

Accenture

Uploaded by

akashkumar4280
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/ 8

#Accenture 2nd Coding Class:

Explanation Video:
https://www.youtube.com/live/uK
mqSy7qKBo?si=I6zSMpiHHq6Yc
PJf

Q1.
Python
def Xor(a,b):
return a^b

def OR(a,b):
return a|b

def AND(a,b):
return a&b

def CalculateBinaryOperations(string):
ans=0
if string[1]=='C':
ans=Xor(int(string[0]),int(string[2]))
elif string[1]=='B':
ans=OR(int(string[0]),int(string[2]))
else:
ans=AND(int(string[0]),int(string[2]))

i=3
while i<len(string):
if string[i]=='C':

ans=Xor(ans,int(string[i+1]))

elif string[i]=='B':
ans=OR(ans,int(string[i+1]))

else:
ans=AND(ans,int(string[i+1]))

i+=2

return ans

C++
#include <iostream>
#include <string>
int Xor(int a, int b) {
return a ^ b;
}

int OR(int a, int b) {


return a | b;
}

int AND(int a, int b) {


return a & b;
}

int calculateBinaryOperations(const std::string& str) {


int ans = 0;

if (str[1] == 'C') {
ans = Xor(static_cast<int>(str[0] - '0'), static_cast<int>(str[2] - '0'));
} else if (str[1] == 'B') {
ans = OR(static_cast<int>(str[0] - '0'), static_cast<int>(str[2] - '0'));
} else {
ans = AND(static_cast<int>(str[0] - '0'), static_cast<int>(str[2] - '0'));
}

int i = 3;
while (i < str.size()) {
if (str[i] == 'C') {
ans = Xor(ans, static_cast<int>(str[i + 1] - '0'));
} else if (str[i] == 'B') {
ans = OR(ans, static_cast<int>(str[i + 1] - '0'));
} else {
ans = AND(ans, static_cast<int>(str[i + 1] - '0'));
}

i += 2;
}

return ans;
}

int main() {
// Example usage
std::string input = "1C0B1A1"; // Example string "1C0B1A1" corresponds to 1 XOR 0 OR 1
AND 1
int result = calculateBinaryOperations(input);
std::cout << "Result: " << result << std::endl;

return 0;
}

Java
public class CalculateBinaryOperations {

public static int xor(int a, int b) {


return a ^ b;
}

public static int or(int a, int b) {


return a | b;
}

public static int and(int a, int b) {


return a & b;
}

public static int calculateBinaryOperations(String str) {


int ans = 0;

if (str.charAt(1) == 'C') {
ans = xor(Character.getNumericValue(str.charAt(0)),
Character.getNumericValue(str.charAt(2)));
} else if (str.charAt(1) == 'B') {
ans = or(Character.getNumericValue(str.charAt(0)),
Character.getNumericValue(str.charAt(2)));
} else {
ans = and(Character.getNumericValue(str.charAt(0)),
Character.getNumericValue(str.charAt(2)));
}

int i = 3;
while (i < str.length()) {
if (str.charAt(i) == 'C') {
ans = xor(ans, Character.getNumericValue(str.charAt(i + 1)));
} else if (str.charAt(i) == 'B') {
ans = or(ans, Character.getNumericValue(str.charAt(i + 1)));
} else {
ans = and(ans, Character.getNumericValue(str.charAt(i + 1)));
}

i += 2;
}

return ans;
}

public static void main(String[] args) {


// Example usage
String input = "1C0B1A1"; // Example string "1C0B1A1" corresponds to 1 XOR 0 OR 1
AND 1
int result = calculateBinaryOperations(input);
System.out.println("Result: " + result);
}
}
Q2.

Python:
def DectToNBase(n,number):
ans=""
d=dict()
for i in range(1,10):
if i not in d:
d[i]=i

letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
j=0
for i in range(10,36):
if i not in d:
d[i]=letters[j]
j+=1

# print(d)
remainder=number%n
number=number//n

ans+=str(d[remainder])

while remainder>0:
remainder=number%n
number//=n
if remainder==0:
break
# print(d[remainder])
ans+=str(d[remainder])

ans=ans[::-1]
return ans

C++
#include <iostream>
#include <string>
#include <unordered_map>

std::string dectToNBase(int n, int number) {


std::string ans = "";
std::unordered_map<int, std::string> d;

// Fill the map for digits and letters


for (int i = 1; i < 10; ++i) {
d[i] = std::to_string(i);
}

char letter = 'A';


for (int i = 10; i < 36; ++i) {
d[i] = std::string(1, letter);
++letter;
}

// Convert number to base n


int remainder = number % n;
number /= n;

ans += d[remainder];

while (remainder > 0) {


remainder = number % n;
number /= n;
if (remainder == 0) {
break;
}
ans += d[remainder];
}
// Reverse the result
std::reverse(ans.begin(), ans.end());
return ans;
}

int main() {
// Example usage
int n = 16;
int number = 255;

std::string result = dectToNBase(n, number);


std::cout << "Result: " << result << std::endl;

return 0;
}

Java
import java.util.HashMap;

public class DectToNBase {

public static String dectToNBase(int n, int number) {


StringBuilder ans = new StringBuilder();
HashMap<Integer, String> d = new HashMap<>();

// Fill the map for digits and letters


for (int i = 1; i < 10; ++i) {
d.put(i, String.valueOf(i));
}

char letter = 'A';


for (int i = 10; i < 36; ++i) {
d.put(i, String.valueOf(letter));
++letter;
}

// Convert number to base n


int remainder = number % n;
number /= n;

ans.append(d.get(remainder));

while (remainder > 0) {


remainder = number % n;
number /= n;
if (remainder == 0) {
break;
}
ans.append(d.get(remainder));
}

// Reverse the result


return ans.reverse().toString();
}

public static void main(String[] args) {


// Example usage
int n = 16;
int number = 255;

String result = dectToNBase(n, number);


System.out.println("Result: " + result);
}
}

You might also like