0% found this document useful (0 votes)
39 views6 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)
39 views6 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

#Accenture 3rd Coding Class:

Explanation Video:
https://www.youtube.com/
live/sXnFBRczUl8?si=ilDO
8LlcgSEymxBv

Q1.

Python
import math

def RootsofEquation(a, b, c):


root1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
root2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)

return root1, root2

# Example usage
a=1
b = -3
c=2
result = RootsofEquation(a, b, c)
print("Roots of the equation:", result)

C++
#include <iostream>
#include <cmath>

std::pair<double, double> RootsofEquation(double a, double b, double c) {


double root1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
double root2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

return std::make_pair(root1, root2);


}

int main() {
// Example usage
double a = 1.0;
double b = -3.0;
double c = 2.0;

std::pair<double, double> result = RootsofEquation(a, b, c);


std::cout << "Roots of the equation: " << result.first << ", " << result.second << std::endl;

return 0;
}

Java
import java.util.*;

public class RootsofEquation {

public static List<Double> calculateRoots(double a, double b, double c) {


List<Double> roots = new ArrayList<>();

double root1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);


double root2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);

roots.add(root1);
roots.add(root2);
return roots;
}

public static void main(String[] args) {


// Example usage
double a = 1.0;
double b = -3.0;
double c = 2.0;

List<Double> result = calculateRoots(a, b, c);


System.out.println("Roots of the equation: " + result.get(0) + ", " + result.get(1));
}
}

Q2.

Python
def replace_most_frequent_character(string, x):
# Count frequencies of each character
d = {}
for char in string:
if char not in d:
d[char] = 1
else:
d[char] += 1

# Find the maximum frequency


max_freq = max(d.values())

# Find the character(s) with the maximum frequency


most_frequent_chars = [char for char in d if d[char] == max_freq]
most_frequent_char = min(most_frequent_chars) # Get lexicographically smallest char

# Replace the most frequent character with x


replaced_string = ''.join(x if char == most_frequent_char else char for char in string)

return replaced_string

# Example usage
input_string = "hello world"
replacement_char = '*'
result = replace_most_frequent_character(input_string, replacement_char)
print("Result:", result) # Output: "h*llo world"

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

std::string replaceMostFrequentCharacter(const std::string& str, char x) {


// Count frequencies of each character
std::unordered_map<char, int> freqMap;
for (char ch : str) {
freqMap[ch]++;
}

// Find the maximum frequency


int maxFreq = 0;
for (const auto& pair : freqMap) {
if (pair.second > maxFreq) {
maxFreq = pair.second;
}
}

// Find the character(s) with the maximum frequency


char mostFrequentChar = '\0';
for (const auto& pair : freqMap) {
if (pair.second == maxFreq) {
if (mostFrequentChar == '\0' || pair.first < mostFrequentChar) {
mostFrequentChar = pair.first;
}
}
}
// Replace the most frequent character with x
std::string replacedString;
for (char ch : str) {
if (ch == mostFrequentChar) {
replacedString += x;
} else {
replacedString += ch;
}
}

return replacedString;
}

int main() {
// Example usage
std::string inputString = "hello world";
char replacementChar = '*';
std::string result = replaceMostFrequentCharacter(inputString, replacementChar);
std::cout << "Result: " << result << std::endl; // Output: "h*llo world"

return 0;
}

Java
import java.util.HashMap;

public class ReplaceMostFrequentCharacter {

public static String replaceMostFrequentCharacter(String str, char x) {


// Count frequencies of each character
HashMap<Character, Integer> freqMap = new HashMap<>();
for (char ch : str.toCharArray()) {
freqMap.put(ch, freqMap.getOrDefault(ch, 0) + 1);
}

// Find the maximum frequency


int maxFreq = 0;
for (int freq : freqMap.values()) {
if (freq > maxFreq) {
maxFreq = freq;
}
}

// Find the character(s) with the maximum frequency


char mostFrequentChar = '\0';
for (char ch : freqMap.keySet()) {
if (freqMap.get(ch) == maxFreq) {
if (mostFrequentChar == '\0' || ch < mostFrequentChar) {
mostFrequentChar = ch;
}
}
}

// Replace the most frequent character with x


StringBuilder replacedString = new StringBuilder();
for (char ch : str.toCharArray()) {
if (ch == mostFrequentChar) {
replacedString.append(x);
} else {
replacedString.append(ch);
}
}

return replacedString.toString();
}

public static void main(String[] args) {


// Example usage
String inputString = "hello world";
char replacementChar = '*';
String result = replaceMostFrequentCharacter(inputString, replacementChar);
System.out.println("Result: " + result); // Output: "h*llo world"
}
}

You might also like