We post one challenging problem here every week. Solution due is 11:59 PM every Friday. We offer awards to two responders. 1. The first one with the right solution. 2. The correct solution with fewest code statements.
If we get only one correct solution, we will skip the award for the fewest code statements.
You need to create an account in this club in order to participate in this competition. The solution can use one of these three languages: Java, Python, JavaScript. You can submit your solution to this topic. Make sure your name and email account are in the end of the solution.
I have two strings:
X=”I think I am smart enough to program in Java”
Y=”You do not know what you are talking about”
Please find the character that appears the most times in string X but not in String Y. Print this character out and also print how many times it appears in string X.
Hello, here is a code for the challenge question
X = “I think I am smart enough to program in Java”
Y = “You do not know what you are talking about”
freq = [None] * len(X);
maxChar = X[10];
for i in range(0, len(X)):
for j in range(i + 1, len(X)):
if X[i] == X[j] and X[i] != ‘ ‘ and X[i] != ‘0’:
string = X[: j] + ‘0’ + X[j + 1:];
print(“Maximum occurring character: ” + maxChar);
There is no winner this time at all. xcvtf used Python. There should be no “;” in Pyhton as end of a statement. scienceqiu used Java Hashmap. Hashmap get method returns an object. Object does not have operator +. Ivan Zhang’s code get result char ‘a’ but ‘a’ does appear in string Y, which violates the requirements.
Our next challenge will be posted no later than 5 PM on Monday 5/25/2020. If you want that we indent your code correctly, please submit your code to this challenge along with a screenshot of your code in your IDE or your code file as an attachment. Remember you still need to submit your code as text here. Thanks for your participation.
Code for challenge question
X = “I think I am smart enough to program in Java”
Y = “You do not know what you are talking about”
Z = “”
for x in X.replace(“”, ” “):
if x not in Y:
Z = Z+x
#print(Z)
import collections
print(collections.Counter(Z.replace(” “, “”)).most_common(1)[0])
edit: here is the modified code
X = “I think I am smart enough to program in Java”
Y = “You do not know what you are talking about”
freq = [None] * len(X);
maxChar = X[10];
count = 0
for i in range(0, len(X)):
for j in range(i + 1, len(X)):
if X[i] == X[j] and X[i] != ‘ ‘ and X[i] != ‘0’:
string = X[: j] + ‘0’ + X[j + 1:];
for i in X:
if i == maxChar:
count = count + 1
print(“Maximum occurring character: ” + maxChar);
print(count)
char ret = ‘0’;
String x= “I think I am smart enough to program in Java”;
String y= “You do not know what you are talking about”;
int max =0;
HashMap s = new HashMap();
for (char c : x.toLowerCase().toCharArray())
if(!y.contains(Character.toString(c)))
if(!s.containsKey(c)) s.put(c, 0);
else s.put(c, s.get(c) + 1);
for(char c : s.keySet())
if(s.get(c) > max) {max = s.get(c); ret = c;}
System.out.println(ret);
EDIT: here is the modified code. Keep in mind that I used very few brackets, so when you run the code please indent the places that need indenting. This website automatically unindents my stuff:
char ret = ‘0’;
String x= “I think I am smart enough to program in Java”;
String y= “You do not know what you are talking about”;
int max =0;
HashMap s = new HashMap();
for (char c : x.toLowerCase().toCharArray())
if(!y.contains(Character.toString(c)))
if(!s.containsKey(c)) s.put(c, 0);
else s.put(c, s.get(c) + 1);
for(char c : s.keySet()) {
ret = (s.get(c)>max)? c: ret;
max = (s.get(c)>max)? s.get(c): max;}
System.out.println(“ret: “+ret+”\nmax: “+(max+1));
I messed up again. this is the final one i promis
char ret = ‘0’;
String x= “I think I am smart enough to program in Java”;
String y= “You do not know what you are talking about”;
int max =0;
HashMap s = new HashMap();
for (char c : x.toLowerCase().toCharArray())
if(!y.contains(Character.toString(c)))
if(!s.containsKey(c)) s.put(c, 0);
else s.put(c, s.get(c) + 1);
for(char c : s.keySet()) {
ret = (s.get(c)>max)? c: ret;
max = (s.get(c)>max)? s.get(c): max;}
System.out.println(“ret: “+ret+”\nmax: “+(max+1));
this website is messing up my code. it cuts out the part where it says hashmap
Here’s my code for Java:
package newpackage;
public class Homeworkdevelopment
{
public static void main(String[] args)
{
String X = “I think I am smart enough to program in Java”;
String Y = “You do not know what you are talking about”;
int[] numchar = new int[256]; int [] numchartwo = new int[256];
int x = X.length();
int z = Y.length();
for (int y = 0; y < x; y++)
{
numchar[X.charAt(y)]++;
}
for (int y = 0; y < z; y++)
{
numchartwo[Y.charAt(y)]++;
}
char mostchar = ' '; char minichar = ' '; char mostchartwo = ' ';
int mostshow = -1; int minishow = -1; int mostshowtwo = -1;
for (int y = 0; y < x; y++)
{
if (mostshow < numchar[X.charAt(y)])
{
mostshow = numchar[X.charAt(y)];
mostchar = X.charAt(y);
}
else if (minishow < numchar[X.charAt(y)] && numchar[X.charAt(y)] < mostshow)
{
minishow = numchar[X.charAt(y)];
minichar = X.charAt(y);
}
}
for (int y = 0; y < z; y++)
{
if (mostshowtwo < numchartwo[Y.charAt(y)])
{
mostshowtwo = numchartwo[Y.charAt(y)];
mostchartwo = Y.charAt(y);
}
}
String output = mostchartwo != mostchar ? "\"" + mostchar + "\" " + mostshow : "\"" + minichar + "\" " + minishow;
System.out.print(output);
}
}